Skip to main content

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