Skip to main content

nominal_api/conjure/objects/scout/compute/api/
variable_value.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 VariableValue {
8    Double(#[derive_with(with = conjure_object::private::DoubleWrapper)] f64),
9    ComputeNode(super::ComputeNodeWithContext),
10    Duration(super::super::super::run::api::Duration),
11    Integer(i32),
12    Channel(Box<super::ChannelSeries>),
13    Derived(Box<super::DerivedSeries>),
14    String(String),
15    StringSet(std::collections::BTreeSet<String>),
16    Timestamp(super::super::super::super::api::Timestamp),
17    /// An unknown variant.
18    Unknown(Unknown),
19}
20impl ser::Serialize for VariableValue {
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            VariableValue::Double(value) => {
28                map.serialize_entry(&"type", &"double")?;
29                map.serialize_entry(&"double", value)?;
30            }
31            VariableValue::ComputeNode(value) => {
32                map.serialize_entry(&"type", &"computeNode")?;
33                map.serialize_entry(&"computeNode", value)?;
34            }
35            VariableValue::Duration(value) => {
36                map.serialize_entry(&"type", &"duration")?;
37                map.serialize_entry(&"duration", value)?;
38            }
39            VariableValue::Integer(value) => {
40                map.serialize_entry(&"type", &"integer")?;
41                map.serialize_entry(&"integer", value)?;
42            }
43            VariableValue::Channel(value) => {
44                map.serialize_entry(&"type", &"channel")?;
45                map.serialize_entry(&"channel", value)?;
46            }
47            VariableValue::Derived(value) => {
48                map.serialize_entry(&"type", &"derived")?;
49                map.serialize_entry(&"derived", value)?;
50            }
51            VariableValue::String(value) => {
52                map.serialize_entry(&"type", &"string")?;
53                map.serialize_entry(&"string", value)?;
54            }
55            VariableValue::StringSet(value) => {
56                map.serialize_entry(&"type", &"stringSet")?;
57                map.serialize_entry(&"stringSet", value)?;
58            }
59            VariableValue::Timestamp(value) => {
60                map.serialize_entry(&"type", &"timestamp")?;
61                map.serialize_entry(&"timestamp", value)?;
62            }
63            VariableValue::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 VariableValue {
72    fn deserialize<D>(d: D) -> Result<VariableValue, 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 = VariableValue;
82    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
83        fmt.write_str("union VariableValue")
84    }
85    fn visit_map<A>(self, mut map: A) -> Result<VariableValue, 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_::Double, Some(Variant_::Double)) => {
95                        let value = map.next_value()?;
96                        VariableValue::Double(value)
97                    }
98                    (Variant_::ComputeNode, Some(Variant_::ComputeNode)) => {
99                        let value = map.next_value()?;
100                        VariableValue::ComputeNode(value)
101                    }
102                    (Variant_::Duration, Some(Variant_::Duration)) => {
103                        let value = map.next_value()?;
104                        VariableValue::Duration(value)
105                    }
106                    (Variant_::Integer, Some(Variant_::Integer)) => {
107                        let value = map.next_value()?;
108                        VariableValue::Integer(value)
109                    }
110                    (Variant_::Channel, Some(Variant_::Channel)) => {
111                        let value = map.next_value()?;
112                        VariableValue::Channel(value)
113                    }
114                    (Variant_::Derived, Some(Variant_::Derived)) => {
115                        let value = map.next_value()?;
116                        VariableValue::Derived(value)
117                    }
118                    (Variant_::String, Some(Variant_::String)) => {
119                        let value = map.next_value()?;
120                        VariableValue::String(value)
121                    }
122                    (Variant_::StringSet, Some(Variant_::StringSet)) => {
123                        let value = map.next_value()?;
124                        VariableValue::StringSet(value)
125                    }
126                    (Variant_::Timestamp, Some(Variant_::Timestamp)) => {
127                        let value = map.next_value()?;
128                        VariableValue::Timestamp(value)
129                    }
130                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
131                        if type_ == b {
132                            let value = map.next_value()?;
133                            VariableValue::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_::Double => {
156                        let value = map.next_value()?;
157                        VariableValue::Double(value)
158                    }
159                    Variant_::ComputeNode => {
160                        let value = map.next_value()?;
161                        VariableValue::ComputeNode(value)
162                    }
163                    Variant_::Duration => {
164                        let value = map.next_value()?;
165                        VariableValue::Duration(value)
166                    }
167                    Variant_::Integer => {
168                        let value = map.next_value()?;
169                        VariableValue::Integer(value)
170                    }
171                    Variant_::Channel => {
172                        let value = map.next_value()?;
173                        VariableValue::Channel(value)
174                    }
175                    Variant_::Derived => {
176                        let value = map.next_value()?;
177                        VariableValue::Derived(value)
178                    }
179                    Variant_::String => {
180                        let value = map.next_value()?;
181                        VariableValue::String(value)
182                    }
183                    Variant_::StringSet => {
184                        let value = map.next_value()?;
185                        VariableValue::StringSet(value)
186                    }
187                    Variant_::Timestamp => {
188                        let value = map.next_value()?;
189                        VariableValue::Timestamp(value)
190                    }
191                    Variant_::Unknown(type_) => {
192                        let value = map.next_value()?;
193                        VariableValue::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    Double,
224    ComputeNode,
225    Duration,
226    Integer,
227    Channel,
228    Derived,
229    String,
230    StringSet,
231    Timestamp,
232    Unknown(Box<str>),
233}
234impl Variant_ {
235    fn as_str(&self) -> &'static str {
236        match *self {
237            Variant_::Double => "double",
238            Variant_::ComputeNode => "computeNode",
239            Variant_::Duration => "duration",
240            Variant_::Integer => "integer",
241            Variant_::Channel => "channel",
242            Variant_::Derived => "derived",
243            Variant_::String => "string",
244            Variant_::StringSet => "stringSet",
245            Variant_::Timestamp => "timestamp",
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            "double" => Variant_::Double,
270            "computeNode" => Variant_::ComputeNode,
271            "duration" => Variant_::Duration,
272            "integer" => Variant_::Integer,
273            "channel" => Variant_::Channel,
274            "derived" => Variant_::Derived,
275            "string" => Variant_::String,
276            "stringSet" => Variant_::StringSet,
277            "timestamp" => Variant_::Timestamp,
278            value => Variant_::Unknown(value.to_string().into_boxed_str()),
279        };
280        Ok(v)
281    }
282}
283///An unknown variant of the VariableValue 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}