Skip to main content

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