Skip to main content

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