Skip to main content

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