Skip to main content

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