zesty_api/location/
aws.rs

1//
2// Copyright 2023 Zesty Tech Ltd. All rights reserved.
3// Use is subject to license terms.
4//
5
6use super::*;
7
8//
9// See https://docs.aws.amazon.com/general/latest/gr/rande.html for official
10// AWS region documentation
11//
12
13#[derive(Clone, Copy, Debug, PartialEq, SerializeDisplay, DeserializeFromStr)]
14#[cfg_attr(feature = "iterator", derive(enum_iterator::Sequence))]
15#[non_exhaustive]
16pub enum AwsRegion {
17    /// US East (N. Virginia)
18    UsEast1,
19    /// US East (Ohio)
20    UsEast2,
21    /// US West (N. California)
22    UsWest1,
23    /// US West (Oregon)
24    UsWest2,
25    /// Canada (Central)
26    CaCentral1,
27    /// Europe (Frankfurt)
28    EuCentral1,
29    /// Europe (Zurich)
30    EuCentral2,
31    /// Europe (Ireland)
32    EuWest1,
33    /// Europe (London)
34    EuWest2,
35    /// Europe (Paris)
36    EuWest3,
37    /// Europe (Milan)
38    EuSouth1,
39    /// Europe (Spain)
40    EuSouth2,
41    /// Europe (Stockholm)
42    EuNorth1,
43    /// Africa (Cape Town)
44    AfSouth1,
45    /// Asia Pacific (Hong Kong)
46    ApEast1,
47    /// Asia Pacific (Mumbai)
48    ApSouth1,
49    /// Asia Pacific (Hyderabad)
50    ApSouth2,
51    /// Asia Pacific (Singapore)
52    ApSoutheast1,
53    /// Asia Pacific (Sydney)
54    ApSoutheast2,
55    /// Asia Pacific (Jakarta)
56    ApSoutheast3,
57    /// Asia Pacific (Melbourne)
58    ApSoutheast4,
59    /// Asia Pacific (Tokyo)
60    ApNorhteast1,
61    /// Asia Pacific (Seoul)
62    ApNorhteast2,
63    /// Asia Pacific (Osaka)
64    ApNorhteast3,
65}
66
67impl AwsRegion {
68    pub fn as_str(&self) -> &'static str {
69        match self {
70            Self::UsEast1 => "us-east-1",
71            Self::UsEast2 => "us-east-2",
72            Self::UsWest1 => "us-west-1",
73            Self::UsWest2 => "us-west-2",
74            Self::CaCentral1 => "ca-central-1",
75            Self::EuCentral1 => "eu-central-1",
76            Self::EuCentral2 => "eu-central-2",
77            Self::EuWest1 => "eu-west-1",
78            Self::EuWest2 => "eu-west-2",
79            Self::EuWest3 => "eu-west-3",
80            Self::EuSouth1 => "eu-south-1",
81            Self::EuSouth2 => "eu-south-2",
82            Self::EuNorth1 => "eu-north-1",
83            Self::AfSouth1 => "af-south-1",
84            Self::ApEast1 => "ap-east-1",
85            Self::ApSouth1 => "ap-south-1",
86            Self::ApSouth2 => "ap-south-2",
87            Self::ApSoutheast1 => "ap-southeast-1",
88            Self::ApSoutheast2 => "ap-southeast-2",
89            Self::ApSoutheast3 => "ap-southeast-3",
90            Self::ApSoutheast4 => "ap-southeast-4",
91            Self::ApNorhteast1 => "ap-northeast-1",
92            Self::ApNorhteast2 => "ap-northeast-2",
93            Self::ApNorhteast3 => "ap-northeast-3",
94        }
95    }
96}
97
98impl CloudLocation for AwsRegion {
99    const CLOUD_VENDOR: &'static str = "AWS";
100    const CLOUD_VENDOR_PREFIX: &'static str = "aws/";
101}
102
103impl FromStr for AwsRegion {
104    type Err = InvalidRegion;
105
106    fn from_str(text: &str) -> Result<Self, Self::Err> {
107        match Self::normal_region(text).as_str() {
108            "us-east-1" => Ok(Self::UsEast1),
109            "us-east-2" => Ok(Self::UsEast2),
110            "us-west-1" => Ok(Self::UsWest1),
111            "us-west-2" => Ok(Self::UsWest2),
112            "ca-central-1" => Ok(Self::CaCentral1),
113            "eu-central-1" => Ok(Self::EuCentral1),
114            "eu-central-2" => Ok(Self::EuCentral2),
115            "eu-west-1" => Ok(Self::EuWest1),
116            "eu-west-2" => Ok(Self::EuWest2),
117            "eu-west-3" => Ok(Self::EuWest3),
118            "eu-south-1" => Ok(Self::EuSouth1),
119            "eu-south-2" => Ok(Self::EuSouth2),
120            "eu-north-1" => Ok(Self::EuNorth1),
121            "af-south-1" => Ok(Self::AfSouth1),
122            "ap-east-1" => Ok(Self::ApEast1),
123            "ap-south-1" => Ok(Self::ApSouth1),
124            "ap-south-2" => Ok(Self::ApSouth2),
125            "ap-southeast-1" => Ok(Self::ApSoutheast1),
126            "ap-southeast-2" => Ok(Self::ApSoutheast2),
127            "ap-southeast-3" => Ok(Self::ApSoutheast3),
128            "ap-southeast-4" => Ok(Self::ApSoutheast4),
129            "ap-northeast-1" => Ok(Self::ApNorhteast1),
130            "ap-northeast-2" => Ok(Self::ApNorhteast2),
131            "ap-northeast-3" => Ok(Self::ApNorhteast3),
132            _ => Err(InvalidRegion::new(text)),
133        }
134    }
135}
136
137impl fmt::Display for AwsRegion {
138    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
139        if f.alternate() {
140            f.write_fmt(format_args!(
141                "{}{}",
142                Self::CLOUD_VENDOR_PREFIX,
143                self.as_str()
144            ))
145        } else {
146            self.as_str().fmt(f)
147        }
148    }
149}
150
151#[cfg(feature = "jsonschema")]
152impl schemars::JsonSchema for AwsRegion {
153    fn schema_name() -> String {
154        "AwsRegion".to_string()
155    }
156
157    fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
158        let enum_values = enum_iterator::all::<Self>()
159            .flat_map(|region| [format!("{region}"), format!("{region:#}")])
160            .map(Into::into)
161            .collect();
162        let instance_type = schemars::schema::InstanceType::String.into();
163        let object = schemars::schema::SchemaObject {
164            instance_type: Some(instance_type),
165            enum_values: Some(enum_values),
166            // metadata: todo!(),
167            // format: todo!(),
168            // const_value: todo!(),
169            // subschemas: todo!(),
170            // number: todo!(),
171            // string: todo!(),
172            // array: todo!(),
173            // object: todo!(),
174            // reference: todo!(),
175            // extensions: todo!(),
176            ..schemars::schema::SchemaObject::default()
177        };
178        object.into()
179    }
180}