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