Skip to main content

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