statehub_location/azure/
impls.rs

1//
2// Copyright (c) 2021 RepliXio Ltd. All rights reserved.
3// Use is subject to license terms.
4//
5
6use std::fmt;
7use std::str;
8
9use super::*;
10
11impl CloudLocation for AzureRegion {
12    const VENDOR: &'static str = "Azure";
13    const VENDOR_PREFIX: &'static str = "azure/";
14
15    fn as_str(&self) -> &'static str {
16        match self {
17            Self::CentralUs => "centralus",
18            Self::EastUs => "eastus",
19            Self::EastUs2 => "eastus2",
20            Self::FranceCentral => "francecentral",
21            Self::JapanEast => "japaneast",
22            Self::NorthEurope => "northeurope",
23            Self::SouthEastasia => "southeastasia",
24            Self::UkSouth => "uksouth",
25            Self::WestEurope => "westeurope",
26            Self::WestUs2 => "westus2",
27        }
28    }
29}
30
31impl str::FromStr for AzureRegion {
32    type Err = InvalidLocation;
33
34    fn from_str(s: &str) -> Result<Self, Self::Err> {
35        let text = s.strip_prefix(Self::VENDOR_PREFIX).unwrap_or(s);
36        match text {
37            "centralus" => Ok(Self::CentralUs),
38            "eastus" => Ok(Self::EastUs),
39            "eastus2" => Ok(Self::EastUs2),
40            "francecentral" => Ok(Self::FranceCentral),
41            "japaneast" => Ok(Self::JapanEast),
42            "northeurope" => Ok(Self::NorthEurope),
43            "southeastasia" => Ok(Self::SouthEastasia),
44            "uksouth" => Ok(Self::UkSouth),
45            "westeurope" => Ok(Self::WestEurope),
46            "westus2" => Ok(Self::WestUs2),
47            other => Err(InvalidLocation::new(Self::VENDOR, other)),
48        }
49    }
50}
51
52impl fmt::Display for AzureRegion {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        let text = self.as_str();
55        if f.alternate() {
56            format!("{}{}", Self::VENDOR_PREFIX, text).fmt(f)
57        } else {
58            text.fmt(f)
59        }
60    }
61}
62
63#[cfg(feature = "jsonschema")]
64impl schemars::JsonSchema for AzureRegion {
65    fn schema_name() -> String {
66        String::from("AzureRegion")
67    }
68
69    fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
70        use enum_iterator::IntoEnumIterator;
71
72        let enum_values = Self::into_enum_iter()
73            .flat_map(|region| [format!("{}", region), format!("{:#}", region)])
74            .map(Into::into)
75            .collect();
76        schemars::schema::Schema::Object(schemars::schema::SchemaObject {
77            instance_type: Some(schemars::schema::InstanceType::String.into()),
78            enum_values: Some(enum_values),
79            ..schemars::schema::SchemaObject::default()
80        })
81    }
82}