Skip to main content

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