Skip to main content

nominal_api/conjure/objects/scout/channel/api/
channel_search_query.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 ChannelSearchQuery {
7    Substring(String),
8    Subsequence(String),
9    Fuzzy(String),
10    /// An unknown variant.
11    Unknown(Unknown),
12}
13impl ser::Serialize for ChannelSearchQuery {
14    fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
15    where
16        S: ser::Serializer,
17    {
18        let mut map = s.serialize_map(Some(2))?;
19        match self {
20            ChannelSearchQuery::Substring(value) => {
21                map.serialize_entry(&"type", &"substring")?;
22                map.serialize_entry(&"substring", value)?;
23            }
24            ChannelSearchQuery::Subsequence(value) => {
25                map.serialize_entry(&"type", &"subsequence")?;
26                map.serialize_entry(&"subsequence", value)?;
27            }
28            ChannelSearchQuery::Fuzzy(value) => {
29                map.serialize_entry(&"type", &"fuzzy")?;
30                map.serialize_entry(&"fuzzy", value)?;
31            }
32            ChannelSearchQuery::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 ChannelSearchQuery {
41    fn deserialize<D>(d: D) -> Result<ChannelSearchQuery, 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 = ChannelSearchQuery;
51    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
52        fmt.write_str("union ChannelSearchQuery")
53    }
54    fn visit_map<A>(self, mut map: A) -> Result<ChannelSearchQuery, 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_::Substring, Some(Variant_::Substring)) => {
64                        let value = map.next_value()?;
65                        ChannelSearchQuery::Substring(value)
66                    }
67                    (Variant_::Subsequence, Some(Variant_::Subsequence)) => {
68                        let value = map.next_value()?;
69                        ChannelSearchQuery::Subsequence(value)
70                    }
71                    (Variant_::Fuzzy, Some(Variant_::Fuzzy)) => {
72                        let value = map.next_value()?;
73                        ChannelSearchQuery::Fuzzy(value)
74                    }
75                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
76                        if type_ == b {
77                            let value = map.next_value()?;
78                            ChannelSearchQuery::Unknown(Unknown { type_, value })
79                        } else {
80                            return Err(
81                                de::Error::invalid_value(de::Unexpected::Str(&type_), &&*b),
82                            )
83                        }
84                    }
85                    (variant, Some(key)) => {
86                        return Err(
87                            de::Error::invalid_value(
88                                de::Unexpected::Str(key.as_str()),
89                                &variant.as_str(),
90                            ),
91                        );
92                    }
93                    (variant, None) => {
94                        return Err(de::Error::missing_field(variant.as_str()));
95                    }
96                }
97            }
98            Some(UnionField_::Value(variant)) => {
99                let value = match &variant {
100                    Variant_::Substring => {
101                        let value = map.next_value()?;
102                        ChannelSearchQuery::Substring(value)
103                    }
104                    Variant_::Subsequence => {
105                        let value = map.next_value()?;
106                        ChannelSearchQuery::Subsequence(value)
107                    }
108                    Variant_::Fuzzy => {
109                        let value = map.next_value()?;
110                        ChannelSearchQuery::Fuzzy(value)
111                    }
112                    Variant_::Unknown(type_) => {
113                        let value = map.next_value()?;
114                        ChannelSearchQuery::Unknown(Unknown {
115                            type_: type_.clone(),
116                            value,
117                        })
118                    }
119                };
120                if map.next_key::<UnionTypeField_>()?.is_none() {
121                    return Err(de::Error::missing_field("type"));
122                }
123                let type_variant = map.next_value::<Variant_>()?;
124                if variant != type_variant {
125                    return Err(
126                        de::Error::invalid_value(
127                            de::Unexpected::Str(type_variant.as_str()),
128                            &variant.as_str(),
129                        ),
130                    );
131                }
132                value
133            }
134            None => return Err(de::Error::missing_field("type")),
135        };
136        if map.next_key::<UnionField_<Variant_>>()?.is_some() {
137            return Err(de::Error::invalid_length(3, &"type and value fields"));
138        }
139        Ok(v)
140    }
141}
142#[derive(PartialEq)]
143enum Variant_ {
144    Substring,
145    Subsequence,
146    Fuzzy,
147    Unknown(Box<str>),
148}
149impl Variant_ {
150    fn as_str(&self) -> &'static str {
151        match *self {
152            Variant_::Substring => "substring",
153            Variant_::Subsequence => "subsequence",
154            Variant_::Fuzzy => "fuzzy",
155            Variant_::Unknown(_) => "unknown variant",
156        }
157    }
158}
159impl<'de> de::Deserialize<'de> for Variant_ {
160    fn deserialize<D>(d: D) -> Result<Variant_, D::Error>
161    where
162        D: de::Deserializer<'de>,
163    {
164        d.deserialize_str(VariantVisitor_)
165    }
166}
167struct VariantVisitor_;
168impl<'de> de::Visitor<'de> for VariantVisitor_ {
169    type Value = Variant_;
170    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
171        fmt.write_str("string")
172    }
173    fn visit_str<E>(self, value: &str) -> Result<Variant_, E>
174    where
175        E: de::Error,
176    {
177        let v = match value {
178            "substring" => Variant_::Substring,
179            "subsequence" => Variant_::Subsequence,
180            "fuzzy" => Variant_::Fuzzy,
181            value => Variant_::Unknown(value.to_string().into_boxed_str()),
182        };
183        Ok(v)
184    }
185}
186///An unknown variant of the ChannelSearchQuery union.
187#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
188pub struct Unknown {
189    type_: Box<str>,
190    value: conjure_object::Any,
191}
192impl Unknown {
193    /// Returns the unknown variant's type name.
194    #[inline]
195    pub fn type_(&self) -> &str {
196        &self.type_
197    }
198}