Skip to main content

nominal_api/conjure/objects/scout/compute/api/
duration.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 Duration {
7    Reference(super::Reference),
8    Nanoseconds(super::DurationNanoseconds),
9    Milliseconds(super::DurationMilliseconds),
10    Seconds(super::DurationSeconds),
11    Minutes(super::DurationMinutes),
12    Hours(super::DurationHours),
13    Days(super::DurationDays),
14    Add(super::DurationAdd),
15    Subtract(super::DurationSubtract),
16    Negate(super::DurationNegate),
17    /// An unknown variant.
18    Unknown(Unknown),
19}
20impl ser::Serialize for Duration {
21    fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
22    where
23        S: ser::Serializer,
24    {
25        let mut map = s.serialize_map(Some(2))?;
26        match self {
27            Duration::Reference(value) => {
28                map.serialize_entry(&"type", &"reference")?;
29                map.serialize_entry(&"reference", value)?;
30            }
31            Duration::Nanoseconds(value) => {
32                map.serialize_entry(&"type", &"nanoseconds")?;
33                map.serialize_entry(&"nanoseconds", value)?;
34            }
35            Duration::Milliseconds(value) => {
36                map.serialize_entry(&"type", &"milliseconds")?;
37                map.serialize_entry(&"milliseconds", value)?;
38            }
39            Duration::Seconds(value) => {
40                map.serialize_entry(&"type", &"seconds")?;
41                map.serialize_entry(&"seconds", value)?;
42            }
43            Duration::Minutes(value) => {
44                map.serialize_entry(&"type", &"minutes")?;
45                map.serialize_entry(&"minutes", value)?;
46            }
47            Duration::Hours(value) => {
48                map.serialize_entry(&"type", &"hours")?;
49                map.serialize_entry(&"hours", value)?;
50            }
51            Duration::Days(value) => {
52                map.serialize_entry(&"type", &"days")?;
53                map.serialize_entry(&"days", value)?;
54            }
55            Duration::Add(value) => {
56                map.serialize_entry(&"type", &"add")?;
57                map.serialize_entry(&"add", value)?;
58            }
59            Duration::Subtract(value) => {
60                map.serialize_entry(&"type", &"subtract")?;
61                map.serialize_entry(&"subtract", value)?;
62            }
63            Duration::Negate(value) => {
64                map.serialize_entry(&"type", &"negate")?;
65                map.serialize_entry(&"negate", value)?;
66            }
67            Duration::Unknown(value) => {
68                map.serialize_entry(&"type", &value.type_)?;
69                map.serialize_entry(&value.type_, &value.value)?;
70            }
71        }
72        map.end()
73    }
74}
75impl<'de> de::Deserialize<'de> for Duration {
76    fn deserialize<D>(d: D) -> Result<Duration, D::Error>
77    where
78        D: de::Deserializer<'de>,
79    {
80        d.deserialize_map(Visitor_)
81    }
82}
83struct Visitor_;
84impl<'de> de::Visitor<'de> for Visitor_ {
85    type Value = Duration;
86    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
87        fmt.write_str("union Duration")
88    }
89    fn visit_map<A>(self, mut map: A) -> Result<Duration, A::Error>
90    where
91        A: de::MapAccess<'de>,
92    {
93        let v = match map.next_key::<UnionField_<Variant_>>()? {
94            Some(UnionField_::Type) => {
95                let variant = map.next_value()?;
96                let key = map.next_key()?;
97                match (variant, key) {
98                    (Variant_::Reference, Some(Variant_::Reference)) => {
99                        let value = map.next_value()?;
100                        Duration::Reference(value)
101                    }
102                    (Variant_::Nanoseconds, Some(Variant_::Nanoseconds)) => {
103                        let value = map.next_value()?;
104                        Duration::Nanoseconds(value)
105                    }
106                    (Variant_::Milliseconds, Some(Variant_::Milliseconds)) => {
107                        let value = map.next_value()?;
108                        Duration::Milliseconds(value)
109                    }
110                    (Variant_::Seconds, Some(Variant_::Seconds)) => {
111                        let value = map.next_value()?;
112                        Duration::Seconds(value)
113                    }
114                    (Variant_::Minutes, Some(Variant_::Minutes)) => {
115                        let value = map.next_value()?;
116                        Duration::Minutes(value)
117                    }
118                    (Variant_::Hours, Some(Variant_::Hours)) => {
119                        let value = map.next_value()?;
120                        Duration::Hours(value)
121                    }
122                    (Variant_::Days, Some(Variant_::Days)) => {
123                        let value = map.next_value()?;
124                        Duration::Days(value)
125                    }
126                    (Variant_::Add, Some(Variant_::Add)) => {
127                        let value = map.next_value()?;
128                        Duration::Add(value)
129                    }
130                    (Variant_::Subtract, Some(Variant_::Subtract)) => {
131                        let value = map.next_value()?;
132                        Duration::Subtract(value)
133                    }
134                    (Variant_::Negate, Some(Variant_::Negate)) => {
135                        let value = map.next_value()?;
136                        Duration::Negate(value)
137                    }
138                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
139                        if type_ == b {
140                            let value = map.next_value()?;
141                            Duration::Unknown(Unknown { type_, value })
142                        } else {
143                            return Err(
144                                de::Error::invalid_value(de::Unexpected::Str(&type_), &&*b),
145                            )
146                        }
147                    }
148                    (variant, Some(key)) => {
149                        return Err(
150                            de::Error::invalid_value(
151                                de::Unexpected::Str(key.as_str()),
152                                &variant.as_str(),
153                            ),
154                        );
155                    }
156                    (variant, None) => {
157                        return Err(de::Error::missing_field(variant.as_str()));
158                    }
159                }
160            }
161            Some(UnionField_::Value(variant)) => {
162                let value = match &variant {
163                    Variant_::Reference => {
164                        let value = map.next_value()?;
165                        Duration::Reference(value)
166                    }
167                    Variant_::Nanoseconds => {
168                        let value = map.next_value()?;
169                        Duration::Nanoseconds(value)
170                    }
171                    Variant_::Milliseconds => {
172                        let value = map.next_value()?;
173                        Duration::Milliseconds(value)
174                    }
175                    Variant_::Seconds => {
176                        let value = map.next_value()?;
177                        Duration::Seconds(value)
178                    }
179                    Variant_::Minutes => {
180                        let value = map.next_value()?;
181                        Duration::Minutes(value)
182                    }
183                    Variant_::Hours => {
184                        let value = map.next_value()?;
185                        Duration::Hours(value)
186                    }
187                    Variant_::Days => {
188                        let value = map.next_value()?;
189                        Duration::Days(value)
190                    }
191                    Variant_::Add => {
192                        let value = map.next_value()?;
193                        Duration::Add(value)
194                    }
195                    Variant_::Subtract => {
196                        let value = map.next_value()?;
197                        Duration::Subtract(value)
198                    }
199                    Variant_::Negate => {
200                        let value = map.next_value()?;
201                        Duration::Negate(value)
202                    }
203                    Variant_::Unknown(type_) => {
204                        let value = map.next_value()?;
205                        Duration::Unknown(Unknown {
206                            type_: type_.clone(),
207                            value,
208                        })
209                    }
210                };
211                if map.next_key::<UnionTypeField_>()?.is_none() {
212                    return Err(de::Error::missing_field("type"));
213                }
214                let type_variant = map.next_value::<Variant_>()?;
215                if variant != type_variant {
216                    return Err(
217                        de::Error::invalid_value(
218                            de::Unexpected::Str(type_variant.as_str()),
219                            &variant.as_str(),
220                        ),
221                    );
222                }
223                value
224            }
225            None => return Err(de::Error::missing_field("type")),
226        };
227        if map.next_key::<UnionField_<Variant_>>()?.is_some() {
228            return Err(de::Error::invalid_length(3, &"type and value fields"));
229        }
230        Ok(v)
231    }
232}
233#[derive(PartialEq)]
234enum Variant_ {
235    Reference,
236    Nanoseconds,
237    Milliseconds,
238    Seconds,
239    Minutes,
240    Hours,
241    Days,
242    Add,
243    Subtract,
244    Negate,
245    Unknown(Box<str>),
246}
247impl Variant_ {
248    fn as_str(&self) -> &'static str {
249        match *self {
250            Variant_::Reference => "reference",
251            Variant_::Nanoseconds => "nanoseconds",
252            Variant_::Milliseconds => "milliseconds",
253            Variant_::Seconds => "seconds",
254            Variant_::Minutes => "minutes",
255            Variant_::Hours => "hours",
256            Variant_::Days => "days",
257            Variant_::Add => "add",
258            Variant_::Subtract => "subtract",
259            Variant_::Negate => "negate",
260            Variant_::Unknown(_) => "unknown variant",
261        }
262    }
263}
264impl<'de> de::Deserialize<'de> for Variant_ {
265    fn deserialize<D>(d: D) -> Result<Variant_, D::Error>
266    where
267        D: de::Deserializer<'de>,
268    {
269        d.deserialize_str(VariantVisitor_)
270    }
271}
272struct VariantVisitor_;
273impl<'de> de::Visitor<'de> for VariantVisitor_ {
274    type Value = Variant_;
275    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
276        fmt.write_str("string")
277    }
278    fn visit_str<E>(self, value: &str) -> Result<Variant_, E>
279    where
280        E: de::Error,
281    {
282        let v = match value {
283            "reference" => Variant_::Reference,
284            "nanoseconds" => Variant_::Nanoseconds,
285            "milliseconds" => Variant_::Milliseconds,
286            "seconds" => Variant_::Seconds,
287            "minutes" => Variant_::Minutes,
288            "hours" => Variant_::Hours,
289            "days" => Variant_::Days,
290            "add" => Variant_::Add,
291            "subtract" => Variant_::Subtract,
292            "negate" => Variant_::Negate,
293            value => Variant_::Unknown(value.to_string().into_boxed_str()),
294        };
295        Ok(v)
296    }
297}
298///An unknown variant of the Duration union.
299#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
300pub struct Unknown {
301    type_: Box<str>,
302    value: conjure_object::Any,
303}
304impl Unknown {
305    /// Returns the unknown variant's type name.
306    #[inline]
307    pub fn type_(&self) -> &str {
308        &self.type_
309    }
310}