nominal_api/conjure/objects/scout/chartdefinition/api/
bucket_display_stat.rs1use 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 BucketDisplayStat {
7 Mean(super::NoConfigDisplayStat),
8 Min(super::NoConfigDisplayStat),
9 Max(super::NoConfigDisplayStat),
10 Count(super::NoConfigDisplayStat),
11 Sum(super::NoConfigDisplayStat),
12 Percentile(super::BucketDisplayStatPercentile),
13 Unknown(Unknown),
15}
16impl ser::Serialize for BucketDisplayStat {
17 fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
18 where
19 S: ser::Serializer,
20 {
21 let mut map = s.serialize_map(Some(2))?;
22 match self {
23 BucketDisplayStat::Mean(value) => {
24 map.serialize_entry(&"type", &"mean")?;
25 map.serialize_entry(&"mean", value)?;
26 }
27 BucketDisplayStat::Min(value) => {
28 map.serialize_entry(&"type", &"min")?;
29 map.serialize_entry(&"min", value)?;
30 }
31 BucketDisplayStat::Max(value) => {
32 map.serialize_entry(&"type", &"max")?;
33 map.serialize_entry(&"max", value)?;
34 }
35 BucketDisplayStat::Count(value) => {
36 map.serialize_entry(&"type", &"count")?;
37 map.serialize_entry(&"count", value)?;
38 }
39 BucketDisplayStat::Sum(value) => {
40 map.serialize_entry(&"type", &"sum")?;
41 map.serialize_entry(&"sum", value)?;
42 }
43 BucketDisplayStat::Percentile(value) => {
44 map.serialize_entry(&"type", &"percentile")?;
45 map.serialize_entry(&"percentile", value)?;
46 }
47 BucketDisplayStat::Unknown(value) => {
48 map.serialize_entry(&"type", &value.type_)?;
49 map.serialize_entry(&value.type_, &value.value)?;
50 }
51 }
52 map.end()
53 }
54}
55impl<'de> de::Deserialize<'de> for BucketDisplayStat {
56 fn deserialize<D>(d: D) -> Result<BucketDisplayStat, D::Error>
57 where
58 D: de::Deserializer<'de>,
59 {
60 d.deserialize_map(Visitor_)
61 }
62}
63struct Visitor_;
64impl<'de> de::Visitor<'de> for Visitor_ {
65 type Value = BucketDisplayStat;
66 fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
67 fmt.write_str("union BucketDisplayStat")
68 }
69 fn visit_map<A>(self, mut map: A) -> Result<BucketDisplayStat, A::Error>
70 where
71 A: de::MapAccess<'de>,
72 {
73 let v = match map.next_key::<UnionField_<Variant_>>()? {
74 Some(UnionField_::Type) => {
75 let variant = map.next_value()?;
76 let key = map.next_key()?;
77 match (variant, key) {
78 (Variant_::Mean, Some(Variant_::Mean)) => {
79 let value = map.next_value()?;
80 BucketDisplayStat::Mean(value)
81 }
82 (Variant_::Min, Some(Variant_::Min)) => {
83 let value = map.next_value()?;
84 BucketDisplayStat::Min(value)
85 }
86 (Variant_::Max, Some(Variant_::Max)) => {
87 let value = map.next_value()?;
88 BucketDisplayStat::Max(value)
89 }
90 (Variant_::Count, Some(Variant_::Count)) => {
91 let value = map.next_value()?;
92 BucketDisplayStat::Count(value)
93 }
94 (Variant_::Sum, Some(Variant_::Sum)) => {
95 let value = map.next_value()?;
96 BucketDisplayStat::Sum(value)
97 }
98 (Variant_::Percentile, Some(Variant_::Percentile)) => {
99 let value = map.next_value()?;
100 BucketDisplayStat::Percentile(value)
101 }
102 (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
103 if type_ == b {
104 let value = map.next_value()?;
105 BucketDisplayStat::Unknown(Unknown { type_, value })
106 } else {
107 return Err(
108 de::Error::invalid_value(de::Unexpected::Str(&type_), &&*b),
109 )
110 }
111 }
112 (variant, Some(key)) => {
113 return Err(
114 de::Error::invalid_value(
115 de::Unexpected::Str(key.as_str()),
116 &variant.as_str(),
117 ),
118 );
119 }
120 (variant, None) => {
121 return Err(de::Error::missing_field(variant.as_str()));
122 }
123 }
124 }
125 Some(UnionField_::Value(variant)) => {
126 let value = match &variant {
127 Variant_::Mean => {
128 let value = map.next_value()?;
129 BucketDisplayStat::Mean(value)
130 }
131 Variant_::Min => {
132 let value = map.next_value()?;
133 BucketDisplayStat::Min(value)
134 }
135 Variant_::Max => {
136 let value = map.next_value()?;
137 BucketDisplayStat::Max(value)
138 }
139 Variant_::Count => {
140 let value = map.next_value()?;
141 BucketDisplayStat::Count(value)
142 }
143 Variant_::Sum => {
144 let value = map.next_value()?;
145 BucketDisplayStat::Sum(value)
146 }
147 Variant_::Percentile => {
148 let value = map.next_value()?;
149 BucketDisplayStat::Percentile(value)
150 }
151 Variant_::Unknown(type_) => {
152 let value = map.next_value()?;
153 BucketDisplayStat::Unknown(Unknown {
154 type_: type_.clone(),
155 value,
156 })
157 }
158 };
159 if map.next_key::<UnionTypeField_>()?.is_none() {
160 return Err(de::Error::missing_field("type"));
161 }
162 let type_variant = map.next_value::<Variant_>()?;
163 if variant != type_variant {
164 return Err(
165 de::Error::invalid_value(
166 de::Unexpected::Str(type_variant.as_str()),
167 &variant.as_str(),
168 ),
169 );
170 }
171 value
172 }
173 None => return Err(de::Error::missing_field("type")),
174 };
175 if map.next_key::<UnionField_<Variant_>>()?.is_some() {
176 return Err(de::Error::invalid_length(3, &"type and value fields"));
177 }
178 Ok(v)
179 }
180}
181#[derive(PartialEq)]
182enum Variant_ {
183 Mean,
184 Min,
185 Max,
186 Count,
187 Sum,
188 Percentile,
189 Unknown(Box<str>),
190}
191impl Variant_ {
192 fn as_str(&self) -> &'static str {
193 match *self {
194 Variant_::Mean => "mean",
195 Variant_::Min => "min",
196 Variant_::Max => "max",
197 Variant_::Count => "count",
198 Variant_::Sum => "sum",
199 Variant_::Percentile => "percentile",
200 Variant_::Unknown(_) => "unknown variant",
201 }
202 }
203}
204impl<'de> de::Deserialize<'de> for Variant_ {
205 fn deserialize<D>(d: D) -> Result<Variant_, D::Error>
206 where
207 D: de::Deserializer<'de>,
208 {
209 d.deserialize_str(VariantVisitor_)
210 }
211}
212struct VariantVisitor_;
213impl<'de> de::Visitor<'de> for VariantVisitor_ {
214 type Value = Variant_;
215 fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
216 fmt.write_str("string")
217 }
218 fn visit_str<E>(self, value: &str) -> Result<Variant_, E>
219 where
220 E: de::Error,
221 {
222 let v = match value {
223 "mean" => Variant_::Mean,
224 "min" => Variant_::Min,
225 "max" => Variant_::Max,
226 "count" => Variant_::Count,
227 "sum" => Variant_::Sum,
228 "percentile" => Variant_::Percentile,
229 value => Variant_::Unknown(value.to_string().into_boxed_str()),
230 };
231 Ok(v)
232 }
233}
234#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
236pub struct Unknown {
237 type_: Box<str>,
238 value: conjure_object::Any,
239}
240impl Unknown {
241 #[inline]
243 pub fn type_(&self) -> &str {
244 &self.type_
245 }
246}