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
//
// 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 GcpRegion {
    UsEast1,
    UsEast2,
    UsWest1,
    UsWest2,
}

impl GcpRegion {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::UsEast1 => "us-east1",
            Self::UsEast2 => "us-east2",
            Self::UsWest1 => "us-west1",
            Self::UsWest2 => "us-west2",
        }
    }
}

impl CloudLocation for GcpRegion {
    const CLOUD_VENDOR: &'static str = "Google";
    const CLOUD_VENDOR_PREFIX: &'static str = "gcp/";
}

impl FromStr for GcpRegion {
    type Err = InvalidRegion;

    fn from_str(text: &str) -> Result<Self, Self::Err> {
        match Self::normal_region(text).as_str() {
            "us-east1" => Ok(Self::UsEast1),
            "us-east2" => Ok(Self::UsEast2),
            "us-west1" => Ok(Self::UsWest1),
            "us-west2" => Ok(Self::UsWest2),
            _ => Err(InvalidRegion::new(text)),
        }
    }
}

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