Skip to main content

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