1use crate::{PreserveIgnoredFields, SerializeIgnoredFields};
2
3impl<T, IgnoredFields> serde::Serialize for PreserveIgnoredFields<T, IgnoredFields>
4where
5 T: serde::Serialize,
6 IgnoredFields: SerializeIgnoredFields,
7{
8 fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
9 self.value.serialize(Serializer::new(serializer, &self.ignored_fields))
10 }
11}
12
13struct Serializer<'a, Inner, IgnoredFields> {
15 inner: Inner,
17
18 ignored_fields: &'a IgnoredFields,
20}
21
22impl<'a, Inner, IgnoredFields> Serializer<'a, Inner, IgnoredFields> {
23 fn new(inner: Inner, ignored_fields: &'a IgnoredFields) -> Self {
25 Self { inner, ignored_fields }
26 }
27}
28
29impl<'a, S, IgnoredFields> serde::Serializer for Serializer<'a, S, IgnoredFields>
30where
31 S: serde::Serializer,
32 IgnoredFields: SerializeIgnoredFields,
33{
34 type Error = S::Error;
35 type Ok = S::Ok;
36 type SerializeMap = Serializer<'a, S::SerializeMap, IgnoredFields>;
37 type SerializeSeq = serde::ser::Impossible<S::Ok, S::Error>;
38 type SerializeStruct = Serializer<'a, S::SerializeMap, IgnoredFields>;
39 type SerializeStructVariant = Serializer<'a, S::SerializeMap, IgnoredFields>;
40 type SerializeTuple = serde::ser::Impossible<S::Ok, S::Error>;
41 type SerializeTupleStruct = serde::ser::Impossible<S::Ok, S::Error>;
42 type SerializeTupleVariant = serde::ser::Impossible<S::Ok, S::Error>;
43
44 fn serialize_bool(self, _v: bool) -> Result<Self::Ok, Self::Error> {
45 Err(serde::ser::Error::custom(
46 "invalid type `bool`: can only re-serialize a map or struct with ignored fields",
47 ))
48 }
49
50 fn serialize_i8(self, _v: i8) -> Result<Self::Ok, Self::Error> {
51 Err(serde::ser::Error::custom(
52 "invalid type `i8`: can only re-serialize a map or struct with ignored fields",
53 ))
54 }
55
56 fn serialize_i16(self, _v: i16) -> Result<Self::Ok, Self::Error> {
57 Err(serde::ser::Error::custom(
58 "invalid type `i16`: can only re-serialize a map or struct with ignored fields",
59 ))
60 }
61
62 fn serialize_i32(self, _v: i32) -> Result<Self::Ok, Self::Error> {
63 Err(serde::ser::Error::custom(
64 "invalid type `i32`: can only re-serialize a map or struct with ignored fields",
65 ))
66 }
67
68 fn serialize_i64(self, _v: i64) -> Result<Self::Ok, Self::Error> {
69 Err(serde::ser::Error::custom(
70 "invalid type `i64`: can only re-serialize a map or struct with ignored fields",
71 ))
72 }
73
74 fn serialize_i128(self, _v: i128) -> Result<Self::Ok, Self::Error> {
75 Err(serde::ser::Error::custom(
76 "invalid type `i128`: can only re-serialize a map or struct with ignored fields",
77 ))
78 }
79
80 fn serialize_u8(self, _v: u8) -> Result<Self::Ok, Self::Error> {
81 Err(serde::ser::Error::custom(
82 "invalid type `u8`: can only re-serialize a map or struct with ignored fields",
83 ))
84 }
85
86 fn serialize_u16(self, _v: u16) -> Result<Self::Ok, Self::Error> {
87 Err(serde::ser::Error::custom(
88 "invalid type `u16`: can only re-serialize a map or struct with ignored fields",
89 ))
90 }
91
92 fn serialize_u32(self, _v: u32) -> Result<Self::Ok, Self::Error> {
93 Err(serde::ser::Error::custom(
94 "invalid type `u32`: can only re-serialize a map or struct with ignored fields",
95 ))
96 }
97
98 fn serialize_u64(self, _v: u64) -> Result<Self::Ok, Self::Error> {
99 Err(serde::ser::Error::custom(
100 "invalid type `u64`: can only re-serialize a map or struct with ignored fields",
101 ))
102 }
103
104 fn serialize_u128(self, _v: u128) -> Result<Self::Ok, Self::Error> {
105 Err(serde::ser::Error::custom(
106 "invalid type `u128`: can only re-serialize a map or struct with ignored fields",
107 ))
108 }
109
110 fn serialize_f32(self, _v: f32) -> Result<Self::Ok, Self::Error> {
111 Err(serde::ser::Error::custom(
112 "invalid type `f32`: can only re-serialize a map or struct with ignored fields",
113 ))
114 }
115
116 fn serialize_f64(self, _v: f64) -> Result<Self::Ok, Self::Error> {
117 Err(serde::ser::Error::custom(
118 "invalid type `f64`: can only re-serialize a map or struct with ignored fields",
119 ))
120 }
121
122 fn serialize_char(self, _v: char) -> Result<Self::Ok, Self::Error> {
123 Err(serde::ser::Error::custom(
124 "invalid type `char`: can only re-serialize a map or struct with ignored fields",
125 ))
126 }
127
128 fn serialize_str(self, _v: &str) -> Result<Self::Ok, Self::Error> {
129 Err(serde::ser::Error::custom(
130 "invalid type `str`: can only re-serialize a map or struct with ignored fields",
131 ))
132 }
133
134 fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok, Self::Error> {
135 Err(serde::ser::Error::custom(
136 "invalid type `bytes`: can only re-serialize a map or struct with ignored fields",
137 ))
138 }
139
140 fn serialize_none(self) -> Result<Self::Ok, Self::Error> {
141 use serde::ser::SerializeMap;
143 let map = self.serialize_map(Some(0))?;
144 map.end()
145 }
146
147 fn serialize_some<T: ?Sized + serde::Serialize>(self, value: &T) -> Result<Self::Ok, Self::Error> {
148 serde::Serialize::serialize(value, self)
150 }
151
152 fn serialize_unit(self) -> Result<Self::Ok, Self::Error> {
153 use serde::ser::SerializeMap;
155 let map = self.serialize_map(Some(0))?;
156 map.end()
157 }
158
159 fn serialize_unit_struct(self, name: &'static str) -> Result<Self::Ok, Self::Error> {
160 use serde::ser::SerializeStruct;
161 let ser = self.serialize_struct(name, 0)?;
162 ser.end()
163 }
164
165 fn serialize_unit_variant(
166 self,
167 _name: &'static str,
168 _variant_index: u32,
169 _variant: &'static str,
170 ) -> Result<Self::Ok, Self::Error> {
171 Err(serde::ser::Error::custom(
172 "invalid type `enum`: can only re-serialize a map or struct with ignored fields",
173 ))
174 }
175
176 fn serialize_newtype_struct<T: ?Sized + serde::Serialize>(
177 self,
178 _name: &'static str,
179 value: &T,
180 ) -> Result<Self::Ok, Self::Error> {
181 serde::Serialize::serialize(value, self)
182 }
183
184 fn serialize_newtype_variant<T: ?Sized + serde::Serialize>(
185 self,
186 _name: &'static str,
187 _variant_index: u32,
188 _variant: &'static str,
189 _value: &T,
190 ) -> Result<Self::Ok, Self::Error> {
191 Err(serde::ser::Error::custom(
192 "invalid type `enum`: can only re-serialize a map or struct with ignored fields",
193 ))
194 }
195
196 fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {
197 Err(serde::ser::Error::custom(
198 "invalid type `sequence`: can only re-serialize a map or struct with ignored fields",
199 ))
200 }
201
202 fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, Self::Error> {
203 Err(serde::ser::Error::custom(
204 "invalid type `tuple`: can only re-serialize a map or struct with ignored fields",
205 ))
206 }
207
208 fn serialize_tuple_struct(
209 self,
210 _name: &'static str,
211 _len: usize,
212 ) -> Result<Self::SerializeTupleStruct, Self::Error> {
213 Err(serde::ser::Error::custom(
214 "invalid type `tuple`: can only re-serialize a map or struct with ignored fields",
215 ))
216 }
217
218 fn serialize_tuple_variant(
219 self,
220 _name: &'static str,
221 _variant_index: u32,
222 _variant: &'static str,
223 _len: usize,
224 ) -> Result<Self::SerializeTupleVariant, Self::Error> {
225 Err(serde::ser::Error::custom(
226 "invalid type `enum`: can only re-serialize a map or struct with ignored fields",
227 ))
228 }
229
230 fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> {
231 let len = len.map(|x| x + self.ignored_fields.len());
232 let map = self.inner.serialize_map(len)?;
233 Ok(Serializer::new(map, self.ignored_fields))
234 }
235
236 fn serialize_struct(self, _name: &'static str, len: usize) -> Result<Self::SerializeStruct, Self::Error> {
237 let map = self.inner.serialize_map(Some(len + self.ignored_fields.len()))?;
238 Ok(Serializer::new(map, self.ignored_fields))
239 }
240
241 fn serialize_struct_variant(
242 self,
243 _name: &'static str,
244 _variant_index: u32,
245 _vartiant_name: &'static str,
246 _len: usize,
247 ) -> Result<Self::SerializeStructVariant, Self::Error> {
248 Err(serde::ser::Error::custom(
249 "invalid type `enum`: can only re-serialize a map or struct with ignored fields",
250 ))
251 }
252}
253
254impl<'a, M, IgnoredFields> serde::ser::SerializeMap for Serializer<'a, M, IgnoredFields>
255where
256 M: serde::ser::SerializeMap,
257 IgnoredFields: SerializeIgnoredFields,
258{
259 type Error = M::Error;
260 type Ok = M::Ok;
261
262 fn serialize_key<T: ?Sized + serde::Serialize>(&mut self, key: &T) -> Result<(), Self::Error> {
263 self.inner.serialize_key(key)
264 }
265
266 fn serialize_value<T: ?Sized + serde::Serialize>(&mut self, value: &T) -> Result<(), Self::Error> {
267 self.inner.serialize_value(value)
268 }
269
270 fn serialize_entry<K, V>(&mut self, key: &K, value: &V) -> Result<(), Self::Error>
271 where
272 K: ?Sized + serde::Serialize,
273 V: ?Sized + serde::Serialize,
274 {
275 self.inner.serialize_entry(key, value)
276 }
277
278 fn end(mut self) -> Result<Self::Ok, Self::Error> {
279 for (key, value) in self.ignored_fields.iter() {
280 self.inner.serialize_entry(key, value)?
281 }
282 self.inner.end()
283 }
284}
285
286impl<'a, M, IgnoredFields> serde::ser::SerializeStruct for Serializer<'a, M, IgnoredFields>
287where
288 M: serde::ser::SerializeMap,
289 IgnoredFields: SerializeIgnoredFields,
290{
291 type Error = M::Error;
292 type Ok = M::Ok;
293
294 fn serialize_field<T: ?Sized + serde::Serialize>(
295 &mut self,
296 key: &'static str,
297 value: &T,
298 ) -> Result<(), Self::Error> {
299 self.inner.serialize_entry(key, value)
300 }
301
302 fn end(mut self) -> Result<Self::Ok, Self::Error> {
303 for (key, value) in self.ignored_fields.iter() {
304 self.inner.serialize_entry(key, value)?
305 }
306 self.inner.end()
307 }
308}
309
310impl<'a, M, IgnoredFields> serde::ser::SerializeStructVariant for Serializer<'a, M, IgnoredFields>
311where
312 M: serde::ser::SerializeMap,
313 IgnoredFields: SerializeIgnoredFields,
314{
315 type Error = M::Error;
316 type Ok = M::Ok;
317
318 fn serialize_field<T: ?Sized + serde::Serialize>(
319 &mut self,
320 key: &'static str,
321 value: &T,
322 ) -> Result<(), Self::Error> {
323 self.inner.serialize_entry(key, value)
324 }
325
326 fn end(mut self) -> Result<Self::Ok, Self::Error> {
327 for (key, value) in self.ignored_fields.iter() {
328 self.inner.serialize_entry(key, value)?
329 }
330 self.inner.end()
331 }
332}