zesty_api/location/
impls.rs

1//
2// Copyright 2023 Zesty Tech Ltd. All rights reserved.
3// Use is subject to license terms.
4//
5
6use super::*;
7
8impl Location {
9    pub fn region(&self) -> &str {
10        match self {
11            Self::Aws(region) => region.as_str(),
12            Self::Azure(region) => region.as_str(),
13            Self::Gcp(region) => region.as_str(),
14        }
15    }
16}
17
18impl fmt::Display for Location {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        match self {
21            Self::Aws(region) => region.fmt(f),
22            Self::Azure(region) => region.fmt(f),
23            Self::Gcp(region) => region.fmt(f),
24        }
25    }
26}
27
28impl From<AwsRegion> for Location {
29    fn from(region: AwsRegion) -> Self {
30        Self::Aws(region)
31    }
32}
33
34impl From<AzureRegion> for Location {
35    fn from(region: AzureRegion) -> Self {
36        Self::Azure(region)
37    }
38}
39
40impl From<GcpRegion> for Location {
41    fn from(region: GcpRegion) -> Self {
42        Self::Gcp(region)
43    }
44}
45
46impl FromStr for Location {
47    type Err = ParseError;
48
49    fn from_str(text: &str) -> Result<Self, Self::Err> {
50        let aws = text.parse::<AwsRegion>();
51        let azure = text.parse::<AzureRegion>();
52        let gcp = text.parse::<GcpRegion>();
53
54        match (aws, azure, gcp) {
55            (Ok(aws), Err(_), Err(_)) => Ok(aws.into()),
56            (Err(_), Ok(azure), Err(_)) => Ok(azure.into()),
57            (Err(_), Err(_), Ok(gcp)) => Ok(gcp.into()),
58
59            (Ok(aws), Ok(azure), Ok(gcp)) => Err(ParseError::ambiguous(&[
60                aws.as_str(),
61                azure.as_str(),
62                gcp.as_str(),
63            ])),
64            (Ok(aws), Ok(azure), Err(_)) => {
65                Err(ParseError::ambiguous(&[aws.as_str(), azure.as_str()]))
66            }
67            (Ok(aws), Err(_), Ok(gcp)) => Err(ParseError::ambiguous(&[aws.as_str(), gcp.as_str()])),
68            (Err(_), Ok(azure), Ok(gcp)) => {
69                Err(ParseError::ambiguous(&[azure.as_str(), gcp.as_str()]))
70            }
71
72            (Err(_), Err(_), Err(_)) => Err(ParseError::unknown(text)),
73        }
74    }
75}
76
77#[cfg(feature = "jsonschema")]
78impl schemars::JsonSchema for Location {
79    fn schema_name() -> String {
80        "Location".to_string()
81    }
82
83    fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
84        let aws = enum_iterator::all::<AwsRegion>().map(|region| format!("{region:#}"));
85        let azure = enum_iterator::all::<AzureRegion>().map(|region| format!("{region:#}"));
86        let gcp = enum_iterator::all::<GcpRegion>().map(|region| format!("{region:#}"));
87
88        let enum_values = aws.chain(azure).chain(gcp).map(Into::into).collect();
89        let instance_type = schemars::schema::InstanceType::String.into();
90        let object = schemars::schema::SchemaObject {
91            instance_type: Some(instance_type),
92            enum_values: Some(enum_values),
93            // metadata: todo!(),
94            // format: todo!(),
95            // const_value: todo!(),
96            // subschemas: todo!(),
97            // number: todo!(),
98            // string: todo!(),
99            // array: todo!(),
100            // object: todo!(),
101            // reference: todo!(),
102            // extensions: todo!(),
103            ..schemars::schema::SchemaObject::default()
104        };
105        object.into()
106    }
107}