Skip to main content

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