Skip to main content

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