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