Skip to main content

nominal_api/conjure/objects/scout/compute/api/
double_constant.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 DoubleConstant {
8    /// Create from a literal floating-point value.
9    Literal(#[derive_with(with = conjure_object::private::DoubleWrapper)] f64),
10    /// Create from a named reference name.
11    Variable(super::VariableName),
12    /// An unknown variant.
13    Unknown(Unknown),
14}
15impl ser::Serialize for DoubleConstant {
16    fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
17    where
18        S: ser::Serializer,
19    {
20        let mut map = s.serialize_map(Some(2))?;
21        match self {
22            DoubleConstant::Literal(value) => {
23                map.serialize_entry(&"type", &"literal")?;
24                map.serialize_entry(&"literal", value)?;
25            }
26            DoubleConstant::Variable(value) => {
27                map.serialize_entry(&"type", &"variable")?;
28                map.serialize_entry(&"variable", value)?;
29            }
30            DoubleConstant::Unknown(value) => {
31                map.serialize_entry(&"type", &value.type_)?;
32                map.serialize_entry(&value.type_, &value.value)?;
33            }
34        }
35        map.end()
36    }
37}
38impl<'de> de::Deserialize<'de> for DoubleConstant {
39    fn deserialize<D>(d: D) -> Result<DoubleConstant, D::Error>
40    where
41        D: de::Deserializer<'de>,
42    {
43        d.deserialize_map(Visitor_)
44    }
45}
46struct Visitor_;
47impl<'de> de::Visitor<'de> for Visitor_ {
48    type Value = DoubleConstant;
49    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
50        fmt.write_str("union DoubleConstant")
51    }
52    fn visit_map<A>(self, mut map: A) -> Result<DoubleConstant, A::Error>
53    where
54        A: de::MapAccess<'de>,
55    {
56        let v = match map.next_key::<UnionField_<Variant_>>()? {
57            Some(UnionField_::Type) => {
58                let variant = map.next_value()?;
59                let key = map.next_key()?;
60                match (variant, key) {
61                    (Variant_::Literal, Some(Variant_::Literal)) => {
62                        let value = map.next_value()?;
63                        DoubleConstant::Literal(value)
64                    }
65                    (Variant_::Variable, Some(Variant_::Variable)) => {
66                        let value = map.next_value()?;
67                        DoubleConstant::Variable(value)
68                    }
69                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
70                        if type_ == b {
71                            let value = map.next_value()?;
72                            DoubleConstant::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_::Literal => {
95                        let value = map.next_value()?;
96                        DoubleConstant::Literal(value)
97                    }
98                    Variant_::Variable => {
99                        let value = map.next_value()?;
100                        DoubleConstant::Variable(value)
101                    }
102                    Variant_::Unknown(type_) => {
103                        let value = map.next_value()?;
104                        DoubleConstant::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    Literal,
135    Variable,
136    Unknown(Box<str>),
137}
138impl Variant_ {
139    fn as_str(&self) -> &'static str {
140        match *self {
141            Variant_::Literal => "literal",
142            Variant_::Variable => "variable",
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            "literal" => Variant_::Literal,
167            "variable" => Variant_::Variable,
168            value => Variant_::Unknown(value.to_string().into_boxed_str()),
169        };
170        Ok(v)
171    }
172}
173///An unknown variant of the DoubleConstant 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}