k8s_openapi/v1_30/apimachinery/pkg/util/intstr/
int_or_string.rs

1// Generated from definition io.k8s.apimachinery.pkg.util.intstr.IntOrString
2
3/// IntOrString is a type that can hold an int32 or a string.  When used in JSON or YAML marshalling and unmarshalling, it produces or consumes the inner type.  This allows you to have, for example, a JSON field that can accept a name or number.
4#[derive(Clone, Debug, Eq, PartialEq)]
5pub enum IntOrString {
6    Int(i32),
7    String(std::string::String),
8}
9
10impl Default for IntOrString {
11    fn default() -> Self {
12        IntOrString::Int(0)
13    }
14}
15
16impl crate::DeepMerge for IntOrString {
17    fn merge_from(&mut self, other: Self) {
18        *self = other;
19    }
20}
21
22impl<'de> crate::serde::Deserialize<'de> for IntOrString {
23    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: crate::serde::Deserializer<'de> {
24        struct Visitor;
25
26        impl crate::serde::de::Visitor<'_> for Visitor {
27            type Value = IntOrString;
28
29            fn expecting(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
30                formatter.write_str("IntOrString")
31            }
32
33            fn visit_i32<E>(self, v: i32) -> Result<Self::Value, E> where E: crate::serde::de::Error {
34                Ok(IntOrString::Int(v))
35            }
36
37            fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E> where E: crate::serde::de::Error {
38                let v = v.try_into().map_err(|_| crate::serde::de::Error::invalid_value(crate::serde::de::Unexpected::Signed(v), &"a 32-bit integer"))?;
39                Ok(IntOrString::Int(v))
40            }
41
42            fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E> where E: crate::serde::de::Error {
43                let v = v.try_into().map_err(|_| crate::serde::de::Error::invalid_value(crate::serde::de::Unexpected::Unsigned(v), &"a 32-bit integer"))?;
44                Ok(IntOrString::Int(v))
45            }
46
47            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> where E: crate::serde::de::Error {
48                self.visit_string(v.into())
49            }
50
51            fn visit_string<E>(self, v: std::string::String) -> Result<Self::Value, E> where E: crate::serde::de::Error {
52                Ok(IntOrString::String(v))
53            }
54        }
55
56        deserializer.deserialize_any(Visitor)
57    }
58}
59
60impl crate::serde::Serialize for IntOrString {
61    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: crate::serde::Serializer {
62        match self {
63            IntOrString::Int(i) => i.serialize(serializer),
64            IntOrString::String(s) => s.serialize(serializer),
65        }
66    }
67}
68
69#[cfg(feature = "schemars")]
70impl crate::schemars::JsonSchema for IntOrString {
71    fn schema_name() -> std::borrow::Cow<'static, str> {
72        "io.k8s.apimachinery.pkg.util.intstr.IntOrString".into()
73    }
74
75    fn json_schema(__gen: &mut crate::schemars::SchemaGenerator) -> crate::schemars::Schema {
76        crate::schemars::json_schema!({
77            "description": "IntOrString is a type that can hold an int32 or a string.  When used in JSON or YAML marshalling and unmarshalling, it produces or consumes the inner type.  This allows you to have, for example, a JSON field that can accept a name or number.",
78            "x-kubernetes-int-or-string": true,
79        })
80    }
81}