Skip to main content

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