1use super::*;
7
8#[derive(Clone, Copy, Debug, PartialEq, SerializeDisplay, DeserializeFromStr)]
14#[cfg_attr(feature = "iterator", derive(enum_iterator::Sequence))]
15#[non_exhaustive]
16pub enum AwsRegion {
17 UsEast1,
19 UsEast2,
21 UsWest1,
23 UsWest2,
25 CaCentral1,
27 EuCentral1,
29 EuCentral2,
31 EuWest1,
33 EuWest2,
35 EuWest3,
37 EuSouth1,
39 EuSouth2,
41 EuNorth1,
43 AfSouth1,
45 ApEast1,
47 ApSouth1,
49 ApSouth2,
51 ApSoutheast1,
53 ApSoutheast2,
55 ApSoutheast3,
57 ApSoutheast4,
59 ApNorhteast1,
61 ApNorhteast2,
63 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 ..schemars::schema::SchemaObject::default()
177 };
178 object.into()
179 }
180}