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