Skip to main content

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