Skip to main content

nominal_api/conjure/objects/scout/compute/api/
numeric_histogram_bucket_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 NumericHistogramBucketStrategy {
8    /// The number of buckets to use in the histogram. Width is automatically calculated from the range
9    /// of the input series, with the lower and upper bounds of the histogram being multiples of the width.
10    BucketCount(Box<super::IntegerConstant>),
11    BucketWidthAndOffset(super::NumericHistogramBucketWidthAndOffset),
12    /// An unknown variant.
13    Unknown(Unknown),
14}
15impl ser::Serialize for NumericHistogramBucketStrategy {
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            NumericHistogramBucketStrategy::BucketCount(value) => {
23                map.serialize_entry(&"type", &"bucketCount")?;
24                map.serialize_entry(&"bucketCount", value)?;
25            }
26            NumericHistogramBucketStrategy::BucketWidthAndOffset(value) => {
27                map.serialize_entry(&"type", &"bucketWidthAndOffset")?;
28                map.serialize_entry(&"bucketWidthAndOffset", value)?;
29            }
30            NumericHistogramBucketStrategy::Unknown(value) => {
31                map.serialize_entry(&"type", &value.type_)?;
32                map.serialize_entry(&value.type_, &value.value)?;
33            }
34        }
35        map.end()
36    }
37}
38impl<'de> de::Deserialize<'de> for NumericHistogramBucketStrategy {
39    fn deserialize<D>(d: D) -> Result<NumericHistogramBucketStrategy, D::Error>
40    where
41        D: de::Deserializer<'de>,
42    {
43        d.deserialize_map(Visitor_)
44    }
45}
46struct Visitor_;
47impl<'de> de::Visitor<'de> for Visitor_ {
48    type Value = NumericHistogramBucketStrategy;
49    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
50        fmt.write_str("union NumericHistogramBucketStrategy")
51    }
52    fn visit_map<A>(self, mut map: A) -> Result<NumericHistogramBucketStrategy, A::Error>
53    where
54        A: de::MapAccess<'de>,
55    {
56        let v = match map.next_key::<UnionField_<Variant_>>()? {
57            Some(UnionField_::Type) => {
58                let variant = map.next_value()?;
59                let key = map.next_key()?;
60                match (variant, key) {
61                    (Variant_::BucketCount, Some(Variant_::BucketCount)) => {
62                        let value = map.next_value()?;
63                        NumericHistogramBucketStrategy::BucketCount(value)
64                    }
65                    (
66                        Variant_::BucketWidthAndOffset,
67                        Some(Variant_::BucketWidthAndOffset),
68                    ) => {
69                        let value = map.next_value()?;
70                        NumericHistogramBucketStrategy::BucketWidthAndOffset(value)
71                    }
72                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
73                        if type_ == b {
74                            let value = map.next_value()?;
75                            NumericHistogramBucketStrategy::Unknown(Unknown {
76                                type_,
77                                value,
78                            })
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_::BucketCount => {
101                        let value = map.next_value()?;
102                        NumericHistogramBucketStrategy::BucketCount(value)
103                    }
104                    Variant_::BucketWidthAndOffset => {
105                        let value = map.next_value()?;
106                        NumericHistogramBucketStrategy::BucketWidthAndOffset(value)
107                    }
108                    Variant_::Unknown(type_) => {
109                        let value = map.next_value()?;
110                        NumericHistogramBucketStrategy::Unknown(Unknown {
111                            type_: type_.clone(),
112                            value,
113                        })
114                    }
115                };
116                if map.next_key::<UnionTypeField_>()?.is_none() {
117                    return Err(de::Error::missing_field("type"));
118                }
119                let type_variant = map.next_value::<Variant_>()?;
120                if variant != type_variant {
121                    return Err(
122                        de::Error::invalid_value(
123                            de::Unexpected::Str(type_variant.as_str()),
124                            &variant.as_str(),
125                        ),
126                    );
127                }
128                value
129            }
130            None => return Err(de::Error::missing_field("type")),
131        };
132        if map.next_key::<UnionField_<Variant_>>()?.is_some() {
133            return Err(de::Error::invalid_length(3, &"type and value fields"));
134        }
135        Ok(v)
136    }
137}
138#[derive(PartialEq)]
139enum Variant_ {
140    BucketCount,
141    BucketWidthAndOffset,
142    Unknown(Box<str>),
143}
144impl Variant_ {
145    fn as_str(&self) -> &'static str {
146        match *self {
147            Variant_::BucketCount => "bucketCount",
148            Variant_::BucketWidthAndOffset => "bucketWidthAndOffset",
149            Variant_::Unknown(_) => "unknown variant",
150        }
151    }
152}
153impl<'de> de::Deserialize<'de> for Variant_ {
154    fn deserialize<D>(d: D) -> Result<Variant_, D::Error>
155    where
156        D: de::Deserializer<'de>,
157    {
158        d.deserialize_str(VariantVisitor_)
159    }
160}
161struct VariantVisitor_;
162impl<'de> de::Visitor<'de> for VariantVisitor_ {
163    type Value = Variant_;
164    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
165        fmt.write_str("string")
166    }
167    fn visit_str<E>(self, value: &str) -> Result<Variant_, E>
168    where
169        E: de::Error,
170    {
171        let v = match value {
172            "bucketCount" => Variant_::BucketCount,
173            "bucketWidthAndOffset" => Variant_::BucketWidthAndOffset,
174            value => Variant_::Unknown(value.to_string().into_boxed_str()),
175        };
176        Ok(v)
177    }
178}
179///An unknown variant of the NumericHistogramBucketStrategy union.
180#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
181pub struct Unknown {
182    type_: Box<str>,
183    value: conjure_object::Any,
184}
185impl Unknown {
186    /// Returns the unknown variant's type name.
187    #[inline]
188    pub fn type_(&self) -> &str {
189        &self.type_
190    }
191}