Skip to main content

nominal_api/conjure/objects/scout/chartdefinition/api/
numeric_value_visualisation.rs

1use conjure_object::serde::{ser, de};
2use conjure_object::serde::ser::SerializeMap as SerializeMap_;
3use conjure_object::private::{UnionField_, UnionTypeField_};
4use std::fmt;
5#[derive(Debug, Clone, conjure_object::private::DeriveWith)]
6#[derive_with(PartialEq, Eq, PartialOrd, Ord, Hash)]
7pub enum NumericValueVisualisation {
8    Raw(super::NumericRawVisualisation),
9    BarGauge(super::NumericBarGaugeVisualisation),
10    /// An unknown variant.
11    Unknown(Unknown),
12}
13impl ser::Serialize for NumericValueVisualisation {
14    fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
15    where
16        S: ser::Serializer,
17    {
18        let mut map = s.serialize_map(Some(2))?;
19        match self {
20            NumericValueVisualisation::Raw(value) => {
21                map.serialize_entry(&"type", &"raw")?;
22                map.serialize_entry(&"raw", value)?;
23            }
24            NumericValueVisualisation::BarGauge(value) => {
25                map.serialize_entry(&"type", &"barGauge")?;
26                map.serialize_entry(&"barGauge", value)?;
27            }
28            NumericValueVisualisation::Unknown(value) => {
29                map.serialize_entry(&"type", &value.type_)?;
30                map.serialize_entry(&value.type_, &value.value)?;
31            }
32        }
33        map.end()
34    }
35}
36impl<'de> de::Deserialize<'de> for NumericValueVisualisation {
37    fn deserialize<D>(d: D) -> Result<NumericValueVisualisation, D::Error>
38    where
39        D: de::Deserializer<'de>,
40    {
41        d.deserialize_map(Visitor_)
42    }
43}
44struct Visitor_;
45impl<'de> de::Visitor<'de> for Visitor_ {
46    type Value = NumericValueVisualisation;
47    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
48        fmt.write_str("union NumericValueVisualisation")
49    }
50    fn visit_map<A>(self, mut map: A) -> Result<NumericValueVisualisation, A::Error>
51    where
52        A: de::MapAccess<'de>,
53    {
54        let v = match map.next_key::<UnionField_<Variant_>>()? {
55            Some(UnionField_::Type) => {
56                let variant = map.next_value()?;
57                let key = map.next_key()?;
58                match (variant, key) {
59                    (Variant_::Raw, Some(Variant_::Raw)) => {
60                        let value = map.next_value()?;
61                        NumericValueVisualisation::Raw(value)
62                    }
63                    (Variant_::BarGauge, Some(Variant_::BarGauge)) => {
64                        let value = map.next_value()?;
65                        NumericValueVisualisation::BarGauge(value)
66                    }
67                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
68                        if type_ == b {
69                            let value = map.next_value()?;
70                            NumericValueVisualisation::Unknown(Unknown { type_, value })
71                        } else {
72                            return Err(
73                                de::Error::invalid_value(de::Unexpected::Str(&type_), &&*b),
74                            )
75                        }
76                    }
77                    (variant, Some(key)) => {
78                        return Err(
79                            de::Error::invalid_value(
80                                de::Unexpected::Str(key.as_str()),
81                                &variant.as_str(),
82                            ),
83                        );
84                    }
85                    (variant, None) => {
86                        return Err(de::Error::missing_field(variant.as_str()));
87                    }
88                }
89            }
90            Some(UnionField_::Value(variant)) => {
91                let value = match &variant {
92                    Variant_::Raw => {
93                        let value = map.next_value()?;
94                        NumericValueVisualisation::Raw(value)
95                    }
96                    Variant_::BarGauge => {
97                        let value = map.next_value()?;
98                        NumericValueVisualisation::BarGauge(value)
99                    }
100                    Variant_::Unknown(type_) => {
101                        let value = map.next_value()?;
102                        NumericValueVisualisation::Unknown(Unknown {
103                            type_: type_.clone(),
104                            value,
105                        })
106                    }
107                };
108                if map.next_key::<UnionTypeField_>()?.is_none() {
109                    return Err(de::Error::missing_field("type"));
110                }
111                let type_variant = map.next_value::<Variant_>()?;
112                if variant != type_variant {
113                    return Err(
114                        de::Error::invalid_value(
115                            de::Unexpected::Str(type_variant.as_str()),
116                            &variant.as_str(),
117                        ),
118                    );
119                }
120                value
121            }
122            None => return Err(de::Error::missing_field("type")),
123        };
124        if map.next_key::<UnionField_<Variant_>>()?.is_some() {
125            return Err(de::Error::invalid_length(3, &"type and value fields"));
126        }
127        Ok(v)
128    }
129}
130#[derive(PartialEq)]
131enum Variant_ {
132    Raw,
133    BarGauge,
134    Unknown(Box<str>),
135}
136impl Variant_ {
137    fn as_str(&self) -> &'static str {
138        match *self {
139            Variant_::Raw => "raw",
140            Variant_::BarGauge => "barGauge",
141            Variant_::Unknown(_) => "unknown variant",
142        }
143    }
144}
145impl<'de> de::Deserialize<'de> for Variant_ {
146    fn deserialize<D>(d: D) -> Result<Variant_, D::Error>
147    where
148        D: de::Deserializer<'de>,
149    {
150        d.deserialize_str(VariantVisitor_)
151    }
152}
153struct VariantVisitor_;
154impl<'de> de::Visitor<'de> for VariantVisitor_ {
155    type Value = Variant_;
156    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
157        fmt.write_str("string")
158    }
159    fn visit_str<E>(self, value: &str) -> Result<Variant_, E>
160    where
161        E: de::Error,
162    {
163        let v = match value {
164            "raw" => Variant_::Raw,
165            "barGauge" => Variant_::BarGauge,
166            value => Variant_::Unknown(value.to_string().into_boxed_str()),
167        };
168        Ok(v)
169    }
170}
171///An unknown variant of the NumericValueVisualisation union.
172#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
173pub struct Unknown {
174    type_: Box<str>,
175    value: conjure_object::Any,
176}
177impl Unknown {
178    /// Returns the unknown variant's type name.
179    #[inline]
180    pub fn type_(&self) -> &str {
181        &self.type_
182    }
183}