messpack_serde/
bytes.rs

1//! Hacky serializer that only allows `u8`
2
3use serde::ser::Impossible;
4use serde::Serialize;
5use std::fmt;
6
7pub(crate) struct OnlyBytes;
8pub(crate) struct Nope;
9
10impl std::error::Error for Nope {}
11
12impl std::fmt::Display for Nope {
13    fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
14        Ok(())
15    }
16}
17
18impl std::fmt::Debug for Nope {
19    fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
20        Ok(())
21    }
22}
23
24impl serde::ser::Error for Nope {
25    fn custom<T: fmt::Display>(_: T) -> Self {
26        Self
27    }
28}
29
30impl serde::de::Error for Nope {
31    fn custom<T: fmt::Display>(_: T) -> Self {
32        Self
33    }
34}
35
36impl serde::Serializer for OnlyBytes {
37    type Ok = u8;
38    type Error = Nope;
39    type SerializeSeq = Impossible<u8, Nope>;
40    type SerializeTuple = Impossible<u8, Nope>;
41    type SerializeTupleStruct = Impossible<u8, Nope>;
42    type SerializeTupleVariant = Impossible<u8, Nope>;
43    type SerializeMap = Impossible<u8, Nope>;
44    type SerializeStruct = Impossible<u8, Nope>;
45    type SerializeStructVariant = Impossible<u8, Nope>;
46
47    fn serialize_u8(self, val: u8) -> Result<u8, Nope> {
48        Ok(val)
49    }
50
51    fn serialize_bool(self, _: bool) -> Result<u8, Nope> {
52        Err(Nope)
53    }
54
55    fn serialize_i8(self, _: i8) -> Result<u8, Nope> {
56        Err(Nope)
57    }
58
59    fn serialize_i16(self, _: i16) -> Result<u8, Nope> {
60        Err(Nope)
61    }
62
63    fn serialize_i32(self, _: i32) -> Result<u8, Nope> {
64        Err(Nope)
65    }
66
67    fn serialize_i64(self, _: i64) -> Result<u8, Nope> {
68        Err(Nope)
69    }
70
71    fn serialize_u16(self, _: u16) -> Result<u8, Nope> {
72        Err(Nope)
73    }
74
75    fn serialize_u32(self, _: u32) -> Result<u8, Nope> {
76        Err(Nope)
77    }
78
79    fn serialize_u64(self, _: u64) -> Result<u8, Nope> {
80        Err(Nope)
81    }
82
83    fn serialize_f32(self, _: f32) -> Result<u8, Nope> {
84        Err(Nope)
85    }
86
87    fn serialize_f64(self, _: f64) -> Result<u8, Nope> {
88        Err(Nope)
89    }
90
91    fn serialize_char(self, _: char) -> Result<u8, Nope> {
92        Err(Nope)
93    }
94
95    fn serialize_str(self, _: &str) -> Result<u8, Nope> {
96        Err(Nope)
97    }
98
99    fn serialize_bytes(self, _: &[u8]) -> Result<u8, Nope> {
100        Err(Nope)
101    }
102
103    fn serialize_none(self) -> Result<u8, Nope> {
104        Err(Nope)
105    }
106
107    fn serialize_some<T: ?Sized + Serialize>(self, _: &T) -> Result<u8, Nope> {
108        Err(Nope)
109    }
110
111    fn serialize_unit(self) -> Result<u8, Nope> {
112        Err(Nope)
113    }
114
115    fn serialize_unit_struct(self, _: &'static str) -> Result<u8, Nope> {
116        Err(Nope)
117    }
118
119    fn serialize_unit_variant(self, _: &'static str, _: u32, _: &'static str) -> Result<u8, Nope> {
120        Err(Nope)
121    }
122
123    fn serialize_newtype_struct<T: ?Sized + Serialize>(
124        self,
125        _: &'static str,
126        _: &T,
127    ) -> Result<u8, Nope> {
128        Err(Nope)
129    }
130
131    fn serialize_newtype_variant<T: ?Sized + Serialize>(
132        self,
133        _: &'static str,
134        _: u32,
135        _: &'static str,
136        _: &T,
137    ) -> Result<u8, Nope> {
138        Err(Nope)
139    }
140
141    fn serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq, Nope> {
142        Err(Nope)
143    }
144
145    fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, Nope> {
146        Err(Nope)
147    }
148
149    fn serialize_tuple_struct(
150        self,
151        _: &'static str,
152        _: usize,
153    ) -> Result<Self::SerializeTupleStruct, Nope> {
154        Err(Nope)
155    }
156
157    fn serialize_tuple_variant(
158        self,
159        _: &'static str,
160        _: u32,
161        _: &'static str,
162        _: usize,
163    ) -> Result<Self::SerializeTupleVariant, Nope> {
164        Err(Nope)
165    }
166
167    fn serialize_map(self, _: Option<usize>) -> Result<Self::SerializeMap, Nope> {
168        Err(Nope)
169    }
170
171    fn serialize_struct(self, _: &'static str, _: usize) -> Result<Self::SerializeStruct, Nope> {
172        Err(Nope)
173    }
174
175    fn serialize_struct_variant(
176        self,
177        _: &'static str,
178        _: u32,
179        _: &'static str,
180        _: usize,
181    ) -> Result<Self::SerializeStructVariant, Nope> {
182        Err(Nope)
183    }
184
185    fn collect_seq<I>(self, _: I) -> Result<u8, Nope>
186    where
187        I: IntoIterator,
188        <I as IntoIterator>::Item: Serialize,
189    {
190        Err(Nope)
191    }
192
193    fn collect_map<K, V, I>(self, _: I) -> Result<u8, Nope>
194    where
195        K: Serialize,
196        V: Serialize,
197        I: IntoIterator<Item = (K, V)>,
198    {
199        Err(Nope)
200    }
201
202    fn collect_str<T: ?Sized + fmt::Display>(self, _: &T) -> Result<u8, Nope> {
203        Err(Nope)
204    }
205}