Skip to main content

nominal_api/conjure/objects/scout/chartdefinition/api/
numeric_value_visualisation_v2.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 NumericValueVisualisationV2 {
8    Raw(super::NumericRawVisualisationV2),
9    Bar(super::NumericBarVisualisationV2),
10    /// An unknown variant.
11    Unknown(Unknown),
12}
13impl ser::Serialize for NumericValueVisualisationV2 {
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            NumericValueVisualisationV2::Raw(value) => {
21                map.serialize_entry(&"type", &"raw")?;
22                map.serialize_entry(&"raw", value)?;
23            }
24            NumericValueVisualisationV2::Bar(value) => {
25                map.serialize_entry(&"type", &"bar")?;
26                map.serialize_entry(&"bar", value)?;
27            }
28            NumericValueVisualisationV2::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 NumericValueVisualisationV2 {
37    fn deserialize<D>(d: D) -> Result<NumericValueVisualisationV2, 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 = NumericValueVisualisationV2;
47    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
48        fmt.write_str("union NumericValueVisualisationV2")
49    }
50    fn visit_map<A>(self, mut map: A) -> Result<NumericValueVisualisationV2, 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                        NumericValueVisualisationV2::Raw(value)
62                    }
63                    (Variant_::Bar, Some(Variant_::Bar)) => {
64                        let value = map.next_value()?;
65                        NumericValueVisualisationV2::Bar(value)
66                    }
67                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
68                        if type_ == b {
69                            let value = map.next_value()?;
70                            NumericValueVisualisationV2::Unknown(Unknown {
71                                type_,
72                                value,
73                            })
74                        } else {
75                            return Err(
76                                de::Error::invalid_value(de::Unexpected::Str(&type_), &&*b),
77                            )
78                        }
79                    }
80                    (variant, Some(key)) => {
81                        return Err(
82                            de::Error::invalid_value(
83                                de::Unexpected::Str(key.as_str()),
84                                &variant.as_str(),
85                            ),
86                        );
87                    }
88                    (variant, None) => {
89                        return Err(de::Error::missing_field(variant.as_str()));
90                    }
91                }
92            }
93            Some(UnionField_::Value(variant)) => {
94                let value = match &variant {
95                    Variant_::Raw => {
96                        let value = map.next_value()?;
97                        NumericValueVisualisationV2::Raw(value)
98                    }
99                    Variant_::Bar => {
100                        let value = map.next_value()?;
101                        NumericValueVisualisationV2::Bar(value)
102                    }
103                    Variant_::Unknown(type_) => {
104                        let value = map.next_value()?;
105                        NumericValueVisualisationV2::Unknown(Unknown {
106                            type_: type_.clone(),
107                            value,
108                        })
109                    }
110                };
111                if map.next_key::<UnionTypeField_>()?.is_none() {
112                    return Err(de::Error::missing_field("type"));
113                }
114                let type_variant = map.next_value::<Variant_>()?;
115                if variant != type_variant {
116                    return Err(
117                        de::Error::invalid_value(
118                            de::Unexpected::Str(type_variant.as_str()),
119                            &variant.as_str(),
120                        ),
121                    );
122                }
123                value
124            }
125            None => return Err(de::Error::missing_field("type")),
126        };
127        if map.next_key::<UnionField_<Variant_>>()?.is_some() {
128            return Err(de::Error::invalid_length(3, &"type and value fields"));
129        }
130        Ok(v)
131    }
132}
133#[derive(PartialEq)]
134enum Variant_ {
135    Raw,
136    Bar,
137    Unknown(Box<str>),
138}
139impl Variant_ {
140    fn as_str(&self) -> &'static str {
141        match *self {
142            Variant_::Raw => "raw",
143            Variant_::Bar => "bar",
144            Variant_::Unknown(_) => "unknown variant",
145        }
146    }
147}
148impl<'de> de::Deserialize<'de> for Variant_ {
149    fn deserialize<D>(d: D) -> Result<Variant_, D::Error>
150    where
151        D: de::Deserializer<'de>,
152    {
153        d.deserialize_str(VariantVisitor_)
154    }
155}
156struct VariantVisitor_;
157impl<'de> de::Visitor<'de> for VariantVisitor_ {
158    type Value = Variant_;
159    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
160        fmt.write_str("string")
161    }
162    fn visit_str<E>(self, value: &str) -> Result<Variant_, E>
163    where
164        E: de::Error,
165    {
166        let v = match value {
167            "raw" => Variant_::Raw,
168            "bar" => Variant_::Bar,
169            value => Variant_::Unknown(value.to_string().into_boxed_str()),
170        };
171        Ok(v)
172    }
173}
174///An unknown variant of the NumericValueVisualisationV2 union.
175#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
176pub struct Unknown {
177    type_: Box<str>,
178    value: conjure_object::Any,
179}
180impl Unknown {
181    /// Returns the unknown variant's type name.
182    #[inline]
183    pub fn type_(&self) -> &str {
184        &self.type_
185    }
186}