1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//
// Copyright 2023 Zesty Tech Ltd. All rights reserved.
// Use is subject to license terms.
//

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),
            // metadata: todo!(),
            // format: todo!(),
            // const_value: todo!(),
            // subschemas: todo!(),
            // number: todo!(),
            // string: todo!(),
            // array: todo!(),
            // object: todo!(),
            // reference: todo!(),
            // extensions: todo!(),
            ..schemars::schema::SchemaObject::default()
        };
        object.into()
    }
}

#[derive(Debug, Error)]
#[error("Invalid vendor: {0}")]
pub struct InvalidVendor(String);