/rust/registry/src/index.crates.io-1949cf8c6b5b557f/serde-1.0.228/src/private/ser.rs
Line | Count | Source |
1 | | use crate::lib::*; |
2 | | |
3 | | use crate::ser::{self, Impossible, Serialize, SerializeMap, SerializeStruct, Serializer}; |
4 | | |
5 | | #[cfg(any(feature = "std", feature = "alloc"))] |
6 | | use self::content::{ |
7 | | Content, ContentSerializer, SerializeStructVariantAsMapValue, SerializeTupleVariantAsMapValue, |
8 | | }; |
9 | | |
10 | | /// Used to check that serde(getter) attributes return the expected type. |
11 | | /// Not public API. |
12 | 0 | pub fn constrain<T: ?Sized>(t: &T) -> &T { |
13 | 0 | t |
14 | 0 | } |
15 | | |
16 | | /// Not public API. |
17 | 0 | pub fn serialize_tagged_newtype<S, T>( |
18 | 0 | serializer: S, |
19 | 0 | type_ident: &'static str, |
20 | 0 | variant_ident: &'static str, |
21 | 0 | tag: &'static str, |
22 | 0 | variant_name: &'static str, |
23 | 0 | value: &T, |
24 | 0 | ) -> Result<S::Ok, S::Error> |
25 | 0 | where |
26 | 0 | S: Serializer, |
27 | 0 | T: Serialize, |
28 | | { |
29 | 0 | value.serialize(TaggedSerializer { |
30 | 0 | type_ident, |
31 | 0 | variant_ident, |
32 | 0 | tag, |
33 | 0 | variant_name, |
34 | 0 | delegate: serializer, |
35 | 0 | }) |
36 | 0 | } |
37 | | |
38 | | struct TaggedSerializer<S> { |
39 | | type_ident: &'static str, |
40 | | variant_ident: &'static str, |
41 | | tag: &'static str, |
42 | | variant_name: &'static str, |
43 | | delegate: S, |
44 | | } |
45 | | |
46 | | enum Unsupported { |
47 | | Boolean, |
48 | | Integer, |
49 | | Float, |
50 | | Char, |
51 | | String, |
52 | | ByteArray, |
53 | | Optional, |
54 | | Sequence, |
55 | | Tuple, |
56 | | TupleStruct, |
57 | | #[cfg(not(any(feature = "std", feature = "alloc")))] |
58 | | Enum, |
59 | | } |
60 | | |
61 | | #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] |
62 | | impl Display for Unsupported { |
63 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
64 | 0 | match *self { |
65 | 0 | Unsupported::Boolean => formatter.write_str("a boolean"), |
66 | 0 | Unsupported::Integer => formatter.write_str("an integer"), |
67 | 0 | Unsupported::Float => formatter.write_str("a float"), |
68 | 0 | Unsupported::Char => formatter.write_str("a char"), |
69 | 0 | Unsupported::String => formatter.write_str("a string"), |
70 | 0 | Unsupported::ByteArray => formatter.write_str("a byte array"), |
71 | 0 | Unsupported::Optional => formatter.write_str("an optional"), |
72 | 0 | Unsupported::Sequence => formatter.write_str("a sequence"), |
73 | 0 | Unsupported::Tuple => formatter.write_str("a tuple"), |
74 | 0 | Unsupported::TupleStruct => formatter.write_str("a tuple struct"), |
75 | | #[cfg(not(any(feature = "std", feature = "alloc")))] |
76 | | Unsupported::Enum => formatter.write_str("an enum"), |
77 | | } |
78 | 0 | } |
79 | | } |
80 | | |
81 | | impl<S> TaggedSerializer<S> |
82 | | where |
83 | | S: Serializer, |
84 | | { |
85 | 0 | fn bad_type(self, what: Unsupported) -> S::Error { |
86 | 0 | ser::Error::custom(format_args!( |
87 | 0 | "cannot serialize tagged newtype variant {}::{} containing {}", |
88 | | self.type_ident, self.variant_ident, what |
89 | | )) |
90 | 0 | } |
91 | | } |
92 | | |
93 | | #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] |
94 | | impl<S> Serializer for TaggedSerializer<S> |
95 | | where |
96 | | S: Serializer, |
97 | | { |
98 | | type Ok = S::Ok; |
99 | | type Error = S::Error; |
100 | | |
101 | | type SerializeSeq = Impossible<S::Ok, S::Error>; |
102 | | type SerializeTuple = Impossible<S::Ok, S::Error>; |
103 | | type SerializeTupleStruct = Impossible<S::Ok, S::Error>; |
104 | | type SerializeMap = S::SerializeMap; |
105 | | type SerializeStruct = S::SerializeStruct; |
106 | | |
107 | | #[cfg(not(any(feature = "std", feature = "alloc")))] |
108 | | type SerializeTupleVariant = Impossible<S::Ok, S::Error>; |
109 | | #[cfg(any(feature = "std", feature = "alloc"))] |
110 | | type SerializeTupleVariant = SerializeTupleVariantAsMapValue<S::SerializeMap>; |
111 | | |
112 | | #[cfg(not(any(feature = "std", feature = "alloc")))] |
113 | | type SerializeStructVariant = Impossible<S::Ok, S::Error>; |
114 | | #[cfg(any(feature = "std", feature = "alloc"))] |
115 | | type SerializeStructVariant = SerializeStructVariantAsMapValue<S::SerializeMap>; |
116 | | |
117 | 0 | fn serialize_bool(self, _: bool) -> Result<Self::Ok, Self::Error> { |
118 | 0 | Err(self.bad_type(Unsupported::Boolean)) |
119 | 0 | } |
120 | | |
121 | 0 | fn serialize_i8(self, _: i8) -> Result<Self::Ok, Self::Error> { |
122 | 0 | Err(self.bad_type(Unsupported::Integer)) |
123 | 0 | } |
124 | | |
125 | 0 | fn serialize_i16(self, _: i16) -> Result<Self::Ok, Self::Error> { |
126 | 0 | Err(self.bad_type(Unsupported::Integer)) |
127 | 0 | } |
128 | | |
129 | 0 | fn serialize_i32(self, _: i32) -> Result<Self::Ok, Self::Error> { |
130 | 0 | Err(self.bad_type(Unsupported::Integer)) |
131 | 0 | } |
132 | | |
133 | 0 | fn serialize_i64(self, _: i64) -> Result<Self::Ok, Self::Error> { |
134 | 0 | Err(self.bad_type(Unsupported::Integer)) |
135 | 0 | } |
136 | | |
137 | 0 | fn serialize_u8(self, _: u8) -> Result<Self::Ok, Self::Error> { |
138 | 0 | Err(self.bad_type(Unsupported::Integer)) |
139 | 0 | } |
140 | | |
141 | 0 | fn serialize_u16(self, _: u16) -> Result<Self::Ok, Self::Error> { |
142 | 0 | Err(self.bad_type(Unsupported::Integer)) |
143 | 0 | } |
144 | | |
145 | 0 | fn serialize_u32(self, _: u32) -> Result<Self::Ok, Self::Error> { |
146 | 0 | Err(self.bad_type(Unsupported::Integer)) |
147 | 0 | } |
148 | | |
149 | 0 | fn serialize_u64(self, _: u64) -> Result<Self::Ok, Self::Error> { |
150 | 0 | Err(self.bad_type(Unsupported::Integer)) |
151 | 0 | } |
152 | | |
153 | 0 | fn serialize_f32(self, _: f32) -> Result<Self::Ok, Self::Error> { |
154 | 0 | Err(self.bad_type(Unsupported::Float)) |
155 | 0 | } |
156 | | |
157 | 0 | fn serialize_f64(self, _: f64) -> Result<Self::Ok, Self::Error> { |
158 | 0 | Err(self.bad_type(Unsupported::Float)) |
159 | 0 | } |
160 | | |
161 | 0 | fn serialize_char(self, _: char) -> Result<Self::Ok, Self::Error> { |
162 | 0 | Err(self.bad_type(Unsupported::Char)) |
163 | 0 | } |
164 | | |
165 | 0 | fn serialize_str(self, _: &str) -> Result<Self::Ok, Self::Error> { |
166 | 0 | Err(self.bad_type(Unsupported::String)) |
167 | 0 | } |
168 | | |
169 | 0 | fn serialize_bytes(self, _: &[u8]) -> Result<Self::Ok, Self::Error> { |
170 | 0 | Err(self.bad_type(Unsupported::ByteArray)) |
171 | 0 | } |
172 | | |
173 | 0 | fn serialize_none(self) -> Result<Self::Ok, Self::Error> { |
174 | 0 | Err(self.bad_type(Unsupported::Optional)) |
175 | 0 | } |
176 | | |
177 | 0 | fn serialize_some<T>(self, _: &T) -> Result<Self::Ok, Self::Error> |
178 | 0 | where |
179 | 0 | T: ?Sized + Serialize, |
180 | | { |
181 | 0 | Err(self.bad_type(Unsupported::Optional)) |
182 | 0 | } |
183 | | |
184 | 0 | fn serialize_unit(self) -> Result<Self::Ok, Self::Error> { |
185 | 0 | let mut map = tri!(self.delegate.serialize_map(Some(1))); |
186 | 0 | tri!(map.serialize_entry(self.tag, self.variant_name)); |
187 | 0 | map.end() |
188 | 0 | } |
189 | | |
190 | 0 | fn serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error> { |
191 | 0 | let mut map = tri!(self.delegate.serialize_map(Some(1))); |
192 | 0 | tri!(map.serialize_entry(self.tag, self.variant_name)); |
193 | 0 | map.end() |
194 | 0 | } |
195 | | |
196 | 0 | fn serialize_unit_variant( |
197 | 0 | self, |
198 | 0 | _: &'static str, |
199 | 0 | _: u32, |
200 | 0 | inner_variant: &'static str, |
201 | 0 | ) -> Result<Self::Ok, Self::Error> { |
202 | 0 | let mut map = tri!(self.delegate.serialize_map(Some(2))); |
203 | 0 | tri!(map.serialize_entry(self.tag, self.variant_name)); |
204 | 0 | tri!(map.serialize_entry(inner_variant, &())); |
205 | 0 | map.end() |
206 | 0 | } |
207 | | |
208 | 0 | fn serialize_newtype_struct<T>( |
209 | 0 | self, |
210 | 0 | _: &'static str, |
211 | 0 | value: &T, |
212 | 0 | ) -> Result<Self::Ok, Self::Error> |
213 | 0 | where |
214 | 0 | T: ?Sized + Serialize, |
215 | | { |
216 | 0 | value.serialize(self) |
217 | 0 | } |
218 | | |
219 | 0 | fn serialize_newtype_variant<T>( |
220 | 0 | self, |
221 | 0 | _: &'static str, |
222 | 0 | _: u32, |
223 | 0 | inner_variant: &'static str, |
224 | 0 | inner_value: &T, |
225 | 0 | ) -> Result<Self::Ok, Self::Error> |
226 | 0 | where |
227 | 0 | T: ?Sized + Serialize, |
228 | | { |
229 | 0 | let mut map = tri!(self.delegate.serialize_map(Some(2))); |
230 | 0 | tri!(map.serialize_entry(self.tag, self.variant_name)); |
231 | 0 | tri!(map.serialize_entry(inner_variant, inner_value)); |
232 | 0 | map.end() |
233 | 0 | } |
234 | | |
235 | 0 | fn serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { |
236 | 0 | Err(self.bad_type(Unsupported::Sequence)) |
237 | 0 | } |
238 | | |
239 | 0 | fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, Self::Error> { |
240 | 0 | Err(self.bad_type(Unsupported::Tuple)) |
241 | 0 | } |
242 | | |
243 | 0 | fn serialize_tuple_struct( |
244 | 0 | self, |
245 | 0 | _: &'static str, |
246 | 0 | _: usize, |
247 | 0 | ) -> Result<Self::SerializeTupleStruct, Self::Error> { |
248 | 0 | Err(self.bad_type(Unsupported::TupleStruct)) |
249 | 0 | } |
250 | | |
251 | | #[cfg(not(any(feature = "std", feature = "alloc")))] |
252 | | fn serialize_tuple_variant( |
253 | | self, |
254 | | _: &'static str, |
255 | | _: u32, |
256 | | _: &'static str, |
257 | | _: usize, |
258 | | ) -> Result<Self::SerializeTupleVariant, Self::Error> { |
259 | | // Lack of push-based serialization means we need to buffer the content |
260 | | // of the tuple variant, so it requires std. |
261 | | Err(self.bad_type(Unsupported::Enum)) |
262 | | } |
263 | | |
264 | | #[cfg(any(feature = "std", feature = "alloc"))] |
265 | 0 | fn serialize_tuple_variant( |
266 | 0 | self, |
267 | 0 | _: &'static str, |
268 | 0 | _: u32, |
269 | 0 | inner_variant: &'static str, |
270 | 0 | len: usize, |
271 | 0 | ) -> Result<Self::SerializeTupleVariant, Self::Error> { |
272 | 0 | let mut map = tri!(self.delegate.serialize_map(Some(2))); |
273 | 0 | tri!(map.serialize_entry(self.tag, self.variant_name)); |
274 | 0 | tri!(map.serialize_key(inner_variant)); |
275 | 0 | Ok(SerializeTupleVariantAsMapValue::new( |
276 | 0 | map, |
277 | 0 | inner_variant, |
278 | 0 | len, |
279 | 0 | )) |
280 | 0 | } |
281 | | |
282 | 0 | fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { |
283 | 0 | let mut map = tri!(self.delegate.serialize_map(len.map(|len| len + 1))); |
284 | 0 | tri!(map.serialize_entry(self.tag, self.variant_name)); |
285 | 0 | Ok(map) |
286 | 0 | } |
287 | | |
288 | 0 | fn serialize_struct( |
289 | 0 | self, |
290 | 0 | name: &'static str, |
291 | 0 | len: usize, |
292 | 0 | ) -> Result<Self::SerializeStruct, Self::Error> { |
293 | 0 | let mut state = tri!(self.delegate.serialize_struct(name, len + 1)); |
294 | 0 | tri!(state.serialize_field(self.tag, self.variant_name)); |
295 | 0 | Ok(state) |
296 | 0 | } |
297 | | |
298 | | #[cfg(not(any(feature = "std", feature = "alloc")))] |
299 | | fn serialize_struct_variant( |
300 | | self, |
301 | | _: &'static str, |
302 | | _: u32, |
303 | | _: &'static str, |
304 | | _: usize, |
305 | | ) -> Result<Self::SerializeStructVariant, Self::Error> { |
306 | | // Lack of push-based serialization means we need to buffer the content |
307 | | // of the struct variant, so it requires std. |
308 | | Err(self.bad_type(Unsupported::Enum)) |
309 | | } |
310 | | |
311 | | #[cfg(any(feature = "std", feature = "alloc"))] |
312 | 0 | fn serialize_struct_variant( |
313 | 0 | self, |
314 | 0 | _: &'static str, |
315 | 0 | _: u32, |
316 | 0 | inner_variant: &'static str, |
317 | 0 | len: usize, |
318 | 0 | ) -> Result<Self::SerializeStructVariant, Self::Error> { |
319 | 0 | let mut map = tri!(self.delegate.serialize_map(Some(2))); |
320 | 0 | tri!(map.serialize_entry(self.tag, self.variant_name)); |
321 | 0 | tri!(map.serialize_key(inner_variant)); |
322 | 0 | Ok(SerializeStructVariantAsMapValue::new( |
323 | 0 | map, |
324 | 0 | inner_variant, |
325 | 0 | len, |
326 | 0 | )) |
327 | 0 | } |
328 | | |
329 | | #[cfg(not(any(feature = "std", feature = "alloc")))] |
330 | | fn collect_str<T>(self, _: &T) -> Result<Self::Ok, Self::Error> |
331 | | where |
332 | | T: ?Sized + Display, |
333 | | { |
334 | | Err(self.bad_type(Unsupported::String)) |
335 | | } |
336 | | } |
337 | | |
338 | | #[cfg(any(feature = "std", feature = "alloc"))] |
339 | | mod content { |
340 | | use crate::lib::*; |
341 | | |
342 | | use crate::ser::{self, Serialize, Serializer}; |
343 | | |
344 | | pub struct SerializeTupleVariantAsMapValue<M> { |
345 | | map: M, |
346 | | name: &'static str, |
347 | | fields: Vec<Content>, |
348 | | } |
349 | | |
350 | | impl<M> SerializeTupleVariantAsMapValue<M> { |
351 | 0 | pub fn new(map: M, name: &'static str, len: usize) -> Self { |
352 | 0 | SerializeTupleVariantAsMapValue { |
353 | 0 | map, |
354 | 0 | name, |
355 | 0 | fields: Vec::with_capacity(len), |
356 | 0 | } |
357 | 0 | } |
358 | | } |
359 | | |
360 | | #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] |
361 | | impl<M> ser::SerializeTupleVariant for SerializeTupleVariantAsMapValue<M> |
362 | | where |
363 | | M: ser::SerializeMap, |
364 | | { |
365 | | type Ok = M::Ok; |
366 | | type Error = M::Error; |
367 | | |
368 | 0 | fn serialize_field<T>(&mut self, value: &T) -> Result<(), M::Error> |
369 | 0 | where |
370 | 0 | T: ?Sized + Serialize, |
371 | | { |
372 | 0 | let value = tri!(value.serialize(ContentSerializer::<M::Error>::new())); |
373 | 0 | self.fields.push(value); |
374 | 0 | Ok(()) |
375 | 0 | } |
376 | | |
377 | 0 | fn end(mut self) -> Result<M::Ok, M::Error> { |
378 | 0 | tri!(self |
379 | 0 | .map |
380 | 0 | .serialize_value(&Content::TupleStruct(self.name, self.fields))); |
381 | 0 | self.map.end() |
382 | 0 | } |
383 | | } |
384 | | |
385 | | pub struct SerializeStructVariantAsMapValue<M> { |
386 | | map: M, |
387 | | name: &'static str, |
388 | | fields: Vec<(&'static str, Content)>, |
389 | | } |
390 | | |
391 | | impl<M> SerializeStructVariantAsMapValue<M> { |
392 | 0 | pub fn new(map: M, name: &'static str, len: usize) -> Self { |
393 | 0 | SerializeStructVariantAsMapValue { |
394 | 0 | map, |
395 | 0 | name, |
396 | 0 | fields: Vec::with_capacity(len), |
397 | 0 | } |
398 | 0 | } |
399 | | } |
400 | | |
401 | | #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] |
402 | | impl<M> ser::SerializeStructVariant for SerializeStructVariantAsMapValue<M> |
403 | | where |
404 | | M: ser::SerializeMap, |
405 | | { |
406 | | type Ok = M::Ok; |
407 | | type Error = M::Error; |
408 | | |
409 | 0 | fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), M::Error> |
410 | 0 | where |
411 | 0 | T: ?Sized + Serialize, |
412 | | { |
413 | 0 | let value = tri!(value.serialize(ContentSerializer::<M::Error>::new())); |
414 | 0 | self.fields.push((key, value)); |
415 | 0 | Ok(()) |
416 | 0 | } |
417 | | |
418 | 0 | fn end(mut self) -> Result<M::Ok, M::Error> { |
419 | 0 | tri!(self |
420 | 0 | .map |
421 | 0 | .serialize_value(&Content::Struct(self.name, self.fields))); |
422 | 0 | self.map.end() |
423 | 0 | } |
424 | | } |
425 | | |
426 | | pub enum Content { |
427 | | Bool(bool), |
428 | | |
429 | | U8(u8), |
430 | | U16(u16), |
431 | | U32(u32), |
432 | | U64(u64), |
433 | | |
434 | | I8(i8), |
435 | | I16(i16), |
436 | | I32(i32), |
437 | | I64(i64), |
438 | | |
439 | | F32(f32), |
440 | | F64(f64), |
441 | | |
442 | | Char(char), |
443 | | String(String), |
444 | | Bytes(Vec<u8>), |
445 | | |
446 | | None, |
447 | | Some(Box<Content>), |
448 | | |
449 | | Unit, |
450 | | UnitStruct(&'static str), |
451 | | UnitVariant(&'static str, u32, &'static str), |
452 | | NewtypeStruct(&'static str, Box<Content>), |
453 | | NewtypeVariant(&'static str, u32, &'static str, Box<Content>), |
454 | | |
455 | | Seq(Vec<Content>), |
456 | | Tuple(Vec<Content>), |
457 | | TupleStruct(&'static str, Vec<Content>), |
458 | | TupleVariant(&'static str, u32, &'static str, Vec<Content>), |
459 | | Map(Vec<(Content, Content)>), |
460 | | Struct(&'static str, Vec<(&'static str, Content)>), |
461 | | StructVariant( |
462 | | &'static str, |
463 | | u32, |
464 | | &'static str, |
465 | | Vec<(&'static str, Content)>, |
466 | | ), |
467 | | } |
468 | | |
469 | | #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] |
470 | | impl Serialize for Content { |
471 | 0 | fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
472 | 0 | where |
473 | 0 | S: Serializer, |
474 | | { |
475 | 0 | match *self { |
476 | 0 | Content::Bool(b) => serializer.serialize_bool(b), |
477 | 0 | Content::U8(u) => serializer.serialize_u8(u), |
478 | 0 | Content::U16(u) => serializer.serialize_u16(u), |
479 | 0 | Content::U32(u) => serializer.serialize_u32(u), |
480 | 0 | Content::U64(u) => serializer.serialize_u64(u), |
481 | 0 | Content::I8(i) => serializer.serialize_i8(i), |
482 | 0 | Content::I16(i) => serializer.serialize_i16(i), |
483 | 0 | Content::I32(i) => serializer.serialize_i32(i), |
484 | 0 | Content::I64(i) => serializer.serialize_i64(i), |
485 | 0 | Content::F32(f) => serializer.serialize_f32(f), |
486 | 0 | Content::F64(f) => serializer.serialize_f64(f), |
487 | 0 | Content::Char(c) => serializer.serialize_char(c), |
488 | 0 | Content::String(ref s) => serializer.serialize_str(s), |
489 | 0 | Content::Bytes(ref b) => serializer.serialize_bytes(b), |
490 | 0 | Content::None => serializer.serialize_none(), |
491 | 0 | Content::Some(ref c) => serializer.serialize_some(&**c), |
492 | 0 | Content::Unit => serializer.serialize_unit(), |
493 | 0 | Content::UnitStruct(n) => serializer.serialize_unit_struct(n), |
494 | 0 | Content::UnitVariant(n, i, v) => serializer.serialize_unit_variant(n, i, v), |
495 | 0 | Content::NewtypeStruct(n, ref c) => serializer.serialize_newtype_struct(n, &**c), |
496 | 0 | Content::NewtypeVariant(n, i, v, ref c) => { |
497 | 0 | serializer.serialize_newtype_variant(n, i, v, &**c) |
498 | | } |
499 | 0 | Content::Seq(ref elements) => elements.serialize(serializer), |
500 | 0 | Content::Tuple(ref elements) => { |
501 | | use crate::ser::SerializeTuple; |
502 | 0 | let mut tuple = tri!(serializer.serialize_tuple(elements.len())); |
503 | 0 | for e in elements { |
504 | 0 | tri!(tuple.serialize_element(e)); |
505 | | } |
506 | 0 | tuple.end() |
507 | | } |
508 | 0 | Content::TupleStruct(n, ref fields) => { |
509 | | use crate::ser::SerializeTupleStruct; |
510 | 0 | let mut ts = tri!(serializer.serialize_tuple_struct(n, fields.len())); |
511 | 0 | for f in fields { |
512 | 0 | tri!(ts.serialize_field(f)); |
513 | | } |
514 | 0 | ts.end() |
515 | | } |
516 | 0 | Content::TupleVariant(n, i, v, ref fields) => { |
517 | | use crate::ser::SerializeTupleVariant; |
518 | 0 | let mut tv = tri!(serializer.serialize_tuple_variant(n, i, v, fields.len())); |
519 | 0 | for f in fields { |
520 | 0 | tri!(tv.serialize_field(f)); |
521 | | } |
522 | 0 | tv.end() |
523 | | } |
524 | 0 | Content::Map(ref entries) => { |
525 | | use crate::ser::SerializeMap; |
526 | 0 | let mut map = tri!(serializer.serialize_map(Some(entries.len()))); |
527 | 0 | for (k, v) in entries { |
528 | 0 | tri!(map.serialize_entry(k, v)); |
529 | | } |
530 | 0 | map.end() |
531 | | } |
532 | 0 | Content::Struct(n, ref fields) => { |
533 | | use crate::ser::SerializeStruct; |
534 | 0 | let mut s = tri!(serializer.serialize_struct(n, fields.len())); |
535 | 0 | for &(k, ref v) in fields { |
536 | 0 | tri!(s.serialize_field(k, v)); |
537 | | } |
538 | 0 | s.end() |
539 | | } |
540 | 0 | Content::StructVariant(n, i, v, ref fields) => { |
541 | | use crate::ser::SerializeStructVariant; |
542 | 0 | let mut sv = tri!(serializer.serialize_struct_variant(n, i, v, fields.len())); |
543 | 0 | for &(k, ref v) in fields { |
544 | 0 | tri!(sv.serialize_field(k, v)); |
545 | | } |
546 | 0 | sv.end() |
547 | | } |
548 | | } |
549 | 0 | } |
550 | | } |
551 | | |
552 | | pub struct ContentSerializer<E> { |
553 | | error: PhantomData<E>, |
554 | | } |
555 | | |
556 | | impl<E> ContentSerializer<E> { |
557 | 0 | pub fn new() -> Self { |
558 | 0 | ContentSerializer { error: PhantomData } |
559 | 0 | } |
560 | | } |
561 | | |
562 | | #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] |
563 | | impl<E> Serializer for ContentSerializer<E> |
564 | | where |
565 | | E: ser::Error, |
566 | | { |
567 | | type Ok = Content; |
568 | | type Error = E; |
569 | | |
570 | | type SerializeSeq = SerializeSeq<E>; |
571 | | type SerializeTuple = SerializeTuple<E>; |
572 | | type SerializeTupleStruct = SerializeTupleStruct<E>; |
573 | | type SerializeTupleVariant = SerializeTupleVariant<E>; |
574 | | type SerializeMap = SerializeMap<E>; |
575 | | type SerializeStruct = SerializeStruct<E>; |
576 | | type SerializeStructVariant = SerializeStructVariant<E>; |
577 | | |
578 | 0 | fn serialize_bool(self, v: bool) -> Result<Content, E> { |
579 | 0 | Ok(Content::Bool(v)) |
580 | 0 | } |
581 | | |
582 | 0 | fn serialize_i8(self, v: i8) -> Result<Content, E> { |
583 | 0 | Ok(Content::I8(v)) |
584 | 0 | } |
585 | | |
586 | 0 | fn serialize_i16(self, v: i16) -> Result<Content, E> { |
587 | 0 | Ok(Content::I16(v)) |
588 | 0 | } |
589 | | |
590 | 0 | fn serialize_i32(self, v: i32) -> Result<Content, E> { |
591 | 0 | Ok(Content::I32(v)) |
592 | 0 | } |
593 | | |
594 | 0 | fn serialize_i64(self, v: i64) -> Result<Content, E> { |
595 | 0 | Ok(Content::I64(v)) |
596 | 0 | } |
597 | | |
598 | 0 | fn serialize_u8(self, v: u8) -> Result<Content, E> { |
599 | 0 | Ok(Content::U8(v)) |
600 | 0 | } |
601 | | |
602 | 0 | fn serialize_u16(self, v: u16) -> Result<Content, E> { |
603 | 0 | Ok(Content::U16(v)) |
604 | 0 | } |
605 | | |
606 | 0 | fn serialize_u32(self, v: u32) -> Result<Content, E> { |
607 | 0 | Ok(Content::U32(v)) |
608 | 0 | } |
609 | | |
610 | 0 | fn serialize_u64(self, v: u64) -> Result<Content, E> { |
611 | 0 | Ok(Content::U64(v)) |
612 | 0 | } |
613 | | |
614 | 0 | fn serialize_f32(self, v: f32) -> Result<Content, E> { |
615 | 0 | Ok(Content::F32(v)) |
616 | 0 | } |
617 | | |
618 | 0 | fn serialize_f64(self, v: f64) -> Result<Content, E> { |
619 | 0 | Ok(Content::F64(v)) |
620 | 0 | } |
621 | | |
622 | 0 | fn serialize_char(self, v: char) -> Result<Content, E> { |
623 | 0 | Ok(Content::Char(v)) |
624 | 0 | } |
625 | | |
626 | 0 | fn serialize_str(self, value: &str) -> Result<Content, E> { |
627 | 0 | Ok(Content::String(value.to_owned())) |
628 | 0 | } |
629 | | |
630 | 0 | fn serialize_bytes(self, value: &[u8]) -> Result<Content, E> { |
631 | 0 | Ok(Content::Bytes(value.to_owned())) |
632 | 0 | } |
633 | | |
634 | 0 | fn serialize_none(self) -> Result<Content, E> { |
635 | 0 | Ok(Content::None) |
636 | 0 | } |
637 | | |
638 | 0 | fn serialize_some<T>(self, value: &T) -> Result<Content, E> |
639 | 0 | where |
640 | 0 | T: ?Sized + Serialize, |
641 | | { |
642 | 0 | Ok(Content::Some(Box::new(tri!(value.serialize(self))))) |
643 | 0 | } |
644 | | |
645 | 0 | fn serialize_unit(self) -> Result<Content, E> { |
646 | 0 | Ok(Content::Unit) |
647 | 0 | } |
648 | | |
649 | 0 | fn serialize_unit_struct(self, name: &'static str) -> Result<Content, E> { |
650 | 0 | Ok(Content::UnitStruct(name)) |
651 | 0 | } |
652 | | |
653 | 0 | fn serialize_unit_variant( |
654 | 0 | self, |
655 | 0 | name: &'static str, |
656 | 0 | variant_index: u32, |
657 | 0 | variant: &'static str, |
658 | 0 | ) -> Result<Content, E> { |
659 | 0 | Ok(Content::UnitVariant(name, variant_index, variant)) |
660 | 0 | } |
661 | | |
662 | 0 | fn serialize_newtype_struct<T>(self, name: &'static str, value: &T) -> Result<Content, E> |
663 | 0 | where |
664 | 0 | T: ?Sized + Serialize, |
665 | | { |
666 | | Ok(Content::NewtypeStruct( |
667 | 0 | name, |
668 | 0 | Box::new(tri!(value.serialize(self))), |
669 | | )) |
670 | 0 | } |
671 | | |
672 | 0 | fn serialize_newtype_variant<T>( |
673 | 0 | self, |
674 | 0 | name: &'static str, |
675 | 0 | variant_index: u32, |
676 | 0 | variant: &'static str, |
677 | 0 | value: &T, |
678 | 0 | ) -> Result<Content, E> |
679 | 0 | where |
680 | 0 | T: ?Sized + Serialize, |
681 | | { |
682 | | Ok(Content::NewtypeVariant( |
683 | 0 | name, |
684 | 0 | variant_index, |
685 | 0 | variant, |
686 | 0 | Box::new(tri!(value.serialize(self))), |
687 | | )) |
688 | 0 | } |
689 | | |
690 | 0 | fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq, E> { |
691 | 0 | Ok(SerializeSeq { |
692 | 0 | elements: Vec::with_capacity(len.unwrap_or(0)), |
693 | 0 | error: PhantomData, |
694 | 0 | }) |
695 | 0 | } |
696 | | |
697 | 0 | fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple, E> { |
698 | 0 | Ok(SerializeTuple { |
699 | 0 | elements: Vec::with_capacity(len), |
700 | 0 | error: PhantomData, |
701 | 0 | }) |
702 | 0 | } |
703 | | |
704 | 0 | fn serialize_tuple_struct( |
705 | 0 | self, |
706 | 0 | name: &'static str, |
707 | 0 | len: usize, |
708 | 0 | ) -> Result<Self::SerializeTupleStruct, E> { |
709 | 0 | Ok(SerializeTupleStruct { |
710 | 0 | name, |
711 | 0 | fields: Vec::with_capacity(len), |
712 | 0 | error: PhantomData, |
713 | 0 | }) |
714 | 0 | } |
715 | | |
716 | 0 | fn serialize_tuple_variant( |
717 | 0 | self, |
718 | 0 | name: &'static str, |
719 | 0 | variant_index: u32, |
720 | 0 | variant: &'static str, |
721 | 0 | len: usize, |
722 | 0 | ) -> Result<Self::SerializeTupleVariant, E> { |
723 | 0 | Ok(SerializeTupleVariant { |
724 | 0 | name, |
725 | 0 | variant_index, |
726 | 0 | variant, |
727 | 0 | fields: Vec::with_capacity(len), |
728 | 0 | error: PhantomData, |
729 | 0 | }) |
730 | 0 | } |
731 | | |
732 | 0 | fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap, E> { |
733 | 0 | Ok(SerializeMap { |
734 | 0 | entries: Vec::with_capacity(len.unwrap_or(0)), |
735 | 0 | key: None, |
736 | 0 | error: PhantomData, |
737 | 0 | }) |
738 | 0 | } |
739 | | |
740 | 0 | fn serialize_struct( |
741 | 0 | self, |
742 | 0 | name: &'static str, |
743 | 0 | len: usize, |
744 | 0 | ) -> Result<Self::SerializeStruct, E> { |
745 | 0 | Ok(SerializeStruct { |
746 | 0 | name, |
747 | 0 | fields: Vec::with_capacity(len), |
748 | 0 | error: PhantomData, |
749 | 0 | }) |
750 | 0 | } |
751 | | |
752 | 0 | fn serialize_struct_variant( |
753 | 0 | self, |
754 | 0 | name: &'static str, |
755 | 0 | variant_index: u32, |
756 | 0 | variant: &'static str, |
757 | 0 | len: usize, |
758 | 0 | ) -> Result<Self::SerializeStructVariant, E> { |
759 | 0 | Ok(SerializeStructVariant { |
760 | 0 | name, |
761 | 0 | variant_index, |
762 | 0 | variant, |
763 | 0 | fields: Vec::with_capacity(len), |
764 | 0 | error: PhantomData, |
765 | 0 | }) |
766 | 0 | } |
767 | | } |
768 | | |
769 | | pub struct SerializeSeq<E> { |
770 | | elements: Vec<Content>, |
771 | | error: PhantomData<E>, |
772 | | } |
773 | | |
774 | | #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] |
775 | | impl<E> ser::SerializeSeq for SerializeSeq<E> |
776 | | where |
777 | | E: ser::Error, |
778 | | { |
779 | | type Ok = Content; |
780 | | type Error = E; |
781 | | |
782 | 0 | fn serialize_element<T>(&mut self, value: &T) -> Result<(), E> |
783 | 0 | where |
784 | 0 | T: ?Sized + Serialize, |
785 | | { |
786 | 0 | let value = tri!(value.serialize(ContentSerializer::<E>::new())); |
787 | 0 | self.elements.push(value); |
788 | 0 | Ok(()) |
789 | 0 | } |
790 | | |
791 | 0 | fn end(self) -> Result<Content, E> { |
792 | 0 | Ok(Content::Seq(self.elements)) |
793 | 0 | } |
794 | | } |
795 | | |
796 | | pub struct SerializeTuple<E> { |
797 | | elements: Vec<Content>, |
798 | | error: PhantomData<E>, |
799 | | } |
800 | | |
801 | | #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] |
802 | | impl<E> ser::SerializeTuple for SerializeTuple<E> |
803 | | where |
804 | | E: ser::Error, |
805 | | { |
806 | | type Ok = Content; |
807 | | type Error = E; |
808 | | |
809 | 0 | fn serialize_element<T>(&mut self, value: &T) -> Result<(), E> |
810 | 0 | where |
811 | 0 | T: ?Sized + Serialize, |
812 | | { |
813 | 0 | let value = tri!(value.serialize(ContentSerializer::<E>::new())); |
814 | 0 | self.elements.push(value); |
815 | 0 | Ok(()) |
816 | 0 | } |
817 | | |
818 | 0 | fn end(self) -> Result<Content, E> { |
819 | 0 | Ok(Content::Tuple(self.elements)) |
820 | 0 | } |
821 | | } |
822 | | |
823 | | pub struct SerializeTupleStruct<E> { |
824 | | name: &'static str, |
825 | | fields: Vec<Content>, |
826 | | error: PhantomData<E>, |
827 | | } |
828 | | |
829 | | #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] |
830 | | impl<E> ser::SerializeTupleStruct for SerializeTupleStruct<E> |
831 | | where |
832 | | E: ser::Error, |
833 | | { |
834 | | type Ok = Content; |
835 | | type Error = E; |
836 | | |
837 | 0 | fn serialize_field<T>(&mut self, value: &T) -> Result<(), E> |
838 | 0 | where |
839 | 0 | T: ?Sized + Serialize, |
840 | | { |
841 | 0 | let value = tri!(value.serialize(ContentSerializer::<E>::new())); |
842 | 0 | self.fields.push(value); |
843 | 0 | Ok(()) |
844 | 0 | } |
845 | | |
846 | 0 | fn end(self) -> Result<Content, E> { |
847 | 0 | Ok(Content::TupleStruct(self.name, self.fields)) |
848 | 0 | } |
849 | | } |
850 | | |
851 | | pub struct SerializeTupleVariant<E> { |
852 | | name: &'static str, |
853 | | variant_index: u32, |
854 | | variant: &'static str, |
855 | | fields: Vec<Content>, |
856 | | error: PhantomData<E>, |
857 | | } |
858 | | |
859 | | #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] |
860 | | impl<E> ser::SerializeTupleVariant for SerializeTupleVariant<E> |
861 | | where |
862 | | E: ser::Error, |
863 | | { |
864 | | type Ok = Content; |
865 | | type Error = E; |
866 | | |
867 | 0 | fn serialize_field<T>(&mut self, value: &T) -> Result<(), E> |
868 | 0 | where |
869 | 0 | T: ?Sized + Serialize, |
870 | | { |
871 | 0 | let value = tri!(value.serialize(ContentSerializer::<E>::new())); |
872 | 0 | self.fields.push(value); |
873 | 0 | Ok(()) |
874 | 0 | } |
875 | | |
876 | 0 | fn end(self) -> Result<Content, E> { |
877 | 0 | Ok(Content::TupleVariant( |
878 | 0 | self.name, |
879 | 0 | self.variant_index, |
880 | 0 | self.variant, |
881 | 0 | self.fields, |
882 | 0 | )) |
883 | 0 | } |
884 | | } |
885 | | |
886 | | pub struct SerializeMap<E> { |
887 | | entries: Vec<(Content, Content)>, |
888 | | key: Option<Content>, |
889 | | error: PhantomData<E>, |
890 | | } |
891 | | |
892 | | #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] |
893 | | impl<E> ser::SerializeMap for SerializeMap<E> |
894 | | where |
895 | | E: ser::Error, |
896 | | { |
897 | | type Ok = Content; |
898 | | type Error = E; |
899 | | |
900 | 0 | fn serialize_key<T>(&mut self, key: &T) -> Result<(), E> |
901 | 0 | where |
902 | 0 | T: ?Sized + Serialize, |
903 | | { |
904 | 0 | let key = tri!(key.serialize(ContentSerializer::<E>::new())); |
905 | 0 | self.key = Some(key); |
906 | 0 | Ok(()) |
907 | 0 | } |
908 | | |
909 | 0 | fn serialize_value<T>(&mut self, value: &T) -> Result<(), E> |
910 | 0 | where |
911 | 0 | T: ?Sized + Serialize, |
912 | | { |
913 | 0 | let key = self |
914 | 0 | .key |
915 | 0 | .take() |
916 | 0 | .expect("serialize_value called before serialize_key"); |
917 | 0 | let value = tri!(value.serialize(ContentSerializer::<E>::new())); |
918 | 0 | self.entries.push((key, value)); |
919 | 0 | Ok(()) |
920 | 0 | } |
921 | | |
922 | 0 | fn end(self) -> Result<Content, E> { |
923 | 0 | Ok(Content::Map(self.entries)) |
924 | 0 | } |
925 | | |
926 | 0 | fn serialize_entry<K, V>(&mut self, key: &K, value: &V) -> Result<(), E> |
927 | 0 | where |
928 | 0 | K: ?Sized + Serialize, |
929 | 0 | V: ?Sized + Serialize, |
930 | | { |
931 | 0 | let key = tri!(key.serialize(ContentSerializer::<E>::new())); |
932 | 0 | let value = tri!(value.serialize(ContentSerializer::<E>::new())); |
933 | 0 | self.entries.push((key, value)); |
934 | 0 | Ok(()) |
935 | 0 | } |
936 | | } |
937 | | |
938 | | pub struct SerializeStruct<E> { |
939 | | name: &'static str, |
940 | | fields: Vec<(&'static str, Content)>, |
941 | | error: PhantomData<E>, |
942 | | } |
943 | | |
944 | | #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] |
945 | | impl<E> ser::SerializeStruct for SerializeStruct<E> |
946 | | where |
947 | | E: ser::Error, |
948 | | { |
949 | | type Ok = Content; |
950 | | type Error = E; |
951 | | |
952 | 0 | fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), E> |
953 | 0 | where |
954 | 0 | T: ?Sized + Serialize, |
955 | | { |
956 | 0 | let value = tri!(value.serialize(ContentSerializer::<E>::new())); |
957 | 0 | self.fields.push((key, value)); |
958 | 0 | Ok(()) |
959 | 0 | } |
960 | | |
961 | 0 | fn end(self) -> Result<Content, E> { |
962 | 0 | Ok(Content::Struct(self.name, self.fields)) |
963 | 0 | } |
964 | | } |
965 | | |
966 | | pub struct SerializeStructVariant<E> { |
967 | | name: &'static str, |
968 | | variant_index: u32, |
969 | | variant: &'static str, |
970 | | fields: Vec<(&'static str, Content)>, |
971 | | error: PhantomData<E>, |
972 | | } |
973 | | |
974 | | #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] |
975 | | impl<E> ser::SerializeStructVariant for SerializeStructVariant<E> |
976 | | where |
977 | | E: ser::Error, |
978 | | { |
979 | | type Ok = Content; |
980 | | type Error = E; |
981 | | |
982 | 0 | fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), E> |
983 | 0 | where |
984 | 0 | T: ?Sized + Serialize, |
985 | | { |
986 | 0 | let value = tri!(value.serialize(ContentSerializer::<E>::new())); |
987 | 0 | self.fields.push((key, value)); |
988 | 0 | Ok(()) |
989 | 0 | } |
990 | | |
991 | 0 | fn end(self) -> Result<Content, E> { |
992 | 0 | Ok(Content::StructVariant( |
993 | 0 | self.name, |
994 | 0 | self.variant_index, |
995 | 0 | self.variant, |
996 | 0 | self.fields, |
997 | 0 | )) |
998 | 0 | } |
999 | | } |
1000 | | } |
1001 | | |
1002 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1003 | | pub struct FlatMapSerializer<'a, M: 'a>(pub &'a mut M); |
1004 | | |
1005 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1006 | | impl<'a, M> FlatMapSerializer<'a, M> |
1007 | | where |
1008 | | M: SerializeMap + 'a, |
1009 | | { |
1010 | 0 | fn bad_type(what: Unsupported) -> M::Error { |
1011 | 0 | ser::Error::custom(format_args!( |
1012 | 0 | "can only flatten structs and maps (got {})", |
1013 | | what |
1014 | | )) |
1015 | 0 | } |
1016 | | } |
1017 | | |
1018 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1019 | | #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] |
1020 | | impl<'a, M> Serializer for FlatMapSerializer<'a, M> |
1021 | | where |
1022 | | M: SerializeMap + 'a, |
1023 | | { |
1024 | | type Ok = (); |
1025 | | type Error = M::Error; |
1026 | | |
1027 | | type SerializeSeq = Impossible<Self::Ok, M::Error>; |
1028 | | type SerializeTuple = Impossible<Self::Ok, M::Error>; |
1029 | | type SerializeTupleStruct = Impossible<Self::Ok, M::Error>; |
1030 | | type SerializeMap = FlatMapSerializeMap<'a, M>; |
1031 | | type SerializeStruct = FlatMapSerializeStruct<'a, M>; |
1032 | | type SerializeTupleVariant = FlatMapSerializeTupleVariantAsMapValue<'a, M>; |
1033 | | type SerializeStructVariant = FlatMapSerializeStructVariantAsMapValue<'a, M>; |
1034 | | |
1035 | 0 | fn serialize_bool(self, _: bool) -> Result<Self::Ok, Self::Error> { |
1036 | 0 | Err(Self::bad_type(Unsupported::Boolean)) |
1037 | 0 | } |
1038 | | |
1039 | 0 | fn serialize_i8(self, _: i8) -> Result<Self::Ok, Self::Error> { |
1040 | 0 | Err(Self::bad_type(Unsupported::Integer)) |
1041 | 0 | } |
1042 | | |
1043 | 0 | fn serialize_i16(self, _: i16) -> Result<Self::Ok, Self::Error> { |
1044 | 0 | Err(Self::bad_type(Unsupported::Integer)) |
1045 | 0 | } |
1046 | | |
1047 | 0 | fn serialize_i32(self, _: i32) -> Result<Self::Ok, Self::Error> { |
1048 | 0 | Err(Self::bad_type(Unsupported::Integer)) |
1049 | 0 | } |
1050 | | |
1051 | 0 | fn serialize_i64(self, _: i64) -> Result<Self::Ok, Self::Error> { |
1052 | 0 | Err(Self::bad_type(Unsupported::Integer)) |
1053 | 0 | } |
1054 | | |
1055 | 0 | fn serialize_u8(self, _: u8) -> Result<Self::Ok, Self::Error> { |
1056 | 0 | Err(Self::bad_type(Unsupported::Integer)) |
1057 | 0 | } |
1058 | | |
1059 | 0 | fn serialize_u16(self, _: u16) -> Result<Self::Ok, Self::Error> { |
1060 | 0 | Err(Self::bad_type(Unsupported::Integer)) |
1061 | 0 | } |
1062 | | |
1063 | 0 | fn serialize_u32(self, _: u32) -> Result<Self::Ok, Self::Error> { |
1064 | 0 | Err(Self::bad_type(Unsupported::Integer)) |
1065 | 0 | } |
1066 | | |
1067 | 0 | fn serialize_u64(self, _: u64) -> Result<Self::Ok, Self::Error> { |
1068 | 0 | Err(Self::bad_type(Unsupported::Integer)) |
1069 | 0 | } |
1070 | | |
1071 | 0 | fn serialize_f32(self, _: f32) -> Result<Self::Ok, Self::Error> { |
1072 | 0 | Err(Self::bad_type(Unsupported::Float)) |
1073 | 0 | } |
1074 | | |
1075 | 0 | fn serialize_f64(self, _: f64) -> Result<Self::Ok, Self::Error> { |
1076 | 0 | Err(Self::bad_type(Unsupported::Float)) |
1077 | 0 | } |
1078 | | |
1079 | 0 | fn serialize_char(self, _: char) -> Result<Self::Ok, Self::Error> { |
1080 | 0 | Err(Self::bad_type(Unsupported::Char)) |
1081 | 0 | } |
1082 | | |
1083 | 0 | fn serialize_str(self, _: &str) -> Result<Self::Ok, Self::Error> { |
1084 | 0 | Err(Self::bad_type(Unsupported::String)) |
1085 | 0 | } |
1086 | | |
1087 | 0 | fn serialize_bytes(self, _: &[u8]) -> Result<Self::Ok, Self::Error> { |
1088 | 0 | Err(Self::bad_type(Unsupported::ByteArray)) |
1089 | 0 | } |
1090 | | |
1091 | 0 | fn serialize_none(self) -> Result<Self::Ok, Self::Error> { |
1092 | 0 | Ok(()) |
1093 | 0 | } |
1094 | | |
1095 | 0 | fn serialize_some<T>(self, value: &T) -> Result<Self::Ok, Self::Error> |
1096 | 0 | where |
1097 | 0 | T: ?Sized + Serialize, |
1098 | | { |
1099 | 0 | value.serialize(self) |
1100 | 0 | } |
1101 | | |
1102 | 0 | fn serialize_unit(self) -> Result<Self::Ok, Self::Error> { |
1103 | 0 | Ok(()) |
1104 | 0 | } |
1105 | | |
1106 | 0 | fn serialize_unit_struct(self, _: &'static str) -> Result<Self::Ok, Self::Error> { |
1107 | 0 | Ok(()) |
1108 | 0 | } |
1109 | | |
1110 | 0 | fn serialize_unit_variant( |
1111 | 0 | self, |
1112 | 0 | _: &'static str, |
1113 | 0 | _: u32, |
1114 | 0 | variant: &'static str, |
1115 | 0 | ) -> Result<Self::Ok, Self::Error> { |
1116 | 0 | self.0.serialize_entry(variant, &()) |
1117 | 0 | } |
1118 | | |
1119 | 0 | fn serialize_newtype_struct<T>( |
1120 | 0 | self, |
1121 | 0 | _: &'static str, |
1122 | 0 | value: &T, |
1123 | 0 | ) -> Result<Self::Ok, Self::Error> |
1124 | 0 | where |
1125 | 0 | T: ?Sized + Serialize, |
1126 | | { |
1127 | 0 | value.serialize(self) |
1128 | 0 | } |
1129 | | |
1130 | 0 | fn serialize_newtype_variant<T>( |
1131 | 0 | self, |
1132 | 0 | _: &'static str, |
1133 | 0 | _: u32, |
1134 | 0 | variant: &'static str, |
1135 | 0 | value: &T, |
1136 | 0 | ) -> Result<Self::Ok, Self::Error> |
1137 | 0 | where |
1138 | 0 | T: ?Sized + Serialize, |
1139 | | { |
1140 | 0 | self.0.serialize_entry(variant, value) |
1141 | 0 | } |
1142 | | |
1143 | 0 | fn serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> { |
1144 | 0 | Err(Self::bad_type(Unsupported::Sequence)) |
1145 | 0 | } |
1146 | | |
1147 | 0 | fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple, Self::Error> { |
1148 | 0 | Err(Self::bad_type(Unsupported::Tuple)) |
1149 | 0 | } |
1150 | | |
1151 | 0 | fn serialize_tuple_struct( |
1152 | 0 | self, |
1153 | 0 | _: &'static str, |
1154 | 0 | _: usize, |
1155 | 0 | ) -> Result<Self::SerializeTupleStruct, Self::Error> { |
1156 | 0 | Err(Self::bad_type(Unsupported::TupleStruct)) |
1157 | 0 | } |
1158 | | |
1159 | 0 | fn serialize_tuple_variant( |
1160 | 0 | self, |
1161 | 0 | _: &'static str, |
1162 | 0 | _: u32, |
1163 | 0 | variant: &'static str, |
1164 | 0 | _: usize, |
1165 | 0 | ) -> Result<Self::SerializeTupleVariant, Self::Error> { |
1166 | 0 | tri!(self.0.serialize_key(variant)); |
1167 | 0 | Ok(FlatMapSerializeTupleVariantAsMapValue::new(self.0)) |
1168 | 0 | } |
1169 | | |
1170 | 0 | fn serialize_map(self, _: Option<usize>) -> Result<Self::SerializeMap, Self::Error> { |
1171 | 0 | Ok(FlatMapSerializeMap(self.0)) |
1172 | 0 | } |
1173 | | |
1174 | 0 | fn serialize_struct( |
1175 | 0 | self, |
1176 | 0 | _: &'static str, |
1177 | 0 | _: usize, |
1178 | 0 | ) -> Result<Self::SerializeStruct, Self::Error> { |
1179 | 0 | Ok(FlatMapSerializeStruct(self.0)) |
1180 | 0 | } |
1181 | | |
1182 | 0 | fn serialize_struct_variant( |
1183 | 0 | self, |
1184 | 0 | _: &'static str, |
1185 | 0 | _: u32, |
1186 | 0 | inner_variant: &'static str, |
1187 | 0 | _: usize, |
1188 | 0 | ) -> Result<Self::SerializeStructVariant, Self::Error> { |
1189 | 0 | tri!(self.0.serialize_key(inner_variant)); |
1190 | 0 | Ok(FlatMapSerializeStructVariantAsMapValue::new( |
1191 | 0 | self.0, |
1192 | 0 | inner_variant, |
1193 | 0 | )) |
1194 | 0 | } |
1195 | | } |
1196 | | |
1197 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1198 | | pub struct FlatMapSerializeMap<'a, M: 'a>(&'a mut M); |
1199 | | |
1200 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1201 | | #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] |
1202 | | impl<'a, M> ser::SerializeMap for FlatMapSerializeMap<'a, M> |
1203 | | where |
1204 | | M: SerializeMap + 'a, |
1205 | | { |
1206 | | type Ok = (); |
1207 | | type Error = M::Error; |
1208 | | |
1209 | 0 | fn serialize_key<T>(&mut self, key: &T) -> Result<(), Self::Error> |
1210 | 0 | where |
1211 | 0 | T: ?Sized + Serialize, |
1212 | | { |
1213 | 0 | self.0.serialize_key(key) |
1214 | 0 | } |
1215 | | |
1216 | 0 | fn serialize_value<T>(&mut self, value: &T) -> Result<(), Self::Error> |
1217 | 0 | where |
1218 | 0 | T: ?Sized + Serialize, |
1219 | | { |
1220 | 0 | self.0.serialize_value(value) |
1221 | 0 | } |
1222 | | |
1223 | 0 | fn serialize_entry<K, V>(&mut self, key: &K, value: &V) -> Result<(), Self::Error> |
1224 | 0 | where |
1225 | 0 | K: ?Sized + Serialize, |
1226 | 0 | V: ?Sized + Serialize, |
1227 | | { |
1228 | 0 | self.0.serialize_entry(key, value) |
1229 | 0 | } |
1230 | | |
1231 | 0 | fn end(self) -> Result<(), Self::Error> { |
1232 | 0 | Ok(()) |
1233 | 0 | } |
1234 | | } |
1235 | | |
1236 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1237 | | pub struct FlatMapSerializeStruct<'a, M: 'a>(&'a mut M); |
1238 | | |
1239 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1240 | | #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] |
1241 | | impl<'a, M> ser::SerializeStruct for FlatMapSerializeStruct<'a, M> |
1242 | | where |
1243 | | M: SerializeMap + 'a, |
1244 | | { |
1245 | | type Ok = (); |
1246 | | type Error = M::Error; |
1247 | | |
1248 | 0 | fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> |
1249 | 0 | where |
1250 | 0 | T: ?Sized + Serialize, |
1251 | | { |
1252 | 0 | self.0.serialize_entry(key, value) |
1253 | 0 | } |
1254 | | |
1255 | 0 | fn end(self) -> Result<(), Self::Error> { |
1256 | 0 | Ok(()) |
1257 | 0 | } |
1258 | | } |
1259 | | |
1260 | | //////////////////////////////////////////////////////////////////////////////////////////////////// |
1261 | | |
1262 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1263 | | pub struct FlatMapSerializeTupleVariantAsMapValue<'a, M: 'a> { |
1264 | | map: &'a mut M, |
1265 | | fields: Vec<Content>, |
1266 | | } |
1267 | | |
1268 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1269 | | impl<'a, M> FlatMapSerializeTupleVariantAsMapValue<'a, M> |
1270 | | where |
1271 | | M: SerializeMap + 'a, |
1272 | | { |
1273 | 0 | fn new(map: &'a mut M) -> Self { |
1274 | 0 | FlatMapSerializeTupleVariantAsMapValue { |
1275 | 0 | map, |
1276 | 0 | fields: Vec::new(), |
1277 | 0 | } |
1278 | 0 | } |
1279 | | } |
1280 | | |
1281 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1282 | | #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] |
1283 | | impl<'a, M> ser::SerializeTupleVariant for FlatMapSerializeTupleVariantAsMapValue<'a, M> |
1284 | | where |
1285 | | M: SerializeMap + 'a, |
1286 | | { |
1287 | | type Ok = (); |
1288 | | type Error = M::Error; |
1289 | | |
1290 | 0 | fn serialize_field<T>(&mut self, value: &T) -> Result<(), Self::Error> |
1291 | 0 | where |
1292 | 0 | T: ?Sized + Serialize, |
1293 | | { |
1294 | 0 | let value = tri!(value.serialize(ContentSerializer::<M::Error>::new())); |
1295 | 0 | self.fields.push(value); |
1296 | 0 | Ok(()) |
1297 | 0 | } |
1298 | | |
1299 | 0 | fn end(self) -> Result<(), Self::Error> { |
1300 | 0 | tri!(self.map.serialize_value(&Content::Seq(self.fields))); |
1301 | 0 | Ok(()) |
1302 | 0 | } |
1303 | | } |
1304 | | |
1305 | | //////////////////////////////////////////////////////////////////////////////////////////////////// |
1306 | | |
1307 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1308 | | pub struct FlatMapSerializeStructVariantAsMapValue<'a, M: 'a> { |
1309 | | map: &'a mut M, |
1310 | | name: &'static str, |
1311 | | fields: Vec<(&'static str, Content)>, |
1312 | | } |
1313 | | |
1314 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1315 | | impl<'a, M> FlatMapSerializeStructVariantAsMapValue<'a, M> |
1316 | | where |
1317 | | M: SerializeMap + 'a, |
1318 | | { |
1319 | 0 | fn new(map: &'a mut M, name: &'static str) -> FlatMapSerializeStructVariantAsMapValue<'a, M> { |
1320 | 0 | FlatMapSerializeStructVariantAsMapValue { |
1321 | 0 | map, |
1322 | 0 | name, |
1323 | 0 | fields: Vec::new(), |
1324 | 0 | } |
1325 | 0 | } |
1326 | | } |
1327 | | |
1328 | | #[cfg(any(feature = "std", feature = "alloc"))] |
1329 | | #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] |
1330 | | impl<'a, M> ser::SerializeStructVariant for FlatMapSerializeStructVariantAsMapValue<'a, M> |
1331 | | where |
1332 | | M: SerializeMap + 'a, |
1333 | | { |
1334 | | type Ok = (); |
1335 | | type Error = M::Error; |
1336 | | |
1337 | 0 | fn serialize_field<T>(&mut self, key: &'static str, value: &T) -> Result<(), Self::Error> |
1338 | 0 | where |
1339 | 0 | T: ?Sized + Serialize, |
1340 | | { |
1341 | 0 | let value = tri!(value.serialize(ContentSerializer::<M::Error>::new())); |
1342 | 0 | self.fields.push((key, value)); |
1343 | 0 | Ok(()) |
1344 | 0 | } |
1345 | | |
1346 | 0 | fn end(self) -> Result<(), Self::Error> { |
1347 | 0 | tri!(self |
1348 | 0 | .map |
1349 | 0 | .serialize_value(&Content::Struct(self.name, self.fields))); |
1350 | 0 | Ok(()) |
1351 | 0 | } |
1352 | | } |
1353 | | |
1354 | | pub struct AdjacentlyTaggedEnumVariant { |
1355 | | pub enum_name: &'static str, |
1356 | | pub variant_index: u32, |
1357 | | pub variant_name: &'static str, |
1358 | | } |
1359 | | |
1360 | | #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] |
1361 | | impl Serialize for AdjacentlyTaggedEnumVariant { |
1362 | 0 | fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
1363 | 0 | where |
1364 | 0 | S: Serializer, |
1365 | | { |
1366 | 0 | serializer.serialize_unit_variant(self.enum_name, self.variant_index, self.variant_name) |
1367 | 0 | } |
1368 | | } |
1369 | | |
1370 | | // Error when Serialize for a non_exhaustive remote enum encounters a variant |
1371 | | // that is not recognized. |
1372 | | pub struct CannotSerializeVariant<T>(pub T); |
1373 | | |
1374 | | #[cfg_attr(not(no_diagnostic_namespace), diagnostic::do_not_recommend)] |
1375 | | impl<T> Display for CannotSerializeVariant<T> |
1376 | | where |
1377 | | T: Debug, |
1378 | | { |
1379 | 0 | fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { |
1380 | 0 | write!(formatter, "enum variant cannot be serialized: {:?}", self.0) |
1381 | 0 | } |
1382 | | } |