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
//
// 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) => f.write_fmt(format_args!("{region:#}")),
            Self::Azure(region) => f.write_fmt(format_args!("{region:#}")),
            Self::Gcp(region) => f.write_fmt(format_args!("{region:#}")),
        }
    }
}

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)),
        }
    }
}