Skip to main content

nominal_api_conjure/conjure/objects/scout/compute/resolved/api/
series_node.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 SeriesNode {
8    Raw(super::RawUntypedSeriesNode),
9    Enum(Box<super::EnumSeriesNode>),
10    Numeric(Box<super::NumericSeriesNode>),
11    Log(Box<super::LogSeriesNode>),
12    Array(Box<super::ArraySeriesNode>),
13    Struct(Box<super::StructSeriesNode>),
14    /// An unknown variant.
15    Unknown(Unknown),
16}
17impl ser::Serialize for SeriesNode {
18    fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
19    where
20        S: ser::Serializer,
21    {
22        let mut map = s.serialize_map(Some(2))?;
23        match self {
24            SeriesNode::Raw(value) => {
25                map.serialize_entry(&"type", &"raw")?;
26                map.serialize_entry(&"raw", value)?;
27            }
28            SeriesNode::Enum(value) => {
29                map.serialize_entry(&"type", &"enum")?;
30                map.serialize_entry(&"enum", value)?;
31            }
32            SeriesNode::Numeric(value) => {
33                map.serialize_entry(&"type", &"numeric")?;
34                map.serialize_entry(&"numeric", value)?;
35            }
36            SeriesNode::Log(value) => {
37                map.serialize_entry(&"type", &"log")?;
38                map.serialize_entry(&"log", value)?;
39            }
40            SeriesNode::Array(value) => {
41                map.serialize_entry(&"type", &"array")?;
42                map.serialize_entry(&"array", value)?;
43            }
44            SeriesNode::Struct(value) => {
45                map.serialize_entry(&"type", &"struct")?;
46                map.serialize_entry(&"struct", value)?;
47            }
48            SeriesNode::Unknown(value) => {
49                map.serialize_entry(&"type", &value.type_)?;
50                map.serialize_entry(&value.type_, &value.value)?;
51            }
52        }
53        map.end()
54    }
55}
56impl<'de> de::Deserialize<'de> for SeriesNode {
57    fn deserialize<D>(d: D) -> Result<SeriesNode, D::Error>
58    where
59        D: de::Deserializer<'de>,
60    {
61        d.deserialize_map(Visitor_)
62    }
63}
64struct Visitor_;
65impl<'de> de::Visitor<'de> for Visitor_ {
66    type Value = SeriesNode;
67    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
68        fmt.write_str("union SeriesNode")
69    }
70    fn visit_map<A>(self, mut map: A) -> Result<SeriesNode, A::Error>
71    where
72        A: de::MapAccess<'de>,
73    {
74        let v = match map.next_key::<UnionField_<Variant_>>()? {
75            Some(UnionField_::Type) => {
76                let variant = map.next_value()?;
77                let key = map.next_key()?;
78                match (variant, key) {
79                    (Variant_::Raw, Some(Variant_::Raw)) => {
80                        let value = map.next_value()?;
81                        SeriesNode::Raw(value)
82                    }
83                    (Variant_::Enum, Some(Variant_::Enum)) => {
84                        let value = map.next_value()?;
85                        SeriesNode::Enum(value)
86                    }
87                    (Variant_::Numeric, Some(Variant_::Numeric)) => {
88                        let value = map.next_value()?;
89                        SeriesNode::Numeric(value)
90                    }
91                    (Variant_::Log, Some(Variant_::Log)) => {
92                        let value = map.next_value()?;
93                        SeriesNode::Log(value)
94                    }
95                    (Variant_::Array, Some(Variant_::Array)) => {
96                        let value = map.next_value()?;
97                        SeriesNode::Array(value)
98                    }
99                    (Variant_::Struct, Some(Variant_::Struct)) => {
100                        let value = map.next_value()?;
101                        SeriesNode::Struct(value)
102                    }
103                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
104                        if type_ == b {
105                            let value = map.next_value()?;
106                            SeriesNode::Unknown(Unknown { type_, value })
107                        } else {
108                            return Err(
109                                de::Error::invalid_value(de::Unexpected::Str(&type_), &&*b),
110                            )
111                        }
112                    }
113                    (variant, Some(key)) => {
114                        return Err(
115                            de::Error::invalid_value(
116                                de::Unexpected::Str(key.as_str()),
117                                &variant.as_str(),
118                            ),
119                        );
120                    }
121                    (variant, None) => {
122                        return Err(de::Error::missing_field(variant.as_str()));
123                    }
124                }
125            }
126            Some(UnionField_::Value(variant)) => {
127                let value = match &variant {
128                    Variant_::Raw => {
129                        let value = map.next_value()?;
130                        SeriesNode::Raw(value)
131                    }
132                    Variant_::Enum => {
133                        let value = map.next_value()?;
134                        SeriesNode::Enum(value)
135                    }
136                    Variant_::Numeric => {
137                        let value = map.next_value()?;
138                        SeriesNode::Numeric(value)
139                    }
140                    Variant_::Log => {
141                        let value = map.next_value()?;
142                        SeriesNode::Log(value)
143                    }
144                    Variant_::Array => {
145                        let value = map.next_value()?;
146                        SeriesNode::Array(value)
147                    }
148                    Variant_::Struct => {
149                        let value = map.next_value()?;
150                        SeriesNode::Struct(value)
151                    }
152                    Variant_::Unknown(type_) => {
153                        let value = map.next_value()?;
154                        SeriesNode::Unknown(Unknown {
155                            type_: type_.clone(),
156                            value,
157                        })
158                    }
159                };
160                if map.next_key::<UnionTypeField_>()?.is_none() {
161                    return Err(de::Error::missing_field("type"));
162                }
163                let type_variant = map.next_value::<Variant_>()?;
164                if variant != type_variant {
165                    return Err(
166                        de::Error::invalid_value(
167                            de::Unexpected::Str(type_variant.as_str()),
168                            &variant.as_str(),
169                        ),
170                    );
171                }
172                value
173            }
174            None => return Err(de::Error::missing_field("type")),
175        };
176        if map.next_key::<UnionField_<Variant_>>()?.is_some() {
177            return Err(de::Error::invalid_length(3, &"type and value fields"));
178        }
179        Ok(v)
180    }
181}
182#[derive(PartialEq)]
183enum Variant_ {
184    Raw,
185    Enum,
186    Numeric,
187    Log,
188    Array,
189    Struct,
190    Unknown(Box<str>),
191}
192impl Variant_ {
193    fn as_str(&self) -> &'static str {
194        match *self {
195            Variant_::Raw => "raw",
196            Variant_::Enum => "enum",
197            Variant_::Numeric => "numeric",
198            Variant_::Log => "log",
199            Variant_::Array => "array",
200            Variant_::Struct => "struct",
201            Variant_::Unknown(_) => "unknown variant",
202        }
203    }
204}
205impl<'de> de::Deserialize<'de> for Variant_ {
206    fn deserialize<D>(d: D) -> Result<Variant_, D::Error>
207    where
208        D: de::Deserializer<'de>,
209    {
210        d.deserialize_str(VariantVisitor_)
211    }
212}
213struct VariantVisitor_;
214impl<'de> de::Visitor<'de> for VariantVisitor_ {
215    type Value = Variant_;
216    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
217        fmt.write_str("string")
218    }
219    fn visit_str<E>(self, value: &str) -> Result<Variant_, E>
220    where
221        E: de::Error,
222    {
223        let v = match value {
224            "raw" => Variant_::Raw,
225            "enum" => Variant_::Enum,
226            "numeric" => Variant_::Numeric,
227            "log" => Variant_::Log,
228            "array" => Variant_::Array,
229            "struct" => Variant_::Struct,
230            value => Variant_::Unknown(value.to_string().into_boxed_str()),
231        };
232        Ok(v)
233    }
234}
235///An unknown variant of the SeriesNode union.
236#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
237pub struct Unknown {
238    type_: Box<str>,
239    value: conjure_object::Any,
240}
241impl Unknown {
242    /// Returns the unknown variant's type name.
243    #[inline]
244    pub fn type_(&self) -> &str {
245        &self.type_
246    }
247}