zesty_api/
vendor.rs

1//
2// Copyright 2023 Zesty Tech Ltd. All rights reserved.
3// Use is subject to license terms.
4//
5
6use std::fmt;
7use std::str::FromStr;
8
9use serde_with::{DeserializeFromStr, SerializeDisplay};
10use thiserror::Error;
11
12#[derive(Clone, Copy, Debug, PartialEq, SerializeDisplay, DeserializeFromStr)]
13#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
14#[cfg_attr(feature = "iterator", derive(enum_iterator::Sequence))]
15pub enum Vendor {
16    Aks,
17    Eks,
18    Gke,
19}
20
21impl Vendor {
22    pub fn as_str(&self) -> &'static str {
23        match self {
24            Self::Aks => "AKS",
25            Self::Eks => "EKS",
26            Self::Gke => "GKE",
27        }
28    }
29}
30
31impl fmt::Display for Vendor {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        self.as_str().fmt(f)
34    }
35}
36
37impl FromStr for Vendor {
38    type Err = InvalidVendor;
39
40    fn from_str(text: &str) -> Result<Self, Self::Err> {
41        match text.to_uppercase().as_str() {
42            "AKS" => Ok(Self::Aks),
43            "EKS" => Ok(Self::Eks),
44            "GKE" => Ok(Self::Gke),
45            other => Err(InvalidVendor(other.to_string())),
46        }
47    }
48}
49
50#[cfg(feature = "jsonschema")]
51impl schemars::JsonSchema for Vendor {
52    fn schema_name() -> String {
53        "Vendor".to_string()
54    }
55
56    fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
57        let enum_values = enum_iterator::all::<Self>()
58            .map(|vendor| vendor.to_string())
59            .map(Into::into)
60            .collect();
61        let instance_type = schemars::schema::InstanceType::String.into();
62        let object = schemars::schema::SchemaObject {
63            instance_type: Some(instance_type),
64            enum_values: Some(enum_values),
65            // metadata: todo!(),
66            // format: todo!(),
67            // const_value: todo!(),
68            // subschemas: todo!(),
69            // number: todo!(),
70            // string: todo!(),
71            // array: todo!(),
72            // object: todo!(),
73            // reference: todo!(),
74            // extensions: todo!(),
75            ..schemars::schema::SchemaObject::default()
76        };
77        object.into()
78    }
79}
80
81#[derive(Debug, Error)]
82#[error("Invalid vendor: {0}")]
83pub struct InvalidVendor(String);