use super::*;
#[derive(Clone, Copy, Debug, PartialEq, SerializeDisplay, DeserializeFromStr)]
#[cfg_attr(feature = "iterator", derive(enum_iterator::Sequence))]
#[non_exhaustive]
pub enum AwsRegion {
UsEast1,
UsEast2,
UsWest1,
UsWest2,
CaCentral1,
EuCentral1,
EuCentral2,
EuWest1,
EuWest2,
EuWest3,
EuSouth1,
EuSouth2,
EuNorth1,
AfSouth1,
ApEast1,
ApSouth1,
ApSouth2,
ApSoutheast1,
ApSoutheast2,
ApSoutheast3,
ApSoutheast4,
ApNorhteast1,
ApNorhteast2,
ApNorhteast3,
}
impl AwsRegion {
pub fn as_str(&self) -> &'static str {
match self {
Self::UsEast1 => "us-east-1",
Self::UsEast2 => "us-east-2",
Self::UsWest1 => "us-west-1",
Self::UsWest2 => "us-west-2",
Self::CaCentral1 => "ca-central-1",
Self::EuCentral1 => "eu-central-1",
Self::EuCentral2 => "eu-central-2",
Self::EuWest1 => "eu-west-1",
Self::EuWest2 => "eu-west-2",
Self::EuWest3 => "eu-west-3",
Self::EuSouth1 => "eu-south-1",
Self::EuSouth2 => "eu-south-2",
Self::EuNorth1 => "eu-north-1",
Self::AfSouth1 => "af-south-1",
Self::ApEast1 => "ap-east-1",
Self::ApSouth1 => "ap-south-1",
Self::ApSouth2 => "ap-south-2",
Self::ApSoutheast1 => "ap-southeast-1",
Self::ApSoutheast2 => "ap-southeast-2",
Self::ApSoutheast3 => "ap-southeast-3",
Self::ApSoutheast4 => "ap-southeast-4",
Self::ApNorhteast1 => "ap-northeast-1",
Self::ApNorhteast2 => "ap-northeast-2",
Self::ApNorhteast3 => "ap-northeast-3",
}
}
}
impl CloudLocation for AwsRegion {
const CLOUD_VENDOR: &'static str = "AWS";
const CLOUD_VENDOR_PREFIX: &'static str = "aws/";
}
impl FromStr for AwsRegion {
type Err = InvalidRegion;
fn from_str(text: &str) -> Result<Self, Self::Err> {
match Self::normal_region(text).as_str() {
"us-east-1" => Ok(Self::UsEast1),
"us-east-2" => Ok(Self::UsEast2),
"us-west-1" => Ok(Self::UsWest1),
"us-west-2" => Ok(Self::UsWest2),
"ca-central-1" => Ok(Self::CaCentral1),
"eu-central-1" => Ok(Self::EuCentral1),
"eu-central-2" => Ok(Self::EuCentral2),
"eu-west-1" => Ok(Self::EuWest1),
"eu-west-2" => Ok(Self::EuWest2),
"eu-west-3" => Ok(Self::EuWest3),
"eu-south-1" => Ok(Self::EuSouth1),
"eu-south-2" => Ok(Self::EuSouth2),
"eu-north-1" => Ok(Self::EuNorth1),
"af-south-1" => Ok(Self::AfSouth1),
"ap-east-1" => Ok(Self::ApEast1),
"ap-south-1" => Ok(Self::ApSouth1),
"ap-south-2" => Ok(Self::ApSouth2),
"ap-southeast-1" => Ok(Self::ApSoutheast1),
"ap-southeast-2" => Ok(Self::ApSoutheast2),
"ap-southeast-3" => Ok(Self::ApSoutheast3),
"ap-southeast-4" => Ok(Self::ApSoutheast4),
"ap-northeast-1" => Ok(Self::ApNorhteast1),
"ap-northeast-2" => Ok(Self::ApNorhteast2),
"ap-northeast-3" => Ok(Self::ApNorhteast3),
_ => Err(InvalidRegion::new(text)),
}
}
}
impl fmt::Display for AwsRegion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if f.alternate() {
f.write_fmt(format_args!(
"{}{}",
Self::CLOUD_VENDOR_PREFIX,
self.as_str()
))
} else {
self.as_str().fmt(f)
}
}
}
#[cfg(feature = "jsonschema")]
impl schemars::JsonSchema for AwsRegion {
fn schema_name() -> String {
"AwsRegion".to_string()
}
fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
let enum_values = enum_iterator::all::<Self>()
.flat_map(|region| [format!("{region}"), format!("{region:#}")])
.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),
..schemars::schema::SchemaObject::default()
};
object.into()
}
}