Skip to main content

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