Skip to main content

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