Skip to main content

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