1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
56//! Region type for determining the endpoint to send requests to.
78use aws_smithy_types::config_bag::{Storable, StoreReplace};
9use std::borrow::Cow;
10use std::fmt::{Display, Formatter};
1112/// The region to send requests to.
13///
14/// The region MUST be specified on a request. It may be configured globally or on a
15/// per-client basis unless otherwise noted. A full list of regions is found in the
16/// "Regions and Endpoints" document.
17///
18/// See <http://docs.aws.amazon.com/general/latest/gr/rande.html> for
19/// information on AWS regions.
20#[derive(Clone, Debug, PartialEq, Eq, Hash)]
21pub struct Region(
22// Regions are almost always known statically. However, as an escape hatch for when they
23 // are not, allow for an owned region
24Cow<'static, str>,
25);
2627impl AsRef<str> for Region {
28fn as_ref(&self) -> &str {
29&self.0
30}
31}
3233impl Display for Region {
34fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
35write!(f, "{}", self.0)
36 }
37}
3839impl Storable for Region {
40type Storer = StoreReplace<Region>;
41}
4243impl Region {
44/// Creates a new `Region` from the given string.
45pub fn new(region: impl Into<Cow<'static, str>>) -> Self {
46Self(region.into())
47 }
4849/// Const function that creates a new `Region` from a static str.
50pub const fn from_static(region: &'static str) -> Self {
51Self(Cow::Borrowed(region))
52 }
53}
5455/// The region to use when signing requests
56///
57/// Generally, user code will not need to interact with `SigningRegion`. See `[Region](crate::Region)`.
58#[derive(Clone, Debug, PartialEq, Eq)]
59pub struct SigningRegion(Cow<'static, str>);
6061impl AsRef<str> for SigningRegion {
62fn as_ref(&self) -> &str {
63&self.0
64}
65}
6667impl From<Region> for SigningRegion {
68fn from(inp: Region) -> Self {
69 SigningRegion(inp.0)
70 }
71}
7273impl From<&'static str> for SigningRegion {
74fn from(region: &'static str) -> Self {
75Self::from_static(region)
76 }
77}
7879impl SigningRegion {
80/// Creates a `SigningRegion` from a static str.
81pub const fn from_static(region: &'static str) -> Self {
82 SigningRegion(Cow::Borrowed(region))
83 }
84}
8586impl Storable for SigningRegion {
87type Storer = StoreReplace<Self>;
88}
8990// The region set to use when signing Sigv4a requests
91///
92/// Generally, user code will not need to interact with `SigningRegionSet`. See `[Region](crate::Region)`.
93#[derive(Clone, Debug, PartialEq, Eq)]
94pub struct SigningRegionSet(Cow<'static, str>);
9596impl From<Region> for SigningRegionSet {
97fn from(inp: Region) -> Self {
98 SigningRegionSet(inp.0)
99 }
100}
101102impl From<&'static str> for SigningRegionSet {
103fn from(region: &'static str) -> Self {
104 SigningRegionSet(Cow::Borrowed(region))
105 }
106}
107108impl<'a> FromIterator<&'a str> for SigningRegionSet {
109fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self {
110let mut s = String::new();
111let mut iter = iter.into_iter();
112113if let Some(region) = iter.next() {
114 s.push_str(region);
115 }
116117// If more than one region is present in the iter, separate remaining regions with commas
118for region in iter {
119 s.push(',');
120 s.push_str(region);
121 }
122123 SigningRegionSet(Cow::Owned(s))
124 }
125}
126127impl Storable for SigningRegionSet {
128type Storer = StoreReplace<Self>;
129}
130131impl AsRef<str> for SigningRegionSet {
132fn as_ref(&self) -> &str {
133self.0.as_ref()
134 }
135}