zesty_api/location/
gcp.rs

1//
2// Copyright 2023 Zesty Tech Ltd. All rights reserved.
3// Use is subject to license terms.
4//
5
6use super::*;
7
8#[derive(Clone, Copy, Debug, PartialEq, SerializeDisplay, DeserializeFromStr)]
9#[cfg_attr(feature = "iterator", derive(enum_iterator::Sequence))]
10#[non_exhaustive]
11pub enum GcpRegion {
12    UsEast1,
13    UsEast2,
14    UsWest1,
15    UsWest2,
16}
17
18impl GcpRegion {
19    pub fn as_str(&self) -> &'static str {
20        match self {
21            Self::UsEast1 => "us-east1",
22            Self::UsEast2 => "us-east2",
23            Self::UsWest1 => "us-west1",
24            Self::UsWest2 => "us-west2",
25        }
26    }
27}
28
29impl CloudLocation for GcpRegion {
30    const CLOUD_VENDOR: &'static str = "Google";
31    const CLOUD_VENDOR_PREFIX: &'static str = "gcp/";
32}
33
34impl FromStr for GcpRegion {
35    type Err = InvalidRegion;
36
37    fn from_str(text: &str) -> Result<Self, Self::Err> {
38        match Self::normal_region(text).as_str() {
39            "us-east1" => Ok(Self::UsEast1),
40            "us-east2" => Ok(Self::UsEast2),
41            "us-west1" => Ok(Self::UsWest1),
42            "us-west2" => Ok(Self::UsWest2),
43            _ => Err(InvalidRegion::new(text)),
44        }
45    }
46}
47
48impl fmt::Display for GcpRegion {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        if f.alternate() {
51            f.write_fmt(format_args!(
52                "{}{}",
53                Self::CLOUD_VENDOR_PREFIX,
54                self.as_str()
55            ))
56        } else {
57            self.as_str().fmt(f)
58        }
59    }
60}
61
62#[cfg(feature = "jsonschema")]
63impl schemars::JsonSchema for GcpRegion {
64    fn schema_name() -> String {
65        "GcpRegion".to_string()
66    }
67
68    fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
69        let enum_values = enum_iterator::all::<Self>()
70            .flat_map(|region| [format!("{region}"), format!("{region:#}")])
71            .map(Into::into)
72            .collect();
73        let instance_type = schemars::schema::InstanceType::String.into();
74        let object = schemars::schema::SchemaObject {
75            instance_type: Some(instance_type),
76            enum_values: Some(enum_values),
77            // metadata: todo!(),
78            // format: todo!(),
79            // const_value: todo!(),
80            // subschemas: todo!(),
81            // number: todo!(),
82            // string: todo!(),
83            // array: todo!(),
84            // object: todo!(),
85            // reference: todo!(),
86            // extensions: todo!(),
87            ..schemars::schema::SchemaObject::default()
88        };
89        object.into()
90    }
91}