Skip to main content

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