Skip to main content

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