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