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),
..schemars::schema::SchemaObject::default()
};
object.into()
}
}