use std::fmt;
use std::str::FromStr;
use serde_with::{DeserializeFromStr, SerializeDisplay};
use thiserror::Error;
#[derive(Clone, Copy, Debug, PartialEq, SerializeDisplay, DeserializeFromStr)]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
#[cfg_attr(feature = "iterator", derive(enum_iterator::Sequence))]
pub enum Vendor {
Aks,
Eks,
Gke,
}
impl Vendor {
pub fn as_str(&self) -> &'static str {
match self {
Self::Aks => "AKS",
Self::Eks => "EKS",
Self::Gke => "GKE",
}
}
}
impl fmt::Display for Vendor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.as_str().fmt(f)
}
}
impl FromStr for Vendor {
type Err = InvalidVendor;
fn from_str(text: &str) -> Result<Self, Self::Err> {
match text.to_uppercase().as_str() {
"AKS" => Ok(Self::Aks),
"EKS" => Ok(Self::Eks),
"GKE" => Ok(Self::Gke),
other => Err(InvalidVendor(other.to_string())),
}
}
}
#[cfg(feature = "jsonschema")]
impl schemars::JsonSchema for Vendor {
fn schema_name() -> String {
"Vendor".to_string()
}
fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
let enum_values = enum_iterator::all::<Self>()
.map(|vendor| vendor.to_string())
.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()
}
}
#[derive(Debug, Error)]
#[error("Invalid vendor: {0}")]
pub struct InvalidVendor(String);