Skip to main content

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