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