Skip to main content

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