Skip to main content

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