1 // Copyright 2017 Serde Developers
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
11 use de
::{Deserialize, Deserializer, IntoDeserializer, Error, Visitor}
;
13 #[cfg(any(feature = "std", feature = "alloc"))]
16 #[cfg(any(feature = "std", feature = "alloc"))]
17 pub use self::content
::{Content
, ContentRefDeserializer
, ContentDeserializer
,
18 TaggedContentVisitor
, TagOrContentField
, TagOrContentFieldVisitor
,
19 TagContentOtherField
, TagContentOtherFieldVisitor
,
20 InternallyTaggedUnitVisitor
, UntaggedUnitVisitor
};
22 /// If the missing field is of type `Option<T>` then treat is as `None`,
23 /// otherwise it is an error.
24 pub fn missing_field
<'de
, V
, E
>(field
: &'
static str) -> Result
<V
, E
>
29 struct MissingFieldDeserializer
<E
>(&'
static str, PhantomData
<E
>);
31 impl<'de
, E
> Deserializer
<'de
> for MissingFieldDeserializer
<E
>
37 fn deserialize_any
<V
>(self, _visitor
: V
) -> Result
<V
::Value
, E
>
41 Err(Error
::missing_field(self.0))
44 fn deserialize_option
<V
>(self, visitor
: V
) -> Result
<V
::Value
, E
>
51 forward_to_deserialize_any
! {
52 bool
i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
53 byte_buf unit unit_struct newtype_struct seq tuple tuple_struct map
54 struct enum identifier ignored_any
58 let deserializer
= MissingFieldDeserializer(field
, PhantomData
);
59 Deserialize
::deserialize(deserializer
)
62 #[cfg(any(feature = "std", feature = "alloc"))]
63 pub fn borrow_cow_str
<'de
: 'a
, 'a
, D
>(deserializer
: D
) -> Result
<Cow
<'a
, str>, D
::Error
>
69 impl<'a
> Visitor
<'a
> for CowStrVisitor
{
70 type Value
= Cow
<'a
, str>;
72 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
73 formatter
.write_str("a string")
76 fn visit_str
<E
>(self, v
: &str) -> Result
<Self::Value
, E
>
80 Ok(Cow
::Owned(v
.to_owned()))
83 fn visit_borrowed_str
<E
>(self, v
: &'a
str) -> Result
<Self::Value
, E
>
90 fn visit_string
<E
>(self, v
: String
) -> Result
<Self::Value
, E
>
97 fn visit_bytes
<E
>(self, v
: &[u8]) -> Result
<Self::Value
, E
>
101 match str::from_utf8(v
) {
102 Ok(s
) => Ok(Cow
::Owned(s
.to_owned())),
103 Err(_
) => Err(Error
::invalid_value(Unexpected
::Bytes(v
), &self)),
107 fn visit_borrowed_bytes
<E
>(self, v
: &'a
[u8]) -> Result
<Self::Value
, E
>
111 match str::from_utf8(v
) {
112 Ok(s
) => Ok(Cow
::Borrowed(s
)),
113 Err(_
) => Err(Error
::invalid_value(Unexpected
::Bytes(v
), &self)),
117 fn visit_byte_buf
<E
>(self, v
: Vec
<u8>) -> Result
<Self::Value
, E
>
121 match String
::from_utf8(v
) {
122 Ok(s
) => Ok(Cow
::Owned(s
)),
123 Err(e
) => Err(Error
::invalid_value(Unexpected
::Bytes(&e
.into_bytes()), &self),),
128 deserializer
.deserialize_str(CowStrVisitor
)
131 #[cfg(any(feature = "std", feature = "alloc"))]
132 pub fn borrow_cow_bytes
<'de
: 'a
, 'a
, D
>(deserializer
: D
) -> Result
<Cow
<'a
, [u8]>, D
::Error
>
134 D
: Deserializer
<'de
>,
136 struct CowBytesVisitor
;
138 impl<'a
> Visitor
<'a
> for CowBytesVisitor
{
139 type Value
= Cow
<'a
, [u8]>;
141 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
142 formatter
.write_str("a byte array")
145 fn visit_str
<E
>(self, v
: &str) -> Result
<Self::Value
, E
>
149 Ok(Cow
::Owned(v
.as_bytes().to_vec()))
152 fn visit_borrowed_str
<E
>(self, v
: &'a
str) -> Result
<Self::Value
, E
>
156 Ok(Cow
::Borrowed(v
.as_bytes()))
159 fn visit_string
<E
>(self, v
: String
) -> Result
<Self::Value
, E
>
163 Ok(Cow
::Owned(v
.into_bytes()))
166 fn visit_bytes
<E
>(self, v
: &[u8]) -> Result
<Self::Value
, E
>
170 Ok(Cow
::Owned(v
.to_vec()))
173 fn visit_borrowed_bytes
<E
>(self, v
: &'a
[u8]) -> Result
<Self::Value
, E
>
180 fn visit_byte_buf
<E
>(self, v
: Vec
<u8>) -> Result
<Self::Value
, E
>
188 deserializer
.deserialize_str(CowBytesVisitor
)
194 pub fn from_bounds
<I
>(iter
: &I
) -> Option
<usize>
198 helper(iter
.size_hint())
201 pub fn cautious(hint
: Option
<usize>) -> usize {
202 cmp
::min(hint
.unwrap_or(0), 4096)
205 fn helper(bounds
: (usize, Option
<usize>)) -> Option
<usize> {
207 (lower
, Some(upper
)) if lower
== upper
=> Some(upper
),
213 #[cfg(any(feature = "std", feature = "alloc"))]
215 // This module is private and nothing here should be used outside of
218 // We will iterate on the implementation for a few releases and only have to
219 // worry about backward compatibility for the `untagged` and `tag` attributes
220 // rather than for this entire mechanism.
222 // This issue is tracking making some of this stuff public:
223 // https://github.com/serde-rs/serde/issues/741
227 use de
::{self, Deserialize
, DeserializeSeed
, Deserializer
, Visitor
, SeqAccess
, MapAccess
,
228 EnumAccess
, Unexpected
};
229 use super::size_hint
;
231 /// Used from generated code to buffer the contents of the Deserializer when
232 /// deserializing untagged enums and internally tagged enums.
234 /// Not public API. Use serde-value instead.
236 pub enum Content
<'de
> {
259 Some(Box
<Content
<'de
>>),
262 Newtype(Box
<Content
<'de
>>),
263 Seq(Vec
<Content
<'de
>>),
264 Map(Vec
<(Content
<'de
>, Content
<'de
>)>),
267 impl<'de
> Content
<'de
> {
268 fn unexpected(&self) -> Unexpected
{
270 Content
::Bool(b
) => Unexpected
::Bool(b
),
271 Content
::U8(n
) => Unexpected
::Unsigned(n
as u64),
272 Content
::U16(n
) => Unexpected
::Unsigned(n
as u64),
273 Content
::U32(n
) => Unexpected
::Unsigned(n
as u64),
274 Content
::U64(n
) => Unexpected
::Unsigned(n
),
275 Content
::I8(n
) => Unexpected
::Signed(n
as i64),
276 Content
::I16(n
) => Unexpected
::Signed(n
as i64),
277 Content
::I32(n
) => Unexpected
::Signed(n
as i64),
278 Content
::I64(n
) => Unexpected
::Signed(n
),
279 Content
::F32(f
) => Unexpected
::Float(f
as f64),
280 Content
::F64(f
) => Unexpected
::Float(f
),
281 Content
::Char(c
) => Unexpected
::Char(c
),
282 Content
::String(ref s
) => Unexpected
::Str(s
),
283 Content
::Str(s
) => Unexpected
::Str(s
),
284 Content
::ByteBuf(ref b
) => Unexpected
::Bytes(b
),
285 Content
::Bytes(b
) => Unexpected
::Bytes(b
),
286 Content
::None
| Content
::Some(_
) => Unexpected
::Option
,
287 Content
::Unit
=> Unexpected
::Unit
,
288 Content
::Newtype(_
) => Unexpected
::NewtypeStruct
,
289 Content
::Seq(_
) => Unexpected
::Seq
,
290 Content
::Map(_
) => Unexpected
::Map
,
295 impl<'de
> Deserialize
<'de
> for Content
<'de
> {
296 fn deserialize
<D
>(deserializer
: D
) -> Result
<Self, D
::Error
>
298 D
: Deserializer
<'de
>,
300 // Untagged and internally tagged enums are only supported in
301 // self-describing formats.
302 let visitor
= ContentVisitor { value: PhantomData }
;
303 deserializer
.deserialize_any(visitor
)
307 struct ContentVisitor
<'de
> {
308 value
: PhantomData
<Content
<'de
>>,
311 impl<'de
> ContentVisitor
<'de
> {
313 ContentVisitor { value: PhantomData }
317 impl<'de
> Visitor
<'de
> for ContentVisitor
<'de
> {
318 type Value
= Content
<'de
>;
320 fn expecting(&self, fmt
: &mut fmt
::Formatter
) -> fmt
::Result
{
321 fmt
.write_str("any value")
324 fn visit_bool
<F
>(self, value
: bool
) -> Result
<Self::Value
, F
>
328 Ok(Content
::Bool(value
))
331 fn visit_i8
<F
>(self, value
: i8) -> Result
<Self::Value
, F
>
335 Ok(Content
::I8(value
))
338 fn visit_i16
<F
>(self, value
: i16) -> Result
<Self::Value
, F
>
342 Ok(Content
::I16(value
))
345 fn visit_i32
<F
>(self, value
: i32) -> Result
<Self::Value
, F
>
349 Ok(Content
::I32(value
))
352 fn visit_i64
<F
>(self, value
: i64) -> Result
<Self::Value
, F
>
356 Ok(Content
::I64(value
))
359 fn visit_u8
<F
>(self, value
: u8) -> Result
<Self::Value
, F
>
363 Ok(Content
::U8(value
))
366 fn visit_u16
<F
>(self, value
: u16) -> Result
<Self::Value
, F
>
370 Ok(Content
::U16(value
))
373 fn visit_u32
<F
>(self, value
: u32) -> Result
<Self::Value
, F
>
377 Ok(Content
::U32(value
))
380 fn visit_u64
<F
>(self, value
: u64) -> Result
<Self::Value
, F
>
384 Ok(Content
::U64(value
))
387 fn visit_f32
<F
>(self, value
: f32) -> Result
<Self::Value
, F
>
391 Ok(Content
::F32(value
))
394 fn visit_f64
<F
>(self, value
: f64) -> Result
<Self::Value
, F
>
398 Ok(Content
::F64(value
))
401 fn visit_char
<F
>(self, value
: char) -> Result
<Self::Value
, F
>
405 Ok(Content
::Char(value
))
408 fn visit_str
<F
>(self, value
: &str) -> Result
<Self::Value
, F
>
412 Ok(Content
::String(value
.into()))
415 fn visit_borrowed_str
<F
>(self, value
: &'de
str) -> Result
<Self::Value
, F
>
419 Ok(Content
::Str(value
))
422 fn visit_string
<F
>(self, value
: String
) -> Result
<Self::Value
, F
>
426 Ok(Content
::String(value
))
429 fn visit_bytes
<F
>(self, value
: &[u8]) -> Result
<Self::Value
, F
>
433 Ok(Content
::ByteBuf(value
.into()))
436 fn visit_borrowed_bytes
<F
>(self, value
: &'de
[u8]) -> Result
<Self::Value
, F
>
440 Ok(Content
::Bytes(value
))
443 fn visit_byte_buf
<F
>(self, value
: Vec
<u8>) -> Result
<Self::Value
, F
>
447 Ok(Content
::ByteBuf(value
))
450 fn visit_unit
<F
>(self) -> Result
<Self::Value
, F
>
457 fn visit_none
<F
>(self) -> Result
<Self::Value
, F
>
464 fn visit_some
<D
>(self, deserializer
: D
) -> Result
<Self::Value
, D
::Error
>
466 D
: Deserializer
<'de
>,
468 Deserialize
::deserialize(deserializer
).map(|v
| Content
::Some(Box
::new(v
)))
471 fn visit_newtype_struct
<D
>(self, deserializer
: D
) -> Result
<Self::Value
, D
::Error
>
473 D
: Deserializer
<'de
>,
475 Deserialize
::deserialize(deserializer
).map(|v
| Content
::Newtype(Box
::new(v
)))
478 fn visit_seq
<V
>(self, mut visitor
: V
) -> Result
<Self::Value
, V
::Error
>
482 let mut vec
= Vec
::with_capacity(size_hint
::cautious(visitor
.size_hint()));
483 while let Some(e
) = try
!(visitor
.next_element()) {
486 Ok(Content
::Seq(vec
))
489 fn visit_map
<V
>(self, mut visitor
: V
) -> Result
<Self::Value
, V
::Error
>
493 let mut vec
= Vec
::with_capacity(size_hint
::cautious(visitor
.size_hint()));
494 while let Some(kv
) = try
!(visitor
.next_entry()) {
497 Ok(Content
::Map(vec
))
500 fn visit_enum
<V
>(self, _visitor
: V
) -> Result
<Self::Value
, V
::Error
>
504 Err(de
::Error
::custom("untagged and internally tagged enums do not support enum input",),)
508 /// This is the type of the map keys in an internally tagged enum.
511 pub enum TagOrContent
<'de
> {
513 Content(Content
<'de
>),
516 struct TagOrContentVisitor
<'de
> {
518 value
: PhantomData
<TagOrContent
<'de
>>,
521 impl<'de
> TagOrContentVisitor
<'de
> {
522 fn new(name
: &'
static str) -> Self {
523 TagOrContentVisitor { name: name, value: PhantomData }
527 impl<'de
> DeserializeSeed
<'de
> for TagOrContentVisitor
<'de
> {
528 type Value
= TagOrContent
<'de
>;
530 fn deserialize
<D
>(self, deserializer
: D
) -> Result
<Self::Value
, D
::Error
>
532 D
: Deserializer
<'de
>,
534 // Internally tagged enums are only supported in self-describing
536 deserializer
.deserialize_any(self)
540 impl<'de
> Visitor
<'de
> for TagOrContentVisitor
<'de
> {
541 type Value
= TagOrContent
<'de
>;
543 fn expecting(&self, fmt
: &mut fmt
::Formatter
) -> fmt
::Result
{
544 write
!(fmt
, "a type tag `{}` or any other value", self.name
)
547 fn visit_bool
<F
>(self, value
: bool
) -> Result
<Self::Value
, F
>
551 ContentVisitor
::new()
553 .map(TagOrContent
::Content
)
556 fn visit_i8
<F
>(self, value
: i8) -> Result
<Self::Value
, F
>
560 ContentVisitor
::new().visit_i8(value
).map(TagOrContent
::Content
)
563 fn visit_i16
<F
>(self, value
: i16) -> Result
<Self::Value
, F
>
567 ContentVisitor
::new()
569 .map(TagOrContent
::Content
)
572 fn visit_i32
<F
>(self, value
: i32) -> Result
<Self::Value
, F
>
576 ContentVisitor
::new()
578 .map(TagOrContent
::Content
)
581 fn visit_i64
<F
>(self, value
: i64) -> Result
<Self::Value
, F
>
585 ContentVisitor
::new()
587 .map(TagOrContent
::Content
)
590 fn visit_u8
<F
>(self, value
: u8) -> Result
<Self::Value
, F
>
594 ContentVisitor
::new().visit_u8(value
).map(TagOrContent
::Content
)
597 fn visit_u16
<F
>(self, value
: u16) -> Result
<Self::Value
, F
>
601 ContentVisitor
::new()
603 .map(TagOrContent
::Content
)
606 fn visit_u32
<F
>(self, value
: u32) -> Result
<Self::Value
, F
>
610 ContentVisitor
::new()
612 .map(TagOrContent
::Content
)
615 fn visit_u64
<F
>(self, value
: u64) -> Result
<Self::Value
, F
>
619 ContentVisitor
::new()
621 .map(TagOrContent
::Content
)
624 fn visit_f32
<F
>(self, value
: f32) -> Result
<Self::Value
, F
>
628 ContentVisitor
::new()
630 .map(TagOrContent
::Content
)
633 fn visit_f64
<F
>(self, value
: f64) -> Result
<Self::Value
, F
>
637 ContentVisitor
::new()
639 .map(TagOrContent
::Content
)
642 fn visit_char
<F
>(self, value
: char) -> Result
<Self::Value
, F
>
646 ContentVisitor
::new()
648 .map(TagOrContent
::Content
)
651 fn visit_str
<F
>(self, value
: &str) -> Result
<Self::Value
, F
>
655 if value
== self.name
{
656 Ok(TagOrContent
::Tag
)
658 ContentVisitor
::new()
660 .map(TagOrContent
::Content
)
664 fn visit_borrowed_str
<F
>(self, value
: &'de
str) -> Result
<Self::Value
, F
>
668 if value
== self.name
{
669 Ok(TagOrContent
::Tag
)
671 ContentVisitor
::new()
672 .visit_borrowed_str(value
)
673 .map(TagOrContent
::Content
)
677 fn visit_string
<F
>(self, value
: String
) -> Result
<Self::Value
, F
>
681 if value
== self.name
{
682 Ok(TagOrContent
::Tag
)
684 ContentVisitor
::new()
686 .map(TagOrContent
::Content
)
690 fn visit_bytes
<F
>(self, value
: &[u8]) -> Result
<Self::Value
, F
>
694 if value
== self.name
.as_bytes() {
695 Ok(TagOrContent
::Tag
)
697 ContentVisitor
::new()
699 .map(TagOrContent
::Content
)
703 fn visit_borrowed_bytes
<F
>(self, value
: &'de
[u8]) -> Result
<Self::Value
, F
>
707 if value
== self.name
.as_bytes() {
708 Ok(TagOrContent
::Tag
)
710 ContentVisitor
::new()
711 .visit_borrowed_bytes(value
)
712 .map(TagOrContent
::Content
)
716 fn visit_byte_buf
<F
>(self, value
: Vec
<u8>) -> Result
<Self::Value
, F
>
720 if value
== self.name
.as_bytes() {
721 Ok(TagOrContent
::Tag
)
723 ContentVisitor
::new()
724 .visit_byte_buf(value
)
725 .map(TagOrContent
::Content
)
729 fn visit_unit
<F
>(self) -> Result
<Self::Value
, F
>
733 ContentVisitor
::new().visit_unit().map(TagOrContent
::Content
)
736 fn visit_none
<F
>(self) -> Result
<Self::Value
, F
>
740 ContentVisitor
::new().visit_none().map(TagOrContent
::Content
)
743 fn visit_some
<D
>(self, deserializer
: D
) -> Result
<Self::Value
, D
::Error
>
745 D
: Deserializer
<'de
>,
747 ContentVisitor
::new()
748 .visit_some(deserializer
)
749 .map(TagOrContent
::Content
)
752 fn visit_newtype_struct
<D
>(self, deserializer
: D
) -> Result
<Self::Value
, D
::Error
>
754 D
: Deserializer
<'de
>,
756 ContentVisitor
::new()
757 .visit_newtype_struct(deserializer
)
758 .map(TagOrContent
::Content
)
761 fn visit_seq
<V
>(self, visitor
: V
) -> Result
<Self::Value
, V
::Error
>
765 ContentVisitor
::new()
767 .map(TagOrContent
::Content
)
770 fn visit_map
<V
>(self, visitor
: V
) -> Result
<Self::Value
, V
::Error
>
774 ContentVisitor
::new()
776 .map(TagOrContent
::Content
)
779 fn visit_enum
<V
>(self, visitor
: V
) -> Result
<Self::Value
, V
::Error
>
783 ContentVisitor
::new()
785 .map(TagOrContent
::Content
)
789 /// Used by generated code to deserialize an internally tagged enum.
792 pub struct TaggedContent
<'de
, T
> {
794 pub content
: Content
<'de
>,
798 pub struct TaggedContentVisitor
<'de
, T
> {
799 tag_name
: &'
static str,
800 value
: PhantomData
<TaggedContent
<'de
, T
>>,
803 impl<'de
, T
> TaggedContentVisitor
<'de
, T
> {
804 /// Visitor for the content of an internally tagged enum with the given tag
806 pub fn new(name
: &'
static str) -> Self {
807 TaggedContentVisitor
{
814 impl<'de
, T
> DeserializeSeed
<'de
> for TaggedContentVisitor
<'de
, T
>
818 type Value
= TaggedContent
<'de
, T
>;
820 fn deserialize
<D
>(self, deserializer
: D
) -> Result
<Self::Value
, D
::Error
>
822 D
: Deserializer
<'de
>,
824 // Internally tagged enums are only supported in self-describing
826 deserializer
.deserialize_any(self)
830 impl<'de
, T
> Visitor
<'de
> for TaggedContentVisitor
<'de
, T
>
834 type Value
= TaggedContent
<'de
, T
>;
836 fn expecting(&self, fmt
: &mut fmt
::Formatter
) -> fmt
::Result
{
837 fmt
.write_str("internally tagged enum")
840 fn visit_seq
<S
>(self, mut seq
: S
) -> Result
<Self::Value
, S
::Error
>
844 let tag
= match try
!(seq
.next_element()) {
847 return Err(de
::Error
::missing_field(self.tag_name
));
850 let rest
= de
::value
::SeqAccessDeserializer
::new(seq
);
853 content
: try
!(Content
::deserialize(rest
)),
857 fn visit_map
<M
>(self, mut map
: M
) -> Result
<Self::Value
, M
::Error
>
862 let mut vec
= Vec
::with_capacity(size_hint
::cautious(map
.size_hint()));
864 try
!(map
.next_key_seed(TagOrContentVisitor
::new(self.tag_name
))) {
866 TagOrContent
::Tag
=> {
868 return Err(de
::Error
::duplicate_field(self.tag_name
));
870 tag
= Some(try
!(map
.next_value()));
872 TagOrContent
::Content(k
) => {
873 let v
= try
!(map
.next_value());
879 None
=> Err(de
::Error
::missing_field(self.tag_name
)),
884 content
: Content
::Map(vec
),
892 /// Used by generated code to deserialize an adjacently tagged enum.
895 pub enum TagOrContentField
{
901 pub struct TagOrContentFieldVisitor
{
902 pub tag
: &'
static str,
903 pub content
: &'
static str,
906 impl<'de
> DeserializeSeed
<'de
> for TagOrContentFieldVisitor
{
907 type Value
= TagOrContentField
;
909 fn deserialize
<D
>(self, deserializer
: D
) -> Result
<Self::Value
, D
::Error
>
911 D
: Deserializer
<'de
>,
913 deserializer
.deserialize_str(self)
917 impl<'de
> Visitor
<'de
> for TagOrContentFieldVisitor
{
918 type Value
= TagOrContentField
;
920 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
921 write
!(formatter
, "{:?} or {:?}", self.tag
, self.content
)
924 fn visit_str
<E
>(self, field
: &str) -> Result
<Self::Value
, E
>
928 if field
== self.tag
{
929 Ok(TagOrContentField
::Tag
)
930 } else if field
== self.content
{
931 Ok(TagOrContentField
::Content
)
933 Err(de
::Error
::invalid_value(Unexpected
::Str(field
), &self))
938 /// Used by generated code to deserialize an adjacently tagged enum when
939 /// ignoring unrelated fields is allowed.
942 pub enum TagContentOtherField
{
949 pub struct TagContentOtherFieldVisitor
{
950 pub tag
: &'
static str,
951 pub content
: &'
static str,
954 impl<'de
> DeserializeSeed
<'de
> for TagContentOtherFieldVisitor
{
955 type Value
= TagContentOtherField
;
957 fn deserialize
<D
>(self, deserializer
: D
) -> Result
<Self::Value
, D
::Error
>
959 D
: Deserializer
<'de
>,
961 deserializer
.deserialize_str(self)
965 impl<'de
> Visitor
<'de
> for TagContentOtherFieldVisitor
{
966 type Value
= TagContentOtherField
;
968 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
969 write
!(formatter
, "{:?}, {:?}, or other ignored fields", self.tag
, self.content
)
972 fn visit_str
<E
>(self, field
: &str) -> Result
<Self::Value
, E
>
976 if field
== self.tag
{
977 Ok(TagContentOtherField
::Tag
)
978 } else if field
== self.content
{
979 Ok(TagContentOtherField
::Content
)
981 Ok(TagContentOtherField
::Other
)
987 pub struct ContentDeserializer
<'de
, E
> {
988 content
: Content
<'de
>,
992 /// Used when deserializing an internally tagged enum because the content will
993 /// be used exactly once.
994 impl<'de
, E
> Deserializer
<'de
> for ContentDeserializer
<'de
, E
>
1000 fn deserialize_any
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1004 match self.content
{
1005 Content
::Bool(v
) => visitor
.visit_bool(v
),
1006 Content
::U8(v
) => visitor
.visit_u8(v
),
1007 Content
::U16(v
) => visitor
.visit_u16(v
),
1008 Content
::U32(v
) => visitor
.visit_u32(v
),
1009 Content
::U64(v
) => visitor
.visit_u64(v
),
1010 Content
::I8(v
) => visitor
.visit_i8(v
),
1011 Content
::I16(v
) => visitor
.visit_i16(v
),
1012 Content
::I32(v
) => visitor
.visit_i32(v
),
1013 Content
::I64(v
) => visitor
.visit_i64(v
),
1014 Content
::F32(v
) => visitor
.visit_f32(v
),
1015 Content
::F64(v
) => visitor
.visit_f64(v
),
1016 Content
::Char(v
) => visitor
.visit_char(v
),
1017 Content
::String(v
) => visitor
.visit_string(v
),
1018 Content
::Str(v
) => visitor
.visit_borrowed_str(v
),
1019 Content
::ByteBuf(v
) => visitor
.visit_byte_buf(v
),
1020 Content
::Bytes(v
) => visitor
.visit_borrowed_bytes(v
),
1021 Content
::Unit
=> visitor
.visit_unit(),
1022 Content
::None
=> visitor
.visit_none(),
1023 Content
::Some(v
) => visitor
.visit_some(ContentDeserializer
::new(*v
)),
1024 Content
::Newtype(v
) => visitor
.visit_newtype_struct(ContentDeserializer
::new(*v
)),
1025 Content
::Seq(v
) => {
1026 let seq
= v
.into_iter().map(ContentDeserializer
::new
);
1027 let mut seq_visitor
= de
::value
::SeqDeserializer
::new(seq
);
1028 let value
= try
!(visitor
.visit_seq(&mut seq_visitor
));
1029 try
!(seq_visitor
.end());
1032 Content
::Map(v
) => {
1033 let map
= v
.into_iter().map(|(k
, v
)| {
1034 (ContentDeserializer
::new(k
),
1035 ContentDeserializer
::new(v
))
1037 let mut map_visitor
= de
::value
::MapDeserializer
::new(map
);
1038 let value
= try
!(visitor
.visit_map(&mut map_visitor
));
1039 try
!(map_visitor
.end());
1045 fn deserialize_option
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1049 match self.content
{
1050 Content
::None
=> visitor
.visit_none(),
1051 Content
::Some(v
) => visitor
.visit_some(ContentDeserializer
::new(*v
)),
1052 Content
::Unit
=> visitor
.visit_unit(),
1053 _
=> visitor
.visit_some(self),
1057 fn deserialize_newtype_struct
<V
>(
1061 ) -> Result
<V
::Value
, Self::Error
>
1065 visitor
.visit_newtype_struct(self)
1068 fn deserialize_enum
<V
>(
1071 _variants
: &'
static [&'
static str],
1073 ) -> Result
<V
::Value
, Self::Error
>
1077 let (variant
, value
) = match self.content
{
1078 Content
::Map(value
) => {
1079 let mut iter
= value
.into_iter();
1080 let (variant
, value
) = match iter
.next() {
1084 de
::Error
::invalid_value(
1085 de
::Unexpected
::Map
,
1086 &"map with a single key",
1091 // enums are encoded in json as maps with a single key:value pair
1092 if iter
.next().is_some() {
1094 de
::Error
::invalid_value(
1095 de
::Unexpected
::Map
,
1096 &"map with a single key",
1100 (variant
, Some(value
))
1102 s @ Content
::String(_
) | s @ Content
::Str(_
) => (s
, None
),
1104 return Err(de
::Error
::invalid_type(other
.unexpected(), &"string or map"),);
1117 fn deserialize_unit_struct
<V
>(
1119 _name
: &'
static str,
1121 ) -> Result
<V
::Value
, Self::Error
>
1125 match self.content
{
1126 // As a special case, allow deserializing untagged newtype
1127 // variant containing unit struct.
1129 // #[derive(Deserialize)]
1132 // #[derive(Deserialize)]
1133 // #[serde(tag = "topic")]
1138 // We want {"topic":"Info"} to deserialize even though
1139 // ordinarily unit structs do not deserialize from empty map.
1140 Content
::Map(ref v
) if v
.is_empty() => visitor
.visit_unit(),
1141 _
=> self.deserialize_any(visitor
),
1145 forward_to_deserialize_any
! {
1146 bool
i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
1147 byte_buf unit seq tuple tuple_struct map
struct identifier
1152 impl<'de
, E
> ContentDeserializer
<'de
, E
> {
1153 /// private API, don't use
1154 pub fn new(content
: Content
<'de
>) -> Self {
1155 ContentDeserializer
{
1162 struct EnumDeserializer
<'de
, E
>
1166 variant
: Content
<'de
>,
1167 value
: Option
<Content
<'de
>>,
1168 err
: PhantomData
<E
>,
1171 impl<'de
, E
> de
::EnumAccess
<'de
> for EnumDeserializer
<'de
, E
>
1176 type Variant
= VariantDeserializer
<'de
, Self::Error
>;
1178 fn variant_seed
<V
>(self, seed
: V
) -> Result
<(V
::Value
, Self::Variant
), E
>
1180 V
: de
::DeserializeSeed
<'de
>,
1182 let visitor
= VariantDeserializer
{
1186 seed
.deserialize(ContentDeserializer
::new(self.variant
))
1187 .map(|v
| (v
, visitor
))
1191 struct VariantDeserializer
<'de
, E
>
1195 value
: Option
<Content
<'de
>>,
1196 err
: PhantomData
<E
>,
1199 impl<'de
, E
> de
::VariantAccess
<'de
> for VariantDeserializer
<'de
, E
>
1205 fn unit_variant(self) -> Result
<(), E
> {
1207 Some(value
) => de
::Deserialize
::deserialize(ContentDeserializer
::new(value
)),
1212 fn newtype_variant_seed
<T
>(self, seed
: T
) -> Result
<T
::Value
, E
>
1214 T
: de
::DeserializeSeed
<'de
>,
1217 Some(value
) => seed
.deserialize(ContentDeserializer
::new(value
)),
1219 Err(de
::Error
::invalid_type(de
::Unexpected
::UnitVariant
, &"newtype variant"),)
1224 fn tuple_variant
<V
>(self, _len
: usize, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1226 V
: de
::Visitor
<'de
>,
1229 Some(Content
::Seq(v
)) => {
1230 de
::Deserializer
::deserialize_any(SeqDeserializer
::new(v
), visitor
)
1232 Some(other
) => Err(de
::Error
::invalid_type(other
.unexpected(), &"tuple variant"),),
1233 None
=> Err(de
::Error
::invalid_type(de
::Unexpected
::UnitVariant
, &"tuple variant"),),
1237 fn struct_variant
<V
>(
1239 _fields
: &'
static [&'
static str],
1241 ) -> Result
<V
::Value
, Self::Error
>
1243 V
: de
::Visitor
<'de
>,
1246 Some(Content
::Map(v
)) => {
1247 de
::Deserializer
::deserialize_any(MapDeserializer
::new(v
), visitor
)
1249 Some(Content
::Seq(v
)) => {
1250 de
::Deserializer
::deserialize_any(SeqDeserializer
::new(v
), visitor
)
1252 Some(other
) => Err(de
::Error
::invalid_type(other
.unexpected(), &"struct variant"),),
1253 _
=> Err(de
::Error
::invalid_type(de
::Unexpected
::UnitVariant
, &"struct variant"),),
1258 struct SeqDeserializer
<'de
, E
>
1262 iter
: <Vec
<Content
<'de
>> as IntoIterator
>::IntoIter
,
1263 err
: PhantomData
<E
>,
1266 impl<'de
, E
> SeqDeserializer
<'de
, E
>
1270 fn new(vec
: Vec
<Content
<'de
>>) -> Self {
1272 iter
: vec
.into_iter(),
1278 impl<'de
, E
> de
::Deserializer
<'de
> for SeqDeserializer
<'de
, E
>
1285 fn deserialize_any
<V
>(mut self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1287 V
: de
::Visitor
<'de
>,
1289 let len
= self.iter
.len();
1291 visitor
.visit_unit()
1293 let ret
= try
!(visitor
.visit_seq(&mut self));
1294 let remaining
= self.iter
.len();
1298 Err(de
::Error
::invalid_length(len
, &"fewer elements in array"))
1303 forward_to_deserialize_any
! {
1304 bool
i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
1305 byte_buf option unit unit_struct newtype_struct seq tuple
1306 tuple_struct map
struct enum identifier ignored_any
1310 impl<'de
, E
> de
::SeqAccess
<'de
> for SeqDeserializer
<'de
, E
>
1316 fn next_element_seed
<T
>(&mut self, seed
: T
) -> Result
<Option
<T
::Value
>, Self::Error
>
1318 T
: de
::DeserializeSeed
<'de
>,
1320 match self.iter
.next() {
1322 seed
.deserialize(ContentDeserializer
::new(value
))
1329 fn size_hint(&self) -> Option
<usize> {
1330 size_hint
::from_bounds(&self.iter
)
1334 struct MapDeserializer
<'de
, E
>
1338 iter
: <Vec
<(Content
<'de
>, Content
<'de
>)> as IntoIterator
>::IntoIter
,
1339 value
: Option
<Content
<'de
>>,
1340 err
: PhantomData
<E
>,
1343 impl<'de
, E
> MapDeserializer
<'de
, E
>
1347 fn new(map
: Vec
<(Content
<'de
>, Content
<'de
>)>) -> Self {
1349 iter
: map
.into_iter(),
1356 impl<'de
, E
> de
::MapAccess
<'de
> for MapDeserializer
<'de
, E
>
1362 fn next_key_seed
<T
>(&mut self, seed
: T
) -> Result
<Option
<T
::Value
>, Self::Error
>
1364 T
: de
::DeserializeSeed
<'de
>,
1366 match self.iter
.next() {
1367 Some((key
, value
)) => {
1368 self.value
= Some(value
);
1369 seed
.deserialize(ContentDeserializer
::new(key
)).map(Some
)
1375 fn next_value_seed
<T
>(&mut self, seed
: T
) -> Result
<T
::Value
, Self::Error
>
1377 T
: de
::DeserializeSeed
<'de
>,
1379 match self.value
.take() {
1380 Some(value
) => seed
.deserialize(ContentDeserializer
::new(value
)),
1381 None
=> Err(de
::Error
::custom("value is missing")),
1385 fn size_hint(&self) -> Option
<usize> {
1386 size_hint
::from_bounds(&self.iter
)
1390 impl<'de
, E
> de
::Deserializer
<'de
> for MapDeserializer
<'de
, E
>
1397 fn deserialize_any
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1399 V
: de
::Visitor
<'de
>,
1401 visitor
.visit_map(self)
1404 forward_to_deserialize_any
! {
1405 bool
i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
1406 byte_buf option unit unit_struct newtype_struct seq tuple
1407 tuple_struct map
struct enum identifier ignored_any
1412 pub struct ContentRefDeserializer
<'a
, 'de
: 'a
, E
> {
1413 content
: &'a Content
<'de
>,
1414 err
: PhantomData
<E
>,
1417 /// Used when deserializing an untagged enum because the content may need to be
1418 /// used more than once.
1419 impl<'de
, 'a
, E
> Deserializer
<'de
> for ContentRefDeserializer
<'a
, 'de
, E
>
1425 fn deserialize_any
<V
>(self, visitor
: V
) -> Result
<V
::Value
, E
>
1429 match *self.content
{
1430 Content
::Bool(v
) => visitor
.visit_bool(v
),
1431 Content
::U8(v
) => visitor
.visit_u8(v
),
1432 Content
::U16(v
) => visitor
.visit_u16(v
),
1433 Content
::U32(v
) => visitor
.visit_u32(v
),
1434 Content
::U64(v
) => visitor
.visit_u64(v
),
1435 Content
::I8(v
) => visitor
.visit_i8(v
),
1436 Content
::I16(v
) => visitor
.visit_i16(v
),
1437 Content
::I32(v
) => visitor
.visit_i32(v
),
1438 Content
::I64(v
) => visitor
.visit_i64(v
),
1439 Content
::F32(v
) => visitor
.visit_f32(v
),
1440 Content
::F64(v
) => visitor
.visit_f64(v
),
1441 Content
::Char(v
) => visitor
.visit_char(v
),
1442 Content
::String(ref v
) => visitor
.visit_str(v
),
1443 Content
::Str(v
) => visitor
.visit_borrowed_str(v
),
1444 Content
::ByteBuf(ref v
) => visitor
.visit_bytes(v
),
1445 Content
::Bytes(v
) => visitor
.visit_borrowed_bytes(v
),
1446 Content
::Unit
=> visitor
.visit_unit(),
1447 Content
::None
=> visitor
.visit_none(),
1448 Content
::Some(ref v
) => visitor
.visit_some(ContentRefDeserializer
::new(v
)),
1449 Content
::Newtype(ref v
) => {
1450 visitor
.visit_newtype_struct(ContentRefDeserializer
::new(v
))
1452 Content
::Seq(ref v
) => {
1453 let seq
= v
.into_iter().map(ContentRefDeserializer
::new
);
1454 let mut seq_visitor
= de
::value
::SeqDeserializer
::new(seq
);
1455 let value
= try
!(visitor
.visit_seq(&mut seq_visitor
));
1456 try
!(seq_visitor
.end());
1459 Content
::Map(ref v
) => {
1460 let map
= v
.into_iter()
1463 (ContentRefDeserializer
::new(k
), ContentRefDeserializer
::new(v
))
1466 let mut map_visitor
= de
::value
::MapDeserializer
::new(map
);
1467 let value
= try
!(visitor
.visit_map(&mut map_visitor
));
1468 try
!(map_visitor
.end());
1474 fn deserialize_option
<V
>(self, visitor
: V
) -> Result
<V
::Value
, E
>
1478 match *self.content
{
1479 Content
::None
=> visitor
.visit_none(),
1480 Content
::Some(ref v
) => visitor
.visit_some(ContentRefDeserializer
::new(v
)),
1481 Content
::Unit
=> visitor
.visit_unit(),
1482 _
=> visitor
.visit_some(self),
1486 fn deserialize_newtype_struct
<V
>(self, _name
: &str, visitor
: V
) -> Result
<V
::Value
, E
>
1490 visitor
.visit_newtype_struct(self)
1493 fn deserialize_enum
<V
>(
1496 _variants
: &'
static [&'
static str],
1498 ) -> Result
<V
::Value
, Self::Error
>
1502 let (variant
, value
) = match *self.content
{
1503 Content
::Map(ref value
) => {
1504 let mut iter
= value
.into_iter();
1505 let &(ref variant
, ref value
) = match iter
.next() {
1509 de
::Error
::invalid_value(
1510 de
::Unexpected
::Map
,
1511 &"map with a single key",
1516 // enums are encoded in json as maps with a single key:value pair
1517 if iter
.next().is_some() {
1519 de
::Error
::invalid_value(
1520 de
::Unexpected
::Map
,
1521 &"map with a single key",
1525 (variant
, Some(value
))
1527 ref s @ Content
::String(_
) | ref s @ Content
::Str(_
) => (s
, None
),
1529 return Err(de
::Error
::invalid_type(other
.unexpected(), &"string or map"),);
1534 EnumRefDeserializer
{
1542 forward_to_deserialize_any
! {
1543 bool
i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
1544 byte_buf unit unit_struct seq tuple tuple_struct map
struct
1545 identifier ignored_any
1549 impl<'a
, 'de
, E
> ContentRefDeserializer
<'a
, 'de
, E
> {
1550 /// private API, don't use
1551 pub fn new(content
: &'a Content
<'de
>) -> Self {
1552 ContentRefDeserializer
{
1559 struct EnumRefDeserializer
<'a
, 'de
: 'a
, E
>
1563 variant
: &'a Content
<'de
>,
1564 value
: Option
<&'a Content
<'de
>>,
1565 err
: PhantomData
<E
>,
1568 impl<'de
, 'a
, E
> de
::EnumAccess
<'de
> for EnumRefDeserializer
<'a
, 'de
, E
>
1573 type Variant
= VariantRefDeserializer
<'a
, 'de
, Self::Error
>;
1575 fn variant_seed
<V
>(self, seed
: V
) -> Result
<(V
::Value
, Self::Variant
), Self::Error
>
1577 V
: de
::DeserializeSeed
<'de
>,
1579 let visitor
= VariantRefDeserializer
{
1583 seed
.deserialize(ContentRefDeserializer
::new(self.variant
))
1584 .map(|v
| (v
, visitor
))
1588 struct VariantRefDeserializer
<'a
, 'de
: 'a
, E
>
1592 value
: Option
<&'a Content
<'de
>>,
1593 err
: PhantomData
<E
>,
1596 impl<'de
, 'a
, E
> de
::VariantAccess
<'de
> for VariantRefDeserializer
<'a
, 'de
, E
>
1602 fn unit_variant(self) -> Result
<(), E
> {
1604 Some(value
) => de
::Deserialize
::deserialize(ContentRefDeserializer
::new(value
)),
1609 fn newtype_variant_seed
<T
>(self, seed
: T
) -> Result
<T
::Value
, E
>
1611 T
: de
::DeserializeSeed
<'de
>,
1614 Some(value
) => seed
.deserialize(ContentRefDeserializer
::new(value
)),
1616 Err(de
::Error
::invalid_type(de
::Unexpected
::UnitVariant
, &"newtype variant"),)
1621 fn tuple_variant
<V
>(self, _len
: usize, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1623 V
: de
::Visitor
<'de
>,
1626 Some(&Content
::Seq(ref v
)) => {
1627 de
::Deserializer
::deserialize_any(SeqRefDeserializer
::new(v
), visitor
)
1629 Some(other
) => Err(de
::Error
::invalid_type(other
.unexpected(), &"tuple variant"),),
1630 None
=> Err(de
::Error
::invalid_type(de
::Unexpected
::UnitVariant
, &"tuple variant"),),
1634 fn struct_variant
<V
>(
1636 _fields
: &'
static [&'
static str],
1638 ) -> Result
<V
::Value
, Self::Error
>
1640 V
: de
::Visitor
<'de
>,
1643 Some(&Content
::Map(ref v
)) => {
1644 de
::Deserializer
::deserialize_any(MapRefDeserializer
::new(v
), visitor
)
1646 Some(&Content
::Seq(ref v
)) => {
1647 de
::Deserializer
::deserialize_any(SeqRefDeserializer
::new(v
), visitor
)
1649 Some(other
) => Err(de
::Error
::invalid_type(other
.unexpected(), &"struct variant"),),
1650 _
=> Err(de
::Error
::invalid_type(de
::Unexpected
::UnitVariant
, &"struct variant"),),
1655 struct SeqRefDeserializer
<'a
, 'de
: 'a
, E
>
1659 iter
: <&'a
[Content
<'de
>] as IntoIterator
>::IntoIter
,
1660 err
: PhantomData
<E
>,
1663 impl<'a
, 'de
, E
> SeqRefDeserializer
<'a
, 'de
, E
>
1667 fn new(vec
: &'a
[Content
<'de
>]) -> Self {
1668 SeqRefDeserializer
{
1669 iter
: vec
.into_iter(),
1675 impl<'de
, 'a
, E
> de
::Deserializer
<'de
> for SeqRefDeserializer
<'a
, 'de
, E
>
1682 fn deserialize_any
<V
>(mut self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1684 V
: de
::Visitor
<'de
>,
1686 let len
= self.iter
.len();
1688 visitor
.visit_unit()
1690 let ret
= try
!(visitor
.visit_seq(&mut self));
1691 let remaining
= self.iter
.len();
1695 Err(de
::Error
::invalid_length(len
, &"fewer elements in array"))
1700 forward_to_deserialize_any
! {
1701 bool
i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
1702 byte_buf option unit unit_struct newtype_struct seq tuple
1703 tuple_struct map
struct enum identifier ignored_any
1707 impl<'de
, 'a
, E
> de
::SeqAccess
<'de
> for SeqRefDeserializer
<'a
, 'de
, E
>
1713 fn next_element_seed
<T
>(&mut self, seed
: T
) -> Result
<Option
<T
::Value
>, Self::Error
>
1715 T
: de
::DeserializeSeed
<'de
>,
1717 match self.iter
.next() {
1719 seed
.deserialize(ContentRefDeserializer
::new(value
))
1726 fn size_hint(&self) -> Option
<usize> {
1727 size_hint
::from_bounds(&self.iter
)
1731 struct MapRefDeserializer
<'a
, 'de
: 'a
, E
>
1735 iter
: <&'a
[(Content
<'de
>, Content
<'de
>)] as IntoIterator
>::IntoIter
,
1736 value
: Option
<&'a Content
<'de
>>,
1737 err
: PhantomData
<E
>,
1740 impl<'a
, 'de
, E
> MapRefDeserializer
<'a
, 'de
, E
>
1744 fn new(map
: &'a
[(Content
<'de
>, Content
<'de
>)]) -> Self {
1745 MapRefDeserializer
{
1746 iter
: map
.into_iter(),
1753 impl<'de
, 'a
, E
> de
::MapAccess
<'de
> for MapRefDeserializer
<'a
, 'de
, E
>
1759 fn next_key_seed
<T
>(&mut self, seed
: T
) -> Result
<Option
<T
::Value
>, Self::Error
>
1761 T
: de
::DeserializeSeed
<'de
>,
1763 match self.iter
.next() {
1764 Some(&(ref key
, ref value
)) => {
1765 self.value
= Some(value
);
1766 seed
.deserialize(ContentRefDeserializer
::new(key
))
1773 fn next_value_seed
<T
>(&mut self, seed
: T
) -> Result
<T
::Value
, Self::Error
>
1775 T
: de
::DeserializeSeed
<'de
>,
1777 match self.value
.take() {
1778 Some(value
) => seed
.deserialize(ContentRefDeserializer
::new(value
)),
1779 None
=> Err(de
::Error
::custom("value is missing")),
1783 fn size_hint(&self) -> Option
<usize> {
1784 size_hint
::from_bounds(&self.iter
)
1788 impl<'de
, 'a
, E
> de
::Deserializer
<'de
> for MapRefDeserializer
<'a
, 'de
, E
>
1795 fn deserialize_any
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1797 V
: de
::Visitor
<'de
>,
1799 visitor
.visit_map(self)
1802 forward_to_deserialize_any
! {
1803 bool
i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
1804 byte_buf option unit unit_struct newtype_struct seq tuple
1805 tuple_struct map
struct enum identifier ignored_any
1809 impl<'de
, E
> de
::IntoDeserializer
<'de
, E
> for ContentDeserializer
<'de
, E
>
1813 type Deserializer
= Self;
1815 fn into_deserializer(self) -> Self {
1820 impl<'de
, 'a
, E
> de
::IntoDeserializer
<'de
, E
> for ContentRefDeserializer
<'a
, 'de
, E
>
1824 type Deserializer
= Self;
1826 fn into_deserializer(self) -> Self {
1831 /// Visitor for deserializing an internally tagged unit variant.
1834 pub struct InternallyTaggedUnitVisitor
<'a
> {
1836 variant_name
: &'a
str,
1839 impl<'a
> InternallyTaggedUnitVisitor
<'a
> {
1841 pub fn new(type_name
: &'a
str, variant_name
: &'a
str) -> Self {
1842 InternallyTaggedUnitVisitor
{
1843 type_name
: type_name
,
1844 variant_name
: variant_name
,
1849 impl<'de
, 'a
> Visitor
<'de
> for InternallyTaggedUnitVisitor
<'a
> {
1852 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
1853 write
!(formatter
, "unit variant {}::{}", self.type_name
, self.variant_name
)
1856 fn visit_seq
<S
>(self, _
: S
) -> Result
<(), S
::Error
>
1863 fn visit_map
<M
>(self, _
: M
) -> Result
<(), M
::Error
>
1871 /// Visitor for deserializing an untagged unit variant.
1874 pub struct UntaggedUnitVisitor
<'a
> {
1876 variant_name
: &'a
str,
1879 impl<'a
> UntaggedUnitVisitor
<'a
> {
1881 pub fn new(type_name
: &'a
str, variant_name
: &'a
str) -> Self {
1882 UntaggedUnitVisitor
{
1883 type_name
: type_name
,
1884 variant_name
: variant_name
,
1889 impl<'de
, 'a
> Visitor
<'de
> for UntaggedUnitVisitor
<'a
> {
1892 fn expecting(&self, formatter
: &mut fmt
::Formatter
) -> fmt
::Result
{
1893 write
!(formatter
, "unit variant {}::{}", self.type_name
, self.variant_name
)
1896 fn visit_unit
<E
>(self) -> Result
<(), E
>
1905 ////////////////////////////////////////////////////////////////////////////////
1907 // Like `IntoDeserializer` but also implemented for `&[u8]`. This is used for
1908 // the newtype fallthrough case of `field_identifier`.
1910 // #[derive(Deserialize)]
1911 // #[serde(field_identifier)]
1915 // Other(String), // deserialized using IdentifierDeserializer
1917 pub trait IdentifierDeserializer
<'de
, E
: Error
> {
1918 type Deserializer
: Deserializer
<'de
, Error
= E
>;
1920 fn from(self) -> Self::Deserializer
;
1923 impl<'de
, E
> IdentifierDeserializer
<'de
, E
> for u32
1927 type Deserializer
= <u32 as IntoDeserializer
<'de
, E
>>::Deserializer
;
1929 fn from(self) -> Self::Deserializer
{
1930 self.into_deserializer()
1934 pub struct StrDeserializer
<'a
, E
> {
1936 marker
: PhantomData
<E
>,
1939 impl<'a
, E
> IdentifierDeserializer
<'a
, E
> for &'a
str
1943 type Deserializer
= StrDeserializer
<'a
, E
>;
1945 fn from(self) -> Self::Deserializer
{
1948 marker
: PhantomData
,
1953 impl<'de
, 'a
, E
> Deserializer
<'de
> for StrDeserializer
<'a
, E
>
1959 fn deserialize_any
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
1963 visitor
.visit_str(self.value
)
1966 forward_to_deserialize_any
! {
1967 bool
i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
1968 byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
1969 map
struct enum identifier ignored_any
1973 pub struct BytesDeserializer
<'a
, E
> {
1975 marker
: PhantomData
<E
>,
1978 impl<'a
, E
> IdentifierDeserializer
<'a
, E
> for &'a
[u8]
1982 type Deserializer
= BytesDeserializer
<'a
, E
>;
1984 fn from(self) -> Self::Deserializer
{
1987 marker
: PhantomData
,
1992 impl<'de
, 'a
, E
> Deserializer
<'de
> for BytesDeserializer
<'a
, E
>
1998 fn deserialize_any
<V
>(self, visitor
: V
) -> Result
<V
::Value
, Self::Error
>
2002 visitor
.visit_bytes(self.value
)
2005 forward_to_deserialize_any
! {
2006 bool
i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 char str string bytes
2007 byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
2008 map
struct enum identifier ignored_any