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