serde_bytes_repr/
serializer.rs

1use crate::{ByteFmtSerializer, ByteFormat};
2use serde::{
3    ser::{self, Error},
4    serde_if_integer128, Serialize, Serializer,
5};
6use std::fmt::Display;
7
8impl<S: Serializer> Serializer for ByteFmtSerializer<S> {
9    type Ok = S::Ok;
10    type Error = S::Error;
11
12    type SerializeSeq = SerializeSeq<S::SerializeSeq>;
13    type SerializeTuple = SerializeTuple<S::SerializeTuple>;
14    type SerializeTupleStruct = SerializeTupleStruct<S::SerializeTupleStruct>;
15    type SerializeTupleVariant = SerializeTupleVariant<S::SerializeTupleVariant>;
16    type SerializeMap = SerializeMap<S::SerializeMap>;
17    type SerializeStruct = SerializeStruct<S::SerializeStruct>;
18    type SerializeStructVariant = SerializeStructVariant<S::SerializeStructVariant>;
19
20    fn serialize_bool(self, v: bool) -> Result<Self::Ok, Self::Error> {
21        S::serialize_bool(self.inner, v)
22    }
23
24    fn serialize_i8(self, v: i8) -> Result<Self::Ok, Self::Error> {
25        S::serialize_i8(self.inner, v)
26    }
27
28    fn serialize_i16(self, v: i16) -> Result<Self::Ok, Self::Error> {
29        S::serialize_i16(self.inner, v)
30    }
31
32    fn serialize_i32(self, v: i32) -> Result<Self::Ok, Self::Error> {
33        S::serialize_i32(self.inner, v)
34    }
35
36    fn serialize_i64(self, v: i64) -> Result<Self::Ok, Self::Error> {
37        S::serialize_i64(self.inner, v)
38    }
39
40    serde_if_integer128! {
41        fn serialize_i128(self, v: i128) -> Result<Self::Ok, Self::Error> {
42            let _ = v;
43            Err(Error::custom("i128 is not supported"))
44        }
45    }
46
47    fn serialize_u8(self, v: u8) -> Result<Self::Ok, Self::Error> {
48        S::serialize_u8(self.inner, v)
49    }
50
51    fn serialize_u16(self, v: u16) -> Result<Self::Ok, Self::Error> {
52        S::serialize_u16(self.inner, v)
53    }
54
55    fn serialize_u32(self, v: u32) -> Result<Self::Ok, Self::Error> {
56        S::serialize_u32(self.inner, v)
57    }
58
59    fn serialize_u64(self, v: u64) -> Result<Self::Ok, Self::Error> {
60        S::serialize_u64(self.inner, v)
61    }
62
63    serde_if_integer128! {
64        fn serialize_u128(self, v: u128) -> Result<Self::Ok, Self::Error> {
65            let _ = v;
66            Err(Error::custom("u128 is not supported"))
67        }
68    }
69
70    fn serialize_f32(self, v: f32) -> Result<Self::Ok, Self::Error> {
71        S::serialize_f32(self.inner, v)
72    }
73
74    fn serialize_f64(self, v: f64) -> Result<Self::Ok, Self::Error> {
75        S::serialize_f64(self.inner, v)
76    }
77
78    fn serialize_char(self, v: char) -> Result<Self::Ok, Self::Error> {
79        S::serialize_char(self.inner, v)
80    }
81
82    fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> {
83        S::serialize_str(self.inner, v)
84    }
85
86    fn serialize_bytes(self, v: &[u8]) -> Result<Self::Ok, Self::Error> {
87        let encoded = self.encode(v);
88        S::serialize_str(self.inner, &encoded)
89    }
90
91    fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
92        S::serialize_none(self.inner)
93    }
94
95    fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
96    where
97        T: Serialize,
98    {
99        value.serialize(self)
100    }
101
102    fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
103        S::serialize_unit(self.inner)
104    }
105
106    fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> {
107        S::serialize_unit_struct(self.inner, name)
108    }
109
110    fn serialize_unit_variant(
111        self,
112        name: &'static str,
113        variant_index: u32,
114        variant: &'static str,
115    ) -> Result<Self::Ok, Self::Error> {
116        S::serialize_unit_variant(self.inner, name, variant_index, variant)
117    }
118
119    fn serialize_newtype_struct<T: ?Sized>(
120        self,
121        name: &'static str,
122        value: &T,
123    ) -> Result<Self::Ok, Self::Error>
124    where
125        T: Serialize,
126    {
127        S::serialize_newtype_struct(
128            self.inner,
129            name,
130            &BytesSerialize::new(value, self.encode_kind.clone()),
131        )
132    }
133
134    fn serialize_newtype_variant<T: ?Sized>(
135        self,
136        name: &'static str,
137        variant_index: u32,
138        variant: &'static str,
139        value: &T,
140    ) -> Result<Self::Ok, Self::Error>
141    where
142        T: Serialize,
143    {
144        S::serialize_newtype_variant(
145            self.inner,
146            name,
147            variant_index,
148            variant,
149            &BytesSerialize::new(value, self.encode_kind.clone()),
150        )
151    }
152
153    fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
154        let kind = self.encode_kind;
155        S::serialize_seq(self.inner, len).map(|ser| SerializeSeq::new(ser, kind))
156    }
157
158    fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, Self::Error> {
159        let kind = self.encode_kind;
160        S::serialize_tuple(self.inner, len).map(|ser| SerializeTuple::new(ser, kind))
161    }
162
163    fn serialize_tuple_struct(
164        self,
165        name: &'static str,
166        len: usize,
167    ) -> Result<Self::SerializeTupleStruct, Self::Error> {
168        let kind = self.encode_kind;
169        S::serialize_tuple_struct(self.inner, name, len)
170            .map(|ser| SerializeTupleStruct::new(ser, kind))
171    }
172
173    fn serialize_tuple_variant(
174        self,
175        name: &'static str,
176        variant_index: u32,
177        variant: &'static str,
178        len: usize,
179    ) -> Result<Self::SerializeTupleVariant, Self::Error> {
180        let kind = self.encode_kind;
181        S::serialize_tuple_variant(self.inner, name, variant_index, variant, len)
182            .map(|ser| SerializeTupleVariant::new(ser, kind))
183    }
184
185    fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
186        let kind = self.encode_kind;
187        S::serialize_map(self.inner, len).map(|ser| SerializeMap::new(ser, kind))
188    }
189
190    fn serialize_struct(
191        self,
192        name: &'static str,
193        len: usize,
194    ) -> Result<Self::SerializeStruct, Self::Error> {
195        let encode_kind = self.encode_kind;
196        S::serialize_struct(self.inner, name, len).map(|ser| SerializeStruct::new(ser, encode_kind))
197    }
198
199    fn serialize_struct_variant(
200        self,
201        name: &'static str,
202        variant_index: u32,
203        variant: &'static str,
204        len: usize,
205    ) -> Result<Self::SerializeStructVariant, Self::Error> {
206        let kind = self.encode_kind;
207        S::serialize_struct_variant(self.inner, name, variant_index, variant, len)
208            .map(|ser| SerializeStructVariant::new(ser, kind))
209    }
210
211    fn collect_seq<I>(self, iter: I) -> Result<Self::Ok, Self::Error>
212    where
213        I: IntoIterator,
214        <I as IntoIterator>::Item: Serialize,
215    {
216        let kind = self.encode_kind;
217        let iter = iter
218            .into_iter()
219            .map(|item| BytesSerializeSized::new(item, kind.clone()));
220        self.inner.collect_seq(iter)
221    }
222
223    fn collect_map<K, V, I>(self, iter: I) -> Result<Self::Ok, Self::Error>
224    where
225        K: Serialize,
226        V: Serialize,
227        I: IntoIterator<Item = (K, V)>,
228    {
229        let kind = self.encode_kind;
230        let iter = iter.into_iter().map(|(k, v)| {
231            (
232                BytesSerializeSized::new(k, kind.clone()),
233                BytesSerializeSized::new(v, kind.clone()),
234            )
235        });
236        self.inner.collect_map(iter)
237    }
238
239    #[cfg(any(feature = "std", feature = "alloc"))]
240    fn collect_str<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
241    where
242        T: Display,
243    {
244        self.inner.serialize_str(&value.to_string())
245    }
246
247    #[cfg(not(any(feature = "std", feature = "alloc")))]
248    fn collect_str<T: ?Sized>(self, value: &T) -> Result<Self::Ok, Self::Error>
249    where
250        T: Display,
251    {
252        S::collect_str(self.inner, value)
253    }
254
255    fn is_human_readable(&self) -> bool {
256        self.inner.is_human_readable()
257    }
258}
259
260pub struct BytesSerialize<'a, T: ?Sized> {
261    value: &'a T,
262    fmt: ByteFormat,
263}
264
265impl<'a, T: ?Sized> BytesSerialize<'a, T> {
266    fn new(value: &'a T, fmt: ByteFormat) -> Self {
267        BytesSerialize { value, fmt }
268    }
269}
270
271impl<'a, T: ?Sized> ser::Serialize for BytesSerialize<'a, T>
272where
273    T: ser::Serialize,
274{
275    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
276    where
277        S: Serializer,
278    {
279        ser::Serialize::serialize(
280            self.value,
281            ByteFmtSerializer {
282                inner: serializer,
283                encode_kind: self.fmt.clone(),
284            },
285        )
286    }
287}
288
289struct BytesSerializeSized<T> {
290    value: T,
291    fmt: ByteFormat,
292}
293
294impl<T> BytesSerializeSized<T> {
295    fn new(value: T, fmt: ByteFormat) -> Self {
296        BytesSerializeSized { value, fmt }
297    }
298}
299
300impl<T> ser::Serialize for BytesSerializeSized<T>
301where
302    T: ser::Serialize,
303{
304    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
305    where
306        S: ser::Serializer,
307    {
308        ser::Serialize::serialize(
309            &self.value,
310            ByteFmtSerializer {
311                inner: serializer,
312                encode_kind: self.fmt.clone(),
313            },
314        )
315    }
316}
317
318pub struct SerializeSeq<S> {
319    ser: S,
320    fmt: ByteFormat,
321}
322
323impl<S> SerializeSeq<S> {
324    fn new(ser: S, fmt: ByteFormat) -> Self {
325        SerializeSeq { ser, fmt }
326    }
327}
328
329impl<S> ser::SerializeSeq for SerializeSeq<S>
330where
331    S: ser::SerializeSeq,
332{
333    type Ok = S::Ok;
334    type Error = S::Error;
335
336    fn serialize_element<T>(&mut self, value: &T) -> Result<(), Self::Error>
337    where
338        T: ?Sized + ser::Serialize,
339    {
340        self.ser
341            .serialize_element(&BytesSerialize::new(value, self.fmt.clone()))
342    }
343
344    fn end(self) -> Result<Self::Ok, Self::Error> {
345        self.ser.end()
346    }
347}
348
349pub struct SerializeTuple<S> {
350    ser: S,
351    fmt: ByteFormat,
352}
353
354impl<S> SerializeTuple<S> {
355    fn new(serialize_tuple: S, fmt: ByteFormat) -> Self {
356        SerializeTuple {
357            ser: serialize_tuple,
358            fmt,
359        }
360    }
361}
362
363impl<S> ser::SerializeTuple for SerializeTuple<S>
364where
365    S: ser::SerializeTuple,
366{
367    type Ok = S::Ok;
368    type Error = S::Error;
369
370    fn serialize_element<T>(&mut self, value: &T) -> Result<(), Self::Error>
371    where
372        T: ?Sized + ser::Serialize,
373    {
374        self.ser
375            .serialize_element(&BytesSerialize::new(value, self.fmt.clone()))
376    }
377
378    fn end(self) -> Result<Self::Ok, Self::Error> {
379        self.ser.end()
380    }
381}
382
383pub struct SerializeTupleStruct<S> {
384    ser: S,
385    fmt: ByteFormat,
386}
387
388impl<S> SerializeTupleStruct<S> {
389    fn new(serialize_tuple_struct: S, fmt: ByteFormat) -> Self {
390        SerializeTupleStruct {
391            ser: serialize_tuple_struct,
392            fmt,
393        }
394    }
395}
396
397impl<S> ser::SerializeTupleStruct for SerializeTupleStruct<S>
398where
399    S: ser::SerializeTupleStruct,
400{
401    type Ok = S::Ok;
402    type Error = S::Error;
403
404    fn serialize_field<T>(&mut self, value: &T) -> Result<(), Self::Error>
405    where
406        T: ?Sized + ser::Serialize,
407    {
408        self.ser
409            .serialize_field(&BytesSerialize::new(value, self.fmt.clone()))
410    }
411
412    fn end(self) -> Result<Self::Ok, Self::Error> {
413        self.ser.end()
414    }
415}
416
417pub struct SerializeTupleVariant<S> {
418    ser: S,
419    kind: ByteFormat,
420}
421
422impl<S> SerializeTupleVariant<S> {
423    fn new(serialize_tuple_variant: S, kind: ByteFormat) -> Self {
424        SerializeTupleVariant {
425            ser: serialize_tuple_variant,
426            kind,
427        }
428    }
429}
430
431impl<S> ser::SerializeTupleVariant for SerializeTupleVariant<S>
432where
433    S: ser::SerializeTupleVariant,
434{
435    type Ok = S::Ok;
436    type Error = S::Error;
437
438    fn serialize_field<T>(&mut self, value: &T) -> Result<(), Self::Error>
439    where
440        T: ?Sized + ser::Serialize,
441    {
442        self.ser
443            .serialize_field(&BytesSerialize::new(value, self.kind.clone()))
444    }
445
446    fn end(self) -> Result<Self::Ok, Self::Error> {
447        self.ser.end()
448    }
449}
450
451pub struct SerializeMap<S> {
452    ser: S,
453    fmt: ByteFormat,
454}
455
456impl<S> SerializeMap<S> {
457    fn new(serialize_map: S, fmt: ByteFormat) -> Self {
458        SerializeMap {
459            ser: serialize_map,
460            fmt,
461        }
462    }
463}
464
465impl<S> ser::SerializeMap for SerializeMap<S>
466where
467    S: ser::SerializeMap,
468{
469    type Ok = S::Ok;
470    type Error = S::Error;
471
472    fn serialize_key<T>(&mut self, key: &T) -> Result<(), Self::Error>
473    where
474        T: ?Sized + ser::Serialize,
475    {
476        self.ser
477            .serialize_key(&BytesSerialize::new(key, self.fmt.clone()))
478    }
479
480    fn serialize_value<T>(&mut self, value: &T) -> Result<(), Self::Error>
481    where
482        T: ?Sized + ser::Serialize,
483    {
484        self.ser
485            .serialize_value(&BytesSerialize::new(value, self.fmt.clone()))
486    }
487
488    fn end(self) -> Result<Self::Ok, Self::Error> {
489        self.ser.end()
490    }
491
492    fn serialize_entry<K, V>(&mut self, key: &K, value: &V) -> Result<(), Self::Error>
493    where
494        K: ?Sized + ser::Serialize,
495        V: ?Sized + ser::Serialize,
496    {
497        self.ser.serialize_entry(
498            &BytesSerialize::new(key, self.fmt.clone()),
499            &BytesSerialize::new(value, self.fmt.clone()),
500        )
501    }
502}
503
504pub struct SerializeStruct<S> {
505    ser: S,
506    fmt: ByteFormat,
507}
508
509impl<S> SerializeStruct<S> {
510    fn new(ser: S, fmt: ByteFormat) -> Self {
511        SerializeStruct { ser, fmt }
512    }
513}
514
515impl<S: ser::SerializeStruct> ser::SerializeStruct for SerializeStruct<S> {
516    type Ok = S::Ok;
517    type Error = S::Error;
518
519    fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error>
520    where
521        T: ?Sized + ser::Serialize,
522    {
523        self.ser
524            .serialize_field(key, &BytesSerialize::new(value, self.fmt.clone()))
525    }
526
527    fn end(self) -> Result<Self::Ok, Self::Error> {
528        self.ser.end()
529    }
530
531    fn skip_field(&mut self, key: &'static str) -> Result<(), Self::Error> {
532        self.ser.skip_field(key)
533    }
534}
535
536pub struct SerializeStructVariant<S> {
537    ser: S,
538    fmt: ByteFormat,
539}
540
541impl<S> SerializeStructVariant<S> {
542    fn new(serialize_struct_variant: S, fmt: ByteFormat) -> Self {
543        SerializeStructVariant {
544            ser: serialize_struct_variant,
545            fmt,
546        }
547    }
548}
549
550impl<S> ser::SerializeStructVariant for SerializeStructVariant<S>
551where
552    S: ser::SerializeStructVariant,
553{
554    type Ok = S::Ok;
555    type Error = S::Error;
556
557    fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error>
558    where
559        T: ?Sized + ser::Serialize,
560    {
561        self.ser
562            .serialize_field(key, &BytesSerialize::new(value, self.fmt.clone()))
563    }
564
565    fn end(self) -> Result<Self::Ok, Self::Error> {
566        self.ser.end()
567    }
568
569    fn skip_field(&mut self, key: &'static str) -> Result<(), Self::Error> {
570        self.ser.skip_field(key)
571    }
572}