zlink_core/call/
ser.rs

1use serde::{
2    ser::{Error, Impossible, SerializeMap, SerializeStruct},
3    Serialize, Serializer,
4};
5
6use super::Call;
7
8impl<M> Serialize for Call<M>
9where
10    M: Serialize,
11{
12    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
13    where
14        S: Serializer,
15    {
16        let mut map = serializer.serialize_map(Some(4))?;
17
18        let flat_ser = FlatSerializer(&mut map);
19        self.method.serialize(flat_ser)?;
20
21        if self.oneway {
22            map.serialize_entry("oneway", &true)?;
23        }
24        if self.more {
25            map.serialize_entry("more", &true)?;
26        }
27        if self.upgrade {
28            map.serialize_entry("upgrade", &true)?;
29        }
30
31        map.end()
32    }
33}
34
35struct FlatSerializer<'a, M: SerializeMap>(&'a mut M);
36
37impl<'a, M> Serializer for FlatSerializer<'a, M>
38where
39    M: SerializeMap,
40{
41    type Ok = ();
42    type Error = M::Error;
43
44    type SerializeMap = Self;
45    type SerializeStruct = Self;
46    // we only support `map` and `struct`
47    type SerializeSeq = Impossible<Self::Ok, Self::Error>;
48    type SerializeTuple = Impossible<Self::Ok, Self::Error>;
49    type SerializeTupleStruct = Impossible<Self::Ok, Self::Error>;
50    type SerializeTupleVariant = Impossible<Self::Ok, Self::Error>;
51    type SerializeStructVariant = Impossible<Self::Ok, Self::Error>;
52
53    // … you’d do the same for serialize_i32, serialize_str, etc.
54
55    // entry-point for T’s Map or Struct
56    fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
57        Ok(self)
58    }
59
60    fn serialize_struct(
61        self,
62        _name: &'static str,
63        _len: usize,
64    ) -> Result<Self::SerializeStruct, Self::Error> {
65        Ok(self)
66    }
67
68    // Dummy impl for all other serializer methods.
69    fn serialize_bool(self, _: bool) -> Result<Self::Ok, Self::Error> {
70        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
71    }
72    fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
73        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
74    }
75    fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
76        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
77    }
78    fn serialize_some<T>(self, _value: &T) -> Result<Self::Ok, Self::Error>
79    where
80        T: ?Sized + Serialize,
81    {
82        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
83    }
84    fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok, Self::Error> {
85        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
86    }
87    fn serialize_unit_variant(
88        self,
89        _name: &'static str,
90        _variant_index: u32,
91        _variant: &'static str,
92    ) -> Result<Self::Ok, Self::Error> {
93        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
94    }
95    fn serialize_struct_variant(
96        self,
97        _name: &'static str,
98        _variant_index: u32,
99        _variant: &'static str,
100        _len: usize,
101    ) -> Result<Self::SerializeStructVariant, Self::Error> {
102        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
103    }
104    fn serialize_newtype_struct<T>(
105        self,
106        _name: &'static str,
107        _value: &T,
108    ) -> Result<Self::Ok, Self::Error>
109    where
110        T: ?Sized + Serialize,
111    {
112        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
113    }
114    fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
115        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
116    }
117    fn serialize_tuple_struct(
118        self,
119        _name: &'static str,
120        _len: usize,
121    ) -> Result<Self::SerializeTupleStruct, Self::Error> {
122        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
123    }
124    fn serialize_tuple_variant(
125        self,
126        _name: &'static str,
127        _variant_index: u32,
128        _variant: &'static str,
129        _len: usize,
130    ) -> Result<Self::SerializeTupleVariant, Self::Error> {
131        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
132    }
133    fn serialize_newtype_variant<T>(
134        self,
135        _name: &'static str,
136        _variant_index: u32,
137        _variant: &'static str,
138        _value: &T,
139    ) -> Result<Self::Ok, Self::Error>
140    where
141        T: ?Sized + Serialize,
142    {
143        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
144    }
145    fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
146        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
147    }
148    fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok, Self::Error> {
149        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
150    }
151    fn serialize_u8(self, _v: u8) -> Result<Self::Ok, Self::Error> {
152        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
153    }
154    fn serialize_u16(self, _v: u16) -> Result<Self::Ok, Self::Error> {
155        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
156    }
157    fn serialize_u32(self, _v: u32) -> Result<Self::Ok, Self::Error> {
158        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
159    }
160    fn serialize_u64(self, _v: u64) -> Result<Self::Ok, Self::Error> {
161        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
162    }
163    fn serialize_u128(self, _v: u128) -> Result<Self::Ok, Self::Error> {
164        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
165    }
166    fn serialize_i8(self, _v: i8) -> Result<Self::Ok, Self::Error> {
167        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
168    }
169    fn serialize_i16(self, _v: i16) -> Result<Self::Ok, Self::Error> {
170        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
171    }
172    fn serialize_i32(self, _v: i32) -> Result<Self::Ok, Self::Error> {
173        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
174    }
175    fn serialize_i64(self, _v: i64) -> Result<Self::Ok, Self::Error> {
176        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
177    }
178    fn serialize_i128(self, _v: i128) -> Result<Self::Ok, Self::Error> {
179        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
180    }
181    fn serialize_f32(self, _v: f32) -> Result<Self::Ok, Self::Error> {
182        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
183    }
184    fn serialize_f64(self, _v: f64) -> Result<Self::Ok, Self::Error> {
185        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
186    }
187    fn serialize_char(self, _v: char) -> Result<Self::Ok, Self::Error> {
188        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
189    }
190    fn serialize_str(self, _v: &str) -> Result<Self::Ok, Self::Error> {
191        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
192    }
193    fn collect_str<T>(self, _value: &T) -> Result<Self::Ok, Self::Error>
194    where
195        T: ?Sized + core::fmt::Display,
196    {
197        Err(<M as SerializeMap>::Error::custom(ERR_MESSAGE))
198    }
199}
200
201impl<M> SerializeMap for FlatSerializer<'_, M>
202where
203    M: SerializeMap,
204{
205    type Ok = ();
206    type Error = M::Error;
207
208    // now forward keys & values into the real map
209    fn serialize_key<K>(&mut self, key: &K) -> Result<(), Self::Error>
210    where
211        K: ?Sized + Serialize,
212    {
213        self.0.serialize_key(key)
214    }
215
216    fn serialize_value<V>(&mut self, value: &V) -> Result<(), Self::Error>
217    where
218        V: ?Sized + Serialize,
219    {
220        self.0.serialize_value(value)
221    }
222
223    // end of inner map
224    fn end(self) -> Result<Self::Ok, Self::Error> {
225        Ok(())
226    }
227}
228
229impl<M> SerializeStruct for FlatSerializer<'_, M>
230where
231    M: SerializeMap,
232{
233    type Ok = ();
234    type Error = M::Error;
235
236    fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error>
237    where
238        T: ?Sized + Serialize,
239    {
240        self.0.serialize_key(key)?;
241        self.0.serialize_value(value)
242    }
243
244    fn end(self) -> Result<Self::Ok, Self::Error> {
245        Ok(())
246    }
247}
248
249const ERR_MESSAGE: &str =
250    "Must serialize as a map or struct with 2 fields/entries: `method` and `parameters`";