1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
// Copyright 2023 Zesty Tech Ltd. All rights reserved.
// Use is subject to license terms.
//

use super::*;

//
// See https://docs.aws.amazon.com/general/latest/gr/rande.html for official
// AWS region documentation
//

#[derive(Clone, Copy, Debug, PartialEq, SerializeDisplay, DeserializeFromStr)]
#[cfg_attr(feature = "iterator", derive(enum_iterator::Sequence))]
#[non_exhaustive]
pub enum AwsRegion {
    /// US East (N. Virginia)
    UsEast1,
    /// US East (Ohio)
    UsEast2,
    /// US West (N. California)
    UsWest1,
    /// US West (Oregon)
    UsWest2,
    /// Canada (Central)
    CaCentral1,
    /// Europe (Frankfurt)
    EuCentral1,
    /// Europe (Zurich)
    EuCentral2,
    /// Europe (Ireland)
    EuWest1,
    /// Europe (London)
    EuWest2,
    /// Europe (Paris)
    EuWest3,
    /// Europe (Milan)
    EuSouth1,
    /// Europe (Spain)
    EuSouth2,
    /// Europe (Stockholm)
    EuNorth1,
    /// Africa (Cape Town)
    AfSouth1,
    /// Asia Pacific (Hong Kong)
    ApEast1,
    /// Asia Pacific (Mumbai)
    ApSouth1,
    /// Asia Pacific (Hyderabad)
    ApSouth2,
    /// Asia Pacific (Singapore)
    ApSoutheast1,
    /// Asia Pacific (Sydney)
    ApSoutheast2,
    /// Asia Pacific (Jakarta)
    ApSoutheast3,
    /// Asia Pacific (Melbourne)
    ApSoutheast4,
    /// Asia Pacific (Tokyo)
    ApNorhteast1,
    /// Asia Pacific (Seoul)
    ApNorhteast2,
    /// Asia Pacific (Osaka)
    ApNorhteast3,
}

impl AwsRegion {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::UsEast1 => "us-east-1",
            Self::UsEast2 => "us-east-2",
            Self::UsWest1 => "us-west-1",
            Self::UsWest2 => "us-west-2",
            Self::CaCentral1 => "ca-central-1",
            Self::EuCentral1 => "eu-central-1",
            Self::EuCentral2 => "eu-central-2",
            Self::EuWest1 => "eu-west-1",
            Self::EuWest2 => "eu-west-2",
            Self::EuWest3 => "eu-west-3",
            Self::EuSouth1 => "eu-south-1",
            Self::EuSouth2 => "eu-south-2",
            Self::EuNorth1 => "eu-north-1",
            Self::AfSouth1 => "af-south-1",
            Self::ApEast1 => "ap-east-1",
            Self::ApSouth1 => "ap-south-1",
            Self::ApSouth2 => "ap-south-2",
            Self::ApSoutheast1 => "ap-southeast-1",
            Self::ApSoutheast2 => "ap-southeast-2",
            Self::ApSoutheast3 => "ap-southeast-3",
            Self::ApSoutheast4 => "ap-southeast-4",
            Self::ApNorhteast1 => "ap-northeast-1",
            Self::ApNorhteast2 => "ap-northeast-2",
            Self::ApNorhteast3 => "ap-northeast-3",
        }
    }
}

impl CloudLocation for AwsRegion {
    const CLOUD_VENDOR: &'static str = "AWS";
    const CLOUD_VENDOR_PREFIX: &'static str = "aws/";
}

impl FromStr for AwsRegion {
    type Err = InvalidRegion;

    fn from_str(text: &str) -> Result<Self, Self::Err> {
        match Self::normal_region(text).as_str() {
            "us-east-1" => Ok(Self::UsEast1),
            "us-east-2" => Ok(Self::UsEast2),
            "us-west-1" => Ok(Self::UsWest1),
            "us-west-2" => Ok(Self::UsWest2),
            "ca-central-1" => Ok(Self::CaCentral1),
            "eu-central-1" => Ok(Self::EuCentral1),
            "eu-central-2" => Ok(Self::EuCentral2),
            "eu-west-1" => Ok(Self::EuWest1),
            "eu-west-2" => Ok(Self::EuWest2),
            "eu-west-3" => Ok(Self::EuWest3),
            "eu-south-1" => Ok(Self::EuSouth1),
            "eu-south-2" => Ok(Self::EuSouth2),
            "eu-north-1" => Ok(Self::EuNorth1),
            "af-south-1" => Ok(Self::AfSouth1),
            "ap-east-1" => Ok(Self::ApEast1),
            "ap-south-1" => Ok(Self::ApSouth1),
            "ap-south-2" => Ok(Self::ApSouth2),
            "ap-southeast-1" => Ok(Self::ApSoutheast1),
            "ap-southeast-2" => Ok(Self::ApSoutheast2),
            "ap-southeast-3" => Ok(Self::ApSoutheast3),
            "ap-southeast-4" => Ok(Self::ApSoutheast4),
            "ap-northeast-1" => Ok(Self::ApNorhteast1),
            "ap-northeast-2" => Ok(Self::ApNorhteast2),
            "ap-northeast-3" => Ok(Self::ApNorhteast3),
            _ => Err(InvalidRegion::new(text)),
        }
    }
}

impl fmt::Display for AwsRegion {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if f.alternate() {
            f.write_fmt(format_args!(
                "{}{}",
                Self::CLOUD_VENDOR_PREFIX,
                self.as_str()
            ))
        } else {
            self.as_str().fmt(f)
        }
    }
}

#[cfg(feature = "jsonschema")]
impl schemars::JsonSchema for AwsRegion {
    fn schema_name() -> String {
        "AwsRegion".to_string()
    }

    fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
        let enum_values = enum_iterator::all::<Self>()
            .flat_map(|region| [format!("{region}"), format!("{region:#}")])
            .map(Into::into)
            .collect();
        let instance_type = schemars::schema::InstanceType::String.into();
        let object = schemars::schema::SchemaObject {
            instance_type: Some(instance_type),
            enum_values: Some(enum_values),
            // metadata: todo!(),
            // format: todo!(),
            // const_value: todo!(),
            // subschemas: todo!(),
            // number: todo!(),
            // string: todo!(),
            // array: todo!(),
            // object: todo!(),
            // reference: todo!(),
            // extensions: todo!(),
            ..schemars::schema::SchemaObject::default()
        };
        object.into()
    }
}