Skip to main content

nominal_api/conjure/objects/event/
event_origin.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 EventOrigin {
7    /// This event was created in this workbook
8    Workbook(super::WorkbookEventOrigin),
9    /// This event was created in this template
10    Template(super::TemplateEventOrigin),
11    /// This event was created programmatically via the API
12    Api(super::ApiEventOrigin),
13    /// This event was created automatically from a checklist execution.
14    DataReview(super::DataReviewEventOrigin),
15    /// This event was created automatically from a procedure execution.
16    Procedure(super::ProcedureEventOrigin),
17    /// This event was created automatically from the execution of a streaming checklist.
18    StreamingChecklist(super::StreamingChecklistEventOrigin),
19    /// An unknown variant.
20    Unknown(Unknown),
21}
22impl ser::Serialize for EventOrigin {
23    fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
24    where
25        S: ser::Serializer,
26    {
27        let mut map = s.serialize_map(Some(2))?;
28        match self {
29            EventOrigin::Workbook(value) => {
30                map.serialize_entry(&"type", &"workbook")?;
31                map.serialize_entry(&"workbook", value)?;
32            }
33            EventOrigin::Template(value) => {
34                map.serialize_entry(&"type", &"template")?;
35                map.serialize_entry(&"template", value)?;
36            }
37            EventOrigin::Api(value) => {
38                map.serialize_entry(&"type", &"api")?;
39                map.serialize_entry(&"api", value)?;
40            }
41            EventOrigin::DataReview(value) => {
42                map.serialize_entry(&"type", &"dataReview")?;
43                map.serialize_entry(&"dataReview", value)?;
44            }
45            EventOrigin::Procedure(value) => {
46                map.serialize_entry(&"type", &"procedure")?;
47                map.serialize_entry(&"procedure", value)?;
48            }
49            EventOrigin::StreamingChecklist(value) => {
50                map.serialize_entry(&"type", &"streamingChecklist")?;
51                map.serialize_entry(&"streamingChecklist", value)?;
52            }
53            EventOrigin::Unknown(value) => {
54                map.serialize_entry(&"type", &value.type_)?;
55                map.serialize_entry(&value.type_, &value.value)?;
56            }
57        }
58        map.end()
59    }
60}
61impl<'de> de::Deserialize<'de> for EventOrigin {
62    fn deserialize<D>(d: D) -> Result<EventOrigin, D::Error>
63    where
64        D: de::Deserializer<'de>,
65    {
66        d.deserialize_map(Visitor_)
67    }
68}
69struct Visitor_;
70impl<'de> de::Visitor<'de> for Visitor_ {
71    type Value = EventOrigin;
72    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
73        fmt.write_str("union EventOrigin")
74    }
75    fn visit_map<A>(self, mut map: A) -> Result<EventOrigin, A::Error>
76    where
77        A: de::MapAccess<'de>,
78    {
79        let v = match map.next_key::<UnionField_<Variant_>>()? {
80            Some(UnionField_::Type) => {
81                let variant = map.next_value()?;
82                let key = map.next_key()?;
83                match (variant, key) {
84                    (Variant_::Workbook, Some(Variant_::Workbook)) => {
85                        let value = map.next_value()?;
86                        EventOrigin::Workbook(value)
87                    }
88                    (Variant_::Template, Some(Variant_::Template)) => {
89                        let value = map.next_value()?;
90                        EventOrigin::Template(value)
91                    }
92                    (Variant_::Api, Some(Variant_::Api)) => {
93                        let value = map.next_value()?;
94                        EventOrigin::Api(value)
95                    }
96                    (Variant_::DataReview, Some(Variant_::DataReview)) => {
97                        let value = map.next_value()?;
98                        EventOrigin::DataReview(value)
99                    }
100                    (Variant_::Procedure, Some(Variant_::Procedure)) => {
101                        let value = map.next_value()?;
102                        EventOrigin::Procedure(value)
103                    }
104                    (
105                        Variant_::StreamingChecklist,
106                        Some(Variant_::StreamingChecklist),
107                    ) => {
108                        let value = map.next_value()?;
109                        EventOrigin::StreamingChecklist(value)
110                    }
111                    (Variant_::Unknown(type_), Some(Variant_::Unknown(b))) => {
112                        if type_ == b {
113                            let value = map.next_value()?;
114                            EventOrigin::Unknown(Unknown { type_, value })
115                        } else {
116                            return Err(
117                                de::Error::invalid_value(de::Unexpected::Str(&type_), &&*b),
118                            )
119                        }
120                    }
121                    (variant, Some(key)) => {
122                        return Err(
123                            de::Error::invalid_value(
124                                de::Unexpected::Str(key.as_str()),
125                                &variant.as_str(),
126                            ),
127                        );
128                    }
129                    (variant, None) => {
130                        return Err(de::Error::missing_field(variant.as_str()));
131                    }
132                }
133            }
134            Some(UnionField_::Value(variant)) => {
135                let value = match &variant {
136                    Variant_::Workbook => {
137                        let value = map.next_value()?;
138                        EventOrigin::Workbook(value)
139                    }
140                    Variant_::Template => {
141                        let value = map.next_value()?;
142                        EventOrigin::Template(value)
143                    }
144                    Variant_::Api => {
145                        let value = map.next_value()?;
146                        EventOrigin::Api(value)
147                    }
148                    Variant_::DataReview => {
149                        let value = map.next_value()?;
150                        EventOrigin::DataReview(value)
151                    }
152                    Variant_::Procedure => {
153                        let value = map.next_value()?;
154                        EventOrigin::Procedure(value)
155                    }
156                    Variant_::StreamingChecklist => {
157                        let value = map.next_value()?;
158                        EventOrigin::StreamingChecklist(value)
159                    }
160                    Variant_::Unknown(type_) => {
161                        let value = map.next_value()?;
162                        EventOrigin::Unknown(Unknown {
163                            type_: type_.clone(),
164                            value,
165                        })
166                    }
167                };
168                if map.next_key::<UnionTypeField_>()?.is_none() {
169                    return Err(de::Error::missing_field("type"));
170                }
171                let type_variant = map.next_value::<Variant_>()?;
172                if variant != type_variant {
173                    return Err(
174                        de::Error::invalid_value(
175                            de::Unexpected::Str(type_variant.as_str()),
176                            &variant.as_str(),
177                        ),
178                    );
179                }
180                value
181            }
182            None => return Err(de::Error::missing_field("type")),
183        };
184        if map.next_key::<UnionField_<Variant_>>()?.is_some() {
185            return Err(de::Error::invalid_length(3, &"type and value fields"));
186        }
187        Ok(v)
188    }
189}
190#[derive(PartialEq)]
191enum Variant_ {
192    Workbook,
193    Template,
194    Api,
195    DataReview,
196    Procedure,
197    StreamingChecklist,
198    Unknown(Box<str>),
199}
200impl Variant_ {
201    fn as_str(&self) -> &'static str {
202        match *self {
203            Variant_::Workbook => "workbook",
204            Variant_::Template => "template",
205            Variant_::Api => "api",
206            Variant_::DataReview => "dataReview",
207            Variant_::Procedure => "procedure",
208            Variant_::StreamingChecklist => "streamingChecklist",
209            Variant_::Unknown(_) => "unknown variant",
210        }
211    }
212}
213impl<'de> de::Deserialize<'de> for Variant_ {
214    fn deserialize<D>(d: D) -> Result<Variant_, D::Error>
215    where
216        D: de::Deserializer<'de>,
217    {
218        d.deserialize_str(VariantVisitor_)
219    }
220}
221struct VariantVisitor_;
222impl<'de> de::Visitor<'de> for VariantVisitor_ {
223    type Value = Variant_;
224    fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
225        fmt.write_str("string")
226    }
227    fn visit_str<E>(self, value: &str) -> Result<Variant_, E>
228    where
229        E: de::Error,
230    {
231        let v = match value {
232            "workbook" => Variant_::Workbook,
233            "template" => Variant_::Template,
234            "api" => Variant_::Api,
235            "dataReview" => Variant_::DataReview,
236            "procedure" => Variant_::Procedure,
237            "streamingChecklist" => Variant_::StreamingChecklist,
238            value => Variant_::Unknown(value.to_string().into_boxed_str()),
239        };
240        Ok(v)
241    }
242}
243///An unknown variant of the EventOrigin union.
244#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
245pub struct Unknown {
246    type_: Box<str>,
247    value: conjure_object::Any,
248}
249impl Unknown {
250    /// Returns the unknown variant's type name.
251    #[inline]
252    pub fn type_(&self) -> &str {
253        &self.type_
254    }
255}