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
//
// Copyright 2023 Zesty Tech Ltd. All rights reserved.
// Use is subject to license terms.
//

use super::*;

#[derive(Clone, Copy, Debug, PartialEq, SerializeDisplay, DeserializeFromStr)]
#[cfg_attr(feature = "iterator", derive(enum_iterator::Sequence))]
#[non_exhaustive]
pub enum AzureRegion {
    EastUs,
    EastUs2,
    WestUs,
    WestUs2,
}

impl CloudLocation for AzureRegion {
    const CLOUD_VENDOR: &'static str = "Azure";
    const CLOUD_VENDOR_PREFIX: &'static str = "azure/";

    fn as_str(&self) -> &'static str {
        match self {
            Self::EastUs => "eastus",
            Self::EastUs2 => "eastus2",
            Self::WestUs => "westus",
            Self::WestUs2 => "westus2",
        }
    }
}

impl FromStr for AzureRegion {
    type Err = InvalidRegion;

    fn from_str(text: &str) -> Result<Self, Self::Err> {
        match Self::normal_region(text).as_str() {
            "eastus" => Ok(Self::EastUs),
            "eastus2" => Ok(Self::EastUs2),
            "westus" => Ok(Self::WestUs),
            "westus2" => Ok(Self::WestUs2),
            _ => Err(InvalidRegion::new(text)),
        }
    }
}

impl fmt::Display for AzureRegion {
    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 AzureRegion {
    fn schema_name() -> String {
        "AzureRegion".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()
    }
}