aws_types/
region.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6//! Region type for determining the endpoint to send requests to.
7
8use aws_smithy_types::config_bag::{Storable, StoreReplace};
9use std::borrow::Cow;
10use std::fmt::{Display, Formatter};
11
12/// 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
24    Cow<'static, str>,
25);
26
27impl AsRef<str> for Region {
28    fn as_ref(&self) -> &str {
29        &self.0
30    }
31}
32
33impl Display for Region {
34    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
35        write!(f, "{}", self.0)
36    }
37}
38
39impl Storable for Region {
40    type Storer = StoreReplace<Region>;
41}
42
43impl Region {
44    /// Creates a new `Region` from the given string.
45    pub fn new(region: impl Into<Cow<'static, str>>) -> Self {
46        Self(region.into())
47    }
48
49    /// Const function that creates a new `Region` from a static str.
50    pub const fn from_static(region: &'static str) -> Self {
51        Self(Cow::Borrowed(region))
52    }
53}
54
55/// 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>);
60
61impl AsRef<str> for SigningRegion {
62    fn as_ref(&self) -> &str {
63        &self.0
64    }
65}
66
67impl From<Region> for SigningRegion {
68    fn from(inp: Region) -> Self {
69        SigningRegion(inp.0)
70    }
71}
72
73impl From<&'static str> for SigningRegion {
74    fn from(region: &'static str) -> Self {
75        Self::from_static(region)
76    }
77}
78
79impl SigningRegion {
80    /// Creates a `SigningRegion` from a static str.
81    pub const fn from_static(region: &'static str) -> Self {
82        SigningRegion(Cow::Borrowed(region))
83    }
84}
85
86impl Storable for SigningRegion {
87    type Storer = StoreReplace<Self>;
88}
89
90// 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>);
95
96impl From<Region> for SigningRegionSet {
97    fn from(inp: Region) -> Self {
98        SigningRegionSet(inp.0)
99    }
100}
101
102impl From<&'static str> for SigningRegionSet {
103    fn from(region: &'static str) -> Self {
104        SigningRegionSet(Cow::Borrowed(region))
105    }
106}
107
108impl<'a> FromIterator<&'a str> for SigningRegionSet {
109    fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self {
110        let mut s = String::new();
111        let mut iter = iter.into_iter();
112
113        if let Some(region) = iter.next() {
114            s.push_str(region);
115        }
116
117        // If more than one region is present in the iter, separate remaining regions with commas
118        for region in iter {
119            s.push(',');
120            s.push_str(region);
121        }
122
123        SigningRegionSet(Cow::Owned(s))
124    }
125}
126
127impl Storable for SigningRegionSet {
128    type Storer = StoreReplace<Self>;
129}
130
131impl AsRef<str> for SigningRegionSet {
132    fn as_ref(&self) -> &str {
133        self.0.as_ref()
134    }
135}