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

use super::*;

impl Location {
    pub fn region(&self) -> &str {
        match self {
            Self::Aws(region) => region.as_str(),
            Self::Azure(region) => region.as_str(),
            Self::Gcp(region) => region.as_str(),
        }
    }
}

impl fmt::Display for Location {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Aws(region) => region.fmt(f),
            Self::Azure(region) => region.fmt(f),
            Self::Gcp(region) => region.fmt(f),
        }
    }
}

impl From<AwsRegion> for Location {
    fn from(region: AwsRegion) -> Self {
        Self::Aws(region)
    }
}

impl From<AzureRegion> for Location {
    fn from(region: AzureRegion) -> Self {
        Self::Azure(region)
    }
}

impl From<GcpRegion> for Location {
    fn from(region: GcpRegion) -> Self {
        Self::Gcp(region)
    }
}

impl FromStr for Location {
    type Err = ParseError;

    fn from_str(text: &str) -> Result<Self, Self::Err> {
        let aws = text.parse::<AwsRegion>();
        let azure = text.parse::<AzureRegion>();
        let gcp = text.parse::<GcpRegion>();

        match (aws, azure, gcp) {
            (Ok(aws), Err(_), Err(_)) => Ok(aws.into()),
            (Err(_), Ok(azure), Err(_)) => Ok(azure.into()),
            (Err(_), Err(_), Ok(gcp)) => Ok(gcp.into()),

            (Ok(aws), Ok(azure), Ok(gcp)) => Err(ParseError::ambiguous(&[
                aws.as_str(),
                azure.as_str(),
                gcp.as_str(),
            ])),
            (Ok(aws), Ok(azure), Err(_)) => {
                Err(ParseError::ambiguous(&[aws.as_str(), azure.as_str()]))
            }
            (Ok(aws), Err(_), Ok(gcp)) => Err(ParseError::ambiguous(&[aws.as_str(), gcp.as_str()])),
            (Err(_), Ok(azure), Ok(gcp)) => {
                Err(ParseError::ambiguous(&[azure.as_str(), gcp.as_str()]))
            }

            (Err(_), Err(_), Err(_)) => Err(ParseError::unknown(text)),
        }
    }
}

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

    fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
        let aws = enum_iterator::all::<AwsRegion>().map(|region| format!("{region:#}"));
        let azure = enum_iterator::all::<AzureRegion>().map(|region| format!("{region:#}"));
        let gcp = enum_iterator::all::<GcpRegion>().map(|region| format!("{region:#}"));

        let enum_values = aws.chain(azure).chain(gcp).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()
    }
}