1use crate::lib::*;
2
3use crate::de::value::{BorrowedBytesDeserializer, BytesDeserializer};
4use crate::de::{
5 Deserialize, DeserializeSeed, Deserializer, EnumAccess, Error, IntoDeserializer, VariantAccess,
6 Visitor,
7};
8
9#[cfg(any(feature = "std", feature = "alloc"))]
10use crate::de::{MapAccess, Unexpected};
11
12#[cfg(any(feature = "std", feature = "alloc"))]
13pub use self::content::{
14 Content, ContentDeserializer, ContentRefDeserializer, EnumDeserializer,
15 InternallyTaggedUnitVisitor, TagContentOtherField, TagContentOtherFieldVisitor,
16 TagOrContentField, TagOrContentFieldVisitor, TaggedContentVisitor, UntaggedUnitVisitor,
17};
18
19pub use crate::seed::InPlaceSeed;
20
21pub fn missing_field<'de, V, E>(field: &'static str) -> Result<V, E>
24where
25 V: Deserialize<'de>,
26 E: Error,
27{
28 struct MissingFieldDeserializer<E>(&'static str, PhantomData<E>);
29
30 impl<'de, E> Deserializer<'de> for MissingFieldDeserializer<E>
31 where
32 E: Error,
33 {
34 type Error = E;
35
36 fn deserialize_any<V>(self, _visitor: V) -> Result<V::Value, E>
37 where
38 V: Visitor<'de>,
39 {
40 Err(Error::missing_field(self.0))
41 }
42
43 fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, E>
44 where
45 V: Visitor<'de>,
46 {
47 visitor.visit_none()
48 }
49
50 forward_to_deserialize_any! {
51 bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
52 bytes byte_buf unit unit_struct newtype_struct seq tuple
53 tuple_struct map struct enum identifier ignored_any
54 }
55 }
56
57 let deserializer = MissingFieldDeserializer(field, PhantomData);
58 Deserialize::deserialize(deserializer)
59}
60
61#[cfg(any(feature = "std", feature = "alloc"))]
62pub fn borrow_cow_str<'de: 'a, 'a, D, R>(deserializer: D) -> Result<R, D::Error>
63where
64 D: Deserializer<'de>,
65 R: From<Cow<'a, str>>,
66{
67 struct CowStrVisitor;
68
69 impl<'a> Visitor<'a> for CowStrVisitor {
70 type Value = Cow<'a, str>;
71
72 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
73 formatter.write_str("a string")
74 }
75
76 fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
77 where
78 E: Error,
79 {
80 Ok(Cow::Owned(v.to_owned()))
81 }
82
83 fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E>
84 where
85 E: Error,
86 {
87 Ok(Cow::Borrowed(v))
88 }
89
90 fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
91 where
92 E: Error,
93 {
94 Ok(Cow::Owned(v))
95 }
96
97 fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
98 where
99 E: Error,
100 {
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)),
104 }
105 }
106
107 fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E>
108 where
109 E: Error,
110 {
111 match str::from_utf8(v) {
112 Ok(s) => Ok(Cow::Borrowed(s)),
113 Err(_) => Err(Error::invalid_value(Unexpected::Bytes(v), &self)),
114 }
115 }
116
117 fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
118 where
119 E: Error,
120 {
121 match String::from_utf8(v) {
122 Ok(s) => Ok(Cow::Owned(s)),
123 Err(e) => Err(Error::invalid_value(
124 Unexpected::Bytes(&e.into_bytes()),
125 &self,
126 )),
127 }
128 }
129 }
130
131 deserializer.deserialize_str(CowStrVisitor).map(From::from)
132}
133
134#[cfg(any(feature = "std", feature = "alloc"))]
135pub fn borrow_cow_bytes<'de: 'a, 'a, D, R>(deserializer: D) -> Result<R, D::Error>
136where
137 D: Deserializer<'de>,
138 R: From<Cow<'a, [u8]>>,
139{
140 struct CowBytesVisitor;
141
142 impl<'a> Visitor<'a> for CowBytesVisitor {
143 type Value = Cow<'a, [u8]>;
144
145 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
146 formatter.write_str("a byte array")
147 }
148
149 fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
150 where
151 E: Error,
152 {
153 Ok(Cow::Owned(v.as_bytes().to_vec()))
154 }
155
156 fn visit_borrowed_str<E>(self, v: &'a str) -> Result<Self::Value, E>
157 where
158 E: Error,
159 {
160 Ok(Cow::Borrowed(v.as_bytes()))
161 }
162
163 fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
164 where
165 E: Error,
166 {
167 Ok(Cow::Owned(v.into_bytes()))
168 }
169
170 fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
171 where
172 E: Error,
173 {
174 Ok(Cow::Owned(v.to_vec()))
175 }
176
177 fn visit_borrowed_bytes<E>(self, v: &'a [u8]) -> Result<Self::Value, E>
178 where
179 E: Error,
180 {
181 Ok(Cow::Borrowed(v))
182 }
183
184 fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
185 where
186 E: Error,
187 {
188 Ok(Cow::Owned(v))
189 }
190 }
191
192 deserializer
193 .deserialize_bytes(CowBytesVisitor)
194 .map(From::from)
195}
196
197#[cfg(any(feature = "std", feature = "alloc"))]
198mod content {
199 use crate::lib::*;
210
211 use crate::actually_private;
212 use crate::de::value::{MapDeserializer, SeqDeserializer};
213 use crate::de::{
214 self, size_hint, Deserialize, DeserializeSeed, Deserializer, EnumAccess, Expected,
215 IgnoredAny, MapAccess, SeqAccess, Unexpected, Visitor,
216 };
217
218 #[derive(Debug, Clone)]
223 pub enum Content<'de> {
224 Bool(bool),
225
226 U8(u8),
227 U16(u16),
228 U32(u32),
229 U64(u64),
230
231 I8(i8),
232 I16(i16),
233 I32(i32),
234 I64(i64),
235
236 F32(f32),
237 F64(f64),
238
239 Char(char),
240 String(String),
241 Str(&'de str),
242 ByteBuf(Vec<u8>),
243 Bytes(&'de [u8]),
244
245 None,
246 Some(Box<Content<'de>>),
247
248 Unit,
249 Newtype(Box<Content<'de>>),
250 Seq(Vec<Content<'de>>),
251 Map(Vec<(Content<'de>, Content<'de>)>),
252 }
253
254 impl<'de> Content<'de> {
255 pub fn as_str(&self) -> Option<&str> {
256 match *self {
257 Content::Str(x) => Some(x),
258 Content::String(ref x) => Some(x),
259 Content::Bytes(x) => str::from_utf8(x).ok(),
260 Content::ByteBuf(ref x) => str::from_utf8(x).ok(),
261 _ => None,
262 }
263 }
264
265 #[cold]
266 fn unexpected(&self) -> Unexpected {
267 match *self {
268 Content::Bool(b) => Unexpected::Bool(b),
269 Content::U8(n) => Unexpected::Unsigned(n as u64),
270 Content::U16(n) => Unexpected::Unsigned(n as u64),
271 Content::U32(n) => Unexpected::Unsigned(n as u64),
272 Content::U64(n) => Unexpected::Unsigned(n),
273 Content::I8(n) => Unexpected::Signed(n as i64),
274 Content::I16(n) => Unexpected::Signed(n as i64),
275 Content::I32(n) => Unexpected::Signed(n as i64),
276 Content::I64(n) => Unexpected::Signed(n),
277 Content::F32(f) => Unexpected::Float(f as f64),
278 Content::F64(f) => Unexpected::Float(f),
279 Content::Char(c) => Unexpected::Char(c),
280 Content::String(ref s) => Unexpected::Str(s),
281 Content::Str(s) => Unexpected::Str(s),
282 Content::ByteBuf(ref b) => Unexpected::Bytes(b),
283 Content::Bytes(b) => Unexpected::Bytes(b),
284 Content::None | Content::Some(_) => Unexpected::Option,
285 Content::Unit => Unexpected::Unit,
286 Content::Newtype(_) => Unexpected::NewtypeStruct,
287 Content::Seq(_) => Unexpected::Seq,
288 Content::Map(_) => Unexpected::Map,
289 }
290 }
291 }
292
293 impl<'de> Deserialize<'de> for Content<'de> {
294 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
295 where
296 D: Deserializer<'de>,
297 {
298 let visitor = ContentVisitor { value: PhantomData };
301 deserializer.__deserialize_content(actually_private::T, visitor)
302 }
303 }
304
305 impl<'de, E> de::IntoDeserializer<'de, E> for Content<'de>
306 where
307 E: de::Error,
308 {
309 type Deserializer = ContentDeserializer<'de, E>;
310
311 fn into_deserializer(self) -> Self::Deserializer {
312 ContentDeserializer::new(self)
313 }
314 }
315
316 struct ContentVisitor<'de> {
319 value: PhantomData<Content<'de>>,
320 }
321
322 impl<'de> ContentVisitor<'de> {
323 fn new() -> Self {
324 ContentVisitor { value: PhantomData }
325 }
326 }
327
328 impl<'de> Visitor<'de> for ContentVisitor<'de> {
329 type Value = Content<'de>;
330
331 fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
332 fmt.write_str("any value")
333 }
334
335 fn visit_bool<F>(self, value: bool) -> Result<Self::Value, F>
336 where
337 F: de::Error,
338 {
339 Ok(Content::Bool(value))
340 }
341
342 fn visit_i8<F>(self, value: i8) -> Result<Self::Value, F>
343 where
344 F: de::Error,
345 {
346 Ok(Content::I8(value))
347 }
348
349 fn visit_i16<F>(self, value: i16) -> Result<Self::Value, F>
350 where
351 F: de::Error,
352 {
353 Ok(Content::I16(value))
354 }
355
356 fn visit_i32<F>(self, value: i32) -> Result<Self::Value, F>
357 where
358 F: de::Error,
359 {
360 Ok(Content::I32(value))
361 }
362
363 fn visit_i64<F>(self, value: i64) -> Result<Self::Value, F>
364 where
365 F: de::Error,
366 {
367 Ok(Content::I64(value))
368 }
369
370 fn visit_u8<F>(self, value: u8) -> Result<Self::Value, F>
371 where
372 F: de::Error,
373 {
374 Ok(Content::U8(value))
375 }
376
377 fn visit_u16<F>(self, value: u16) -> Result<Self::Value, F>
378 where
379 F: de::Error,
380 {
381 Ok(Content::U16(value))
382 }
383
384 fn visit_u32<F>(self, value: u32) -> Result<Self::Value, F>
385 where
386 F: de::Error,
387 {
388 Ok(Content::U32(value))
389 }
390
391 fn visit_u64<F>(self, value: u64) -> Result<Self::Value, F>
392 where
393 F: de::Error,
394 {
395 Ok(Content::U64(value))
396 }
397
398 fn visit_f32<F>(self, value: f32) -> Result<Self::Value, F>
399 where
400 F: de::Error,
401 {
402 Ok(Content::F32(value))
403 }
404
405 fn visit_f64<F>(self, value: f64) -> Result<Self::Value, F>
406 where
407 F: de::Error,
408 {
409 Ok(Content::F64(value))
410 }
411
412 fn visit_char<F>(self, value: char) -> Result<Self::Value, F>
413 where
414 F: de::Error,
415 {
416 Ok(Content::Char(value))
417 }
418
419 fn visit_str<F>(self, value: &str) -> Result<Self::Value, F>
420 where
421 F: de::Error,
422 {
423 Ok(Content::String(value.into()))
424 }
425
426 fn visit_borrowed_str<F>(self, value: &'de str) -> Result<Self::Value, F>
427 where
428 F: de::Error,
429 {
430 Ok(Content::Str(value))
431 }
432
433 fn visit_string<F>(self, value: String) -> Result<Self::Value, F>
434 where
435 F: de::Error,
436 {
437 Ok(Content::String(value))
438 }
439
440 fn visit_bytes<F>(self, value: &[u8]) -> Result<Self::Value, F>
441 where
442 F: de::Error,
443 {
444 Ok(Content::ByteBuf(value.into()))
445 }
446
447 fn visit_borrowed_bytes<F>(self, value: &'de [u8]) -> Result<Self::Value, F>
448 where
449 F: de::Error,
450 {
451 Ok(Content::Bytes(value))
452 }
453
454 fn visit_byte_buf<F>(self, value: Vec<u8>) -> Result<Self::Value, F>
455 where
456 F: de::Error,
457 {
458 Ok(Content::ByteBuf(value))
459 }
460
461 fn visit_unit<F>(self) -> Result<Self::Value, F>
462 where
463 F: de::Error,
464 {
465 Ok(Content::Unit)
466 }
467
468 fn visit_none<F>(self) -> Result<Self::Value, F>
469 where
470 F: de::Error,
471 {
472 Ok(Content::None)
473 }
474
475 fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
476 where
477 D: Deserializer<'de>,
478 {
479 Deserialize::deserialize(deserializer).map(|v| Content::Some(Box::new(v)))
480 }
481
482 fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
483 where
484 D: Deserializer<'de>,
485 {
486 Deserialize::deserialize(deserializer).map(|v| Content::Newtype(Box::new(v)))
487 }
488
489 fn visit_seq<V>(self, mut visitor: V) -> Result<Self::Value, V::Error>
490 where
491 V: SeqAccess<'de>,
492 {
493 let mut vec =
494 Vec::<Content>::with_capacity(size_hint::cautious::<Content>(visitor.size_hint()));
495 while let Some(e) = tri!(visitor.next_element()) {
496 vec.push(e);
497 }
498 Ok(Content::Seq(vec))
499 }
500
501 fn visit_map<V>(self, mut visitor: V) -> Result<Self::Value, V::Error>
502 where
503 V: MapAccess<'de>,
504 {
505 let mut vec =
506 Vec::<(Content, Content)>::with_capacity(
507 size_hint::cautious::<(Content, Content)>(visitor.size_hint()),
508 );
509 while let Some(kv) = tri!(visitor.next_entry()) {
510 vec.push(kv);
511 }
512 Ok(Content::Map(vec))
513 }
514
515 fn visit_enum<V>(self, _visitor: V) -> Result<Self::Value, V::Error>
516 where
517 V: EnumAccess<'de>,
518 {
519 Err(de::Error::custom(
520 "untagged and internally tagged enums do not support enum input",
521 ))
522 }
523 }
524
525 pub enum TagOrContent<'de> {
529 Tag,
530 Content(Content<'de>),
531 }
532
533 struct TagOrContentVisitor<'de> {
536 name: &'static str,
537 value: PhantomData<TagOrContent<'de>>,
538 }
539
540 impl<'de> TagOrContentVisitor<'de> {
541 fn new(name: &'static str) -> Self {
542 TagOrContentVisitor {
543 name,
544 value: PhantomData,
545 }
546 }
547 }
548
549 impl<'de> DeserializeSeed<'de> for TagOrContentVisitor<'de> {
550 type Value = TagOrContent<'de>;
551
552 fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
553 where
554 D: Deserializer<'de>,
555 {
556 deserializer.deserialize_any(self)
559 }
560 }
561
562 impl<'de> Visitor<'de> for TagOrContentVisitor<'de> {
563 type Value = TagOrContent<'de>;
564
565 fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
566 write!(fmt, "a type tag `{}` or any other value", self.name)
567 }
568
569 fn visit_bool<F>(self, value: bool) -> Result<Self::Value, F>
570 where
571 F: de::Error,
572 {
573 ContentVisitor::new()
574 .visit_bool(value)
575 .map(TagOrContent::Content)
576 }
577
578 fn visit_i8<F>(self, value: i8) -> Result<Self::Value, F>
579 where
580 F: de::Error,
581 {
582 ContentVisitor::new()
583 .visit_i8(value)
584 .map(TagOrContent::Content)
585 }
586
587 fn visit_i16<F>(self, value: i16) -> Result<Self::Value, F>
588 where
589 F: de::Error,
590 {
591 ContentVisitor::new()
592 .visit_i16(value)
593 .map(TagOrContent::Content)
594 }
595
596 fn visit_i32<F>(self, value: i32) -> Result<Self::Value, F>
597 where
598 F: de::Error,
599 {
600 ContentVisitor::new()
601 .visit_i32(value)
602 .map(TagOrContent::Content)
603 }
604
605 fn visit_i64<F>(self, value: i64) -> Result<Self::Value, F>
606 where
607 F: de::Error,
608 {
609 ContentVisitor::new()
610 .visit_i64(value)
611 .map(TagOrContent::Content)
612 }
613
614 fn visit_u8<F>(self, value: u8) -> Result<Self::Value, F>
615 where
616 F: de::Error,
617 {
618 ContentVisitor::new()
619 .visit_u8(value)
620 .map(TagOrContent::Content)
621 }
622
623 fn visit_u16<F>(self, value: u16) -> Result<Self::Value, F>
624 where
625 F: de::Error,
626 {
627 ContentVisitor::new()
628 .visit_u16(value)
629 .map(TagOrContent::Content)
630 }
631
632 fn visit_u32<F>(self, value: u32) -> Result<Self::Value, F>
633 where
634 F: de::Error,
635 {
636 ContentVisitor::new()
637 .visit_u32(value)
638 .map(TagOrContent::Content)
639 }
640
641 fn visit_u64<F>(self, value: u64) -> Result<Self::Value, F>
642 where
643 F: de::Error,
644 {
645 ContentVisitor::new()
646 .visit_u64(value)
647 .map(TagOrContent::Content)
648 }
649
650 fn visit_f32<F>(self, value: f32) -> Result<Self::Value, F>
651 where
652 F: de::Error,
653 {
654 ContentVisitor::new()
655 .visit_f32(value)
656 .map(TagOrContent::Content)
657 }
658
659 fn visit_f64<F>(self, value: f64) -> Result<Self::Value, F>
660 where
661 F: de::Error,
662 {
663 ContentVisitor::new()
664 .visit_f64(value)
665 .map(TagOrContent::Content)
666 }
667
668 fn visit_char<F>(self, value: char) -> Result<Self::Value, F>
669 where
670 F: de::Error,
671 {
672 ContentVisitor::new()
673 .visit_char(value)
674 .map(TagOrContent::Content)
675 }
676
677 fn visit_str<F>(self, value: &str) -> Result<Self::Value, F>
678 where
679 F: de::Error,
680 {
681 if value == self.name {
682 Ok(TagOrContent::Tag)
683 } else {
684 ContentVisitor::new()
685 .visit_str(value)
686 .map(TagOrContent::Content)
687 }
688 }
689
690 fn visit_borrowed_str<F>(self, value: &'de str) -> Result<Self::Value, F>
691 where
692 F: de::Error,
693 {
694 if value == self.name {
695 Ok(TagOrContent::Tag)
696 } else {
697 ContentVisitor::new()
698 .visit_borrowed_str(value)
699 .map(TagOrContent::Content)
700 }
701 }
702
703 fn visit_string<F>(self, value: String) -> Result<Self::Value, F>
704 where
705 F: de::Error,
706 {
707 if value == self.name {
708 Ok(TagOrContent::Tag)
709 } else {
710 ContentVisitor::new()
711 .visit_string(value)
712 .map(TagOrContent::Content)
713 }
714 }
715
716 fn visit_bytes<F>(self, value: &[u8]) -> Result<Self::Value, F>
717 where
718 F: de::Error,
719 {
720 if value == self.name.as_bytes() {
721 Ok(TagOrContent::Tag)
722 } else {
723 ContentVisitor::new()
724 .visit_bytes(value)
725 .map(TagOrContent::Content)
726 }
727 }
728
729 fn visit_borrowed_bytes<F>(self, value: &'de [u8]) -> Result<Self::Value, F>
730 where
731 F: de::Error,
732 {
733 if value == self.name.as_bytes() {
734 Ok(TagOrContent::Tag)
735 } else {
736 ContentVisitor::new()
737 .visit_borrowed_bytes(value)
738 .map(TagOrContent::Content)
739 }
740 }
741
742 fn visit_byte_buf<F>(self, value: Vec<u8>) -> Result<Self::Value, F>
743 where
744 F: de::Error,
745 {
746 if value == self.name.as_bytes() {
747 Ok(TagOrContent::Tag)
748 } else {
749 ContentVisitor::new()
750 .visit_byte_buf(value)
751 .map(TagOrContent::Content)
752 }
753 }
754
755 fn visit_unit<F>(self) -> Result<Self::Value, F>
756 where
757 F: de::Error,
758 {
759 ContentVisitor::new()
760 .visit_unit()
761 .map(TagOrContent::Content)
762 }
763
764 fn visit_none<F>(self) -> Result<Self::Value, F>
765 where
766 F: de::Error,
767 {
768 ContentVisitor::new()
769 .visit_none()
770 .map(TagOrContent::Content)
771 }
772
773 fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
774 where
775 D: Deserializer<'de>,
776 {
777 ContentVisitor::new()
778 .visit_some(deserializer)
779 .map(TagOrContent::Content)
780 }
781
782 fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
783 where
784 D: Deserializer<'de>,
785 {
786 ContentVisitor::new()
787 .visit_newtype_struct(deserializer)
788 .map(TagOrContent::Content)
789 }
790
791 fn visit_seq<V>(self, visitor: V) -> Result<Self::Value, V::Error>
792 where
793 V: SeqAccess<'de>,
794 {
795 ContentVisitor::new()
796 .visit_seq(visitor)
797 .map(TagOrContent::Content)
798 }
799
800 fn visit_map<V>(self, visitor: V) -> Result<Self::Value, V::Error>
801 where
802 V: MapAccess<'de>,
803 {
804 ContentVisitor::new()
805 .visit_map(visitor)
806 .map(TagOrContent::Content)
807 }
808
809 fn visit_enum<V>(self, visitor: V) -> Result<Self::Value, V::Error>
810 where
811 V: EnumAccess<'de>,
812 {
813 ContentVisitor::new()
814 .visit_enum(visitor)
815 .map(TagOrContent::Content)
816 }
817 }
818
819 pub struct TaggedContentVisitor<T> {
826 tag_name: &'static str,
827 expecting: &'static str,
828 value: PhantomData<T>,
829 }
830
831 impl<T> TaggedContentVisitor<T> {
832 pub fn new(name: &'static str, expecting: &'static str) -> Self {
835 TaggedContentVisitor {
836 tag_name: name,
837 expecting,
838 value: PhantomData,
839 }
840 }
841 }
842
843 impl<'de, T> Visitor<'de> for TaggedContentVisitor<T>
844 where
845 T: Deserialize<'de>,
846 {
847 type Value = (T, Content<'de>);
848
849 fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
850 fmt.write_str(self.expecting)
851 }
852
853 fn visit_seq<S>(self, mut seq: S) -> Result<Self::Value, S::Error>
854 where
855 S: SeqAccess<'de>,
856 {
857 let tag = match tri!(seq.next_element()) {
858 Some(tag) => tag,
859 None => {
860 return Err(de::Error::missing_field(self.tag_name));
861 }
862 };
863 let rest = de::value::SeqAccessDeserializer::new(seq);
864 Ok((tag, tri!(Content::deserialize(rest))))
865 }
866
867 fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
868 where
869 M: MapAccess<'de>,
870 {
871 let mut tag = None;
872 let mut vec = Vec::<(Content, Content)>::with_capacity(size_hint::cautious::<(
873 Content,
874 Content,
875 )>(map.size_hint()));
876 while let Some(k) = tri!(map.next_key_seed(TagOrContentVisitor::new(self.tag_name))) {
877 match k {
878 TagOrContent::Tag => {
879 if tag.is_some() {
880 return Err(de::Error::duplicate_field(self.tag_name));
881 }
882 tag = Some(tri!(map.next_value()));
883 }
884 TagOrContent::Content(k) => {
885 let v = tri!(map.next_value());
886 vec.push((k, v));
887 }
888 }
889 }
890 match tag {
891 None => Err(de::Error::missing_field(self.tag_name)),
892 Some(tag) => Ok((tag, Content::Map(vec))),
893 }
894 }
895 }
896
897 pub enum TagOrContentField {
901 Tag,
902 Content,
903 }
904
905 pub struct TagOrContentFieldVisitor {
907 pub tag: &'static str,
909 pub content: &'static str,
911 }
912
913 impl<'de> DeserializeSeed<'de> for TagOrContentFieldVisitor {
914 type Value = TagOrContentField;
915
916 fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
917 where
918 D: Deserializer<'de>,
919 {
920 deserializer.deserialize_identifier(self)
921 }
922 }
923
924 impl<'de> Visitor<'de> for TagOrContentFieldVisitor {
925 type Value = TagOrContentField;
926
927 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
928 write!(formatter, "{:?} or {:?}", self.tag, self.content)
929 }
930
931 fn visit_u64<E>(self, field_index: u64) -> Result<Self::Value, E>
932 where
933 E: de::Error,
934 {
935 match field_index {
936 0 => Ok(TagOrContentField::Tag),
937 1 => Ok(TagOrContentField::Content),
938 _ => Err(de::Error::invalid_value(
939 Unexpected::Unsigned(field_index),
940 &self,
941 )),
942 }
943 }
944
945 fn visit_str<E>(self, field: &str) -> Result<Self::Value, E>
946 where
947 E: de::Error,
948 {
949 if field == self.tag {
950 Ok(TagOrContentField::Tag)
951 } else if field == self.content {
952 Ok(TagOrContentField::Content)
953 } else {
954 Err(de::Error::invalid_value(Unexpected::Str(field), &self))
955 }
956 }
957
958 fn visit_bytes<E>(self, field: &[u8]) -> Result<Self::Value, E>
959 where
960 E: de::Error,
961 {
962 if field == self.tag.as_bytes() {
963 Ok(TagOrContentField::Tag)
964 } else if field == self.content.as_bytes() {
965 Ok(TagOrContentField::Content)
966 } else {
967 Err(de::Error::invalid_value(Unexpected::Bytes(field), &self))
968 }
969 }
970 }
971
972 pub enum TagContentOtherField {
977 Tag,
978 Content,
979 Other,
980 }
981
982 pub struct TagContentOtherFieldVisitor {
984 pub tag: &'static str,
986 pub content: &'static str,
988 }
989
990 impl<'de> DeserializeSeed<'de> for TagContentOtherFieldVisitor {
991 type Value = TagContentOtherField;
992
993 fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
994 where
995 D: Deserializer<'de>,
996 {
997 deserializer.deserialize_identifier(self)
998 }
999 }
1000
1001 impl<'de> Visitor<'de> for TagContentOtherFieldVisitor {
1002 type Value = TagContentOtherField;
1003
1004 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1005 write!(
1006 formatter,
1007 "{:?}, {:?}, or other ignored fields",
1008 self.tag, self.content
1009 )
1010 }
1011
1012 fn visit_u64<E>(self, field_index: u64) -> Result<Self::Value, E>
1013 where
1014 E: de::Error,
1015 {
1016 match field_index {
1017 0 => Ok(TagContentOtherField::Tag),
1018 1 => Ok(TagContentOtherField::Content),
1019 _ => Ok(TagContentOtherField::Other),
1020 }
1021 }
1022
1023 fn visit_str<E>(self, field: &str) -> Result<Self::Value, E>
1024 where
1025 E: de::Error,
1026 {
1027 self.visit_bytes(field.as_bytes())
1028 }
1029
1030 fn visit_bytes<E>(self, field: &[u8]) -> Result<Self::Value, E>
1031 where
1032 E: de::Error,
1033 {
1034 if field == self.tag.as_bytes() {
1035 Ok(TagContentOtherField::Tag)
1036 } else if field == self.content.as_bytes() {
1037 Ok(TagContentOtherField::Content)
1038 } else {
1039 Ok(TagContentOtherField::Other)
1040 }
1041 }
1042 }
1043
1044 pub struct ContentDeserializer<'de, E> {
1046 content: Content<'de>,
1047 err: PhantomData<E>,
1048 }
1049
1050 impl<'de, E> ContentDeserializer<'de, E>
1051 where
1052 E: de::Error,
1053 {
1054 #[cold]
1055 fn invalid_type(self, exp: &Expected) -> E {
1056 de::Error::invalid_type(self.content.unexpected(), exp)
1057 }
1058
1059 fn deserialize_integer<V>(self, visitor: V) -> Result<V::Value, E>
1060 where
1061 V: Visitor<'de>,
1062 {
1063 match self.content {
1064 Content::U8(v) => visitor.visit_u8(v),
1065 Content::U16(v) => visitor.visit_u16(v),
1066 Content::U32(v) => visitor.visit_u32(v),
1067 Content::U64(v) => visitor.visit_u64(v),
1068 Content::I8(v) => visitor.visit_i8(v),
1069 Content::I16(v) => visitor.visit_i16(v),
1070 Content::I32(v) => visitor.visit_i32(v),
1071 Content::I64(v) => visitor.visit_i64(v),
1072 _ => Err(self.invalid_type(&visitor)),
1073 }
1074 }
1075
1076 fn deserialize_float<V>(self, visitor: V) -> Result<V::Value, E>
1077 where
1078 V: Visitor<'de>,
1079 {
1080 match self.content {
1081 Content::F32(v) => visitor.visit_f32(v),
1082 Content::F64(v) => visitor.visit_f64(v),
1083 Content::U8(v) => visitor.visit_u8(v),
1084 Content::U16(v) => visitor.visit_u16(v),
1085 Content::U32(v) => visitor.visit_u32(v),
1086 Content::U64(v) => visitor.visit_u64(v),
1087 Content::I8(v) => visitor.visit_i8(v),
1088 Content::I16(v) => visitor.visit_i16(v),
1089 Content::I32(v) => visitor.visit_i32(v),
1090 Content::I64(v) => visitor.visit_i64(v),
1091 _ => Err(self.invalid_type(&visitor)),
1092 }
1093 }
1094 }
1095
1096 fn visit_content_seq<'de, V, E>(content: Vec<Content<'de>>, visitor: V) -> Result<V::Value, E>
1097 where
1098 V: Visitor<'de>,
1099 E: de::Error,
1100 {
1101 let seq = content.into_iter().map(ContentDeserializer::new);
1102 let mut seq_visitor = SeqDeserializer::new(seq);
1103 let value = tri!(visitor.visit_seq(&mut seq_visitor));
1104 tri!(seq_visitor.end());
1105 Ok(value)
1106 }
1107
1108 fn visit_content_map<'de, V, E>(
1109 content: Vec<(Content<'de>, Content<'de>)>,
1110 visitor: V,
1111 ) -> Result<V::Value, E>
1112 where
1113 V: Visitor<'de>,
1114 E: de::Error,
1115 {
1116 let map = content
1117 .into_iter()
1118 .map(|(k, v)| (ContentDeserializer::new(k), ContentDeserializer::new(v)));
1119 let mut map_visitor = MapDeserializer::new(map);
1120 let value = tri!(visitor.visit_map(&mut map_visitor));
1121 tri!(map_visitor.end());
1122 Ok(value)
1123 }
1124
1125 impl<'de, E> Deserializer<'de> for ContentDeserializer<'de, E>
1128 where
1129 E: de::Error,
1130 {
1131 type Error = E;
1132
1133 fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1134 where
1135 V: Visitor<'de>,
1136 {
1137 match self.content {
1138 Content::Bool(v) => visitor.visit_bool(v),
1139 Content::U8(v) => visitor.visit_u8(v),
1140 Content::U16(v) => visitor.visit_u16(v),
1141 Content::U32(v) => visitor.visit_u32(v),
1142 Content::U64(v) => visitor.visit_u64(v),
1143 Content::I8(v) => visitor.visit_i8(v),
1144 Content::I16(v) => visitor.visit_i16(v),
1145 Content::I32(v) => visitor.visit_i32(v),
1146 Content::I64(v) => visitor.visit_i64(v),
1147 Content::F32(v) => visitor.visit_f32(v),
1148 Content::F64(v) => visitor.visit_f64(v),
1149 Content::Char(v) => visitor.visit_char(v),
1150 Content::String(v) => visitor.visit_string(v),
1151 Content::Str(v) => visitor.visit_borrowed_str(v),
1152 Content::ByteBuf(v) => visitor.visit_byte_buf(v),
1153 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
1154 Content::Unit => visitor.visit_unit(),
1155 Content::None => visitor.visit_none(),
1156 Content::Some(v) => visitor.visit_some(ContentDeserializer::new(*v)),
1157 Content::Newtype(v) => visitor.visit_newtype_struct(ContentDeserializer::new(*v)),
1158 Content::Seq(v) => visit_content_seq(v, visitor),
1159 Content::Map(v) => visit_content_map(v, visitor),
1160 }
1161 }
1162
1163 fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1164 where
1165 V: Visitor<'de>,
1166 {
1167 match self.content {
1168 Content::Bool(v) => visitor.visit_bool(v),
1169 _ => Err(self.invalid_type(&visitor)),
1170 }
1171 }
1172
1173 fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1174 where
1175 V: Visitor<'de>,
1176 {
1177 self.deserialize_integer(visitor)
1178 }
1179
1180 fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1181 where
1182 V: Visitor<'de>,
1183 {
1184 self.deserialize_integer(visitor)
1185 }
1186
1187 fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1188 where
1189 V: Visitor<'de>,
1190 {
1191 self.deserialize_integer(visitor)
1192 }
1193
1194 fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1195 where
1196 V: Visitor<'de>,
1197 {
1198 self.deserialize_integer(visitor)
1199 }
1200
1201 fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1202 where
1203 V: Visitor<'de>,
1204 {
1205 self.deserialize_integer(visitor)
1206 }
1207
1208 fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1209 where
1210 V: Visitor<'de>,
1211 {
1212 self.deserialize_integer(visitor)
1213 }
1214
1215 fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1216 where
1217 V: Visitor<'de>,
1218 {
1219 self.deserialize_integer(visitor)
1220 }
1221
1222 fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1223 where
1224 V: Visitor<'de>,
1225 {
1226 self.deserialize_integer(visitor)
1227 }
1228
1229 fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1230 where
1231 V: Visitor<'de>,
1232 {
1233 self.deserialize_float(visitor)
1234 }
1235
1236 fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1237 where
1238 V: Visitor<'de>,
1239 {
1240 self.deserialize_float(visitor)
1241 }
1242
1243 fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1244 where
1245 V: Visitor<'de>,
1246 {
1247 match self.content {
1248 Content::Char(v) => visitor.visit_char(v),
1249 Content::String(v) => visitor.visit_string(v),
1250 Content::Str(v) => visitor.visit_borrowed_str(v),
1251 _ => Err(self.invalid_type(&visitor)),
1252 }
1253 }
1254
1255 fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1256 where
1257 V: Visitor<'de>,
1258 {
1259 self.deserialize_string(visitor)
1260 }
1261
1262 fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1263 where
1264 V: Visitor<'de>,
1265 {
1266 match self.content {
1267 Content::String(v) => visitor.visit_string(v),
1268 Content::Str(v) => visitor.visit_borrowed_str(v),
1269 Content::ByteBuf(v) => visitor.visit_byte_buf(v),
1270 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
1271 _ => Err(self.invalid_type(&visitor)),
1272 }
1273 }
1274
1275 fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1276 where
1277 V: Visitor<'de>,
1278 {
1279 self.deserialize_byte_buf(visitor)
1280 }
1281
1282 fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1283 where
1284 V: Visitor<'de>,
1285 {
1286 match self.content {
1287 Content::String(v) => visitor.visit_string(v),
1288 Content::Str(v) => visitor.visit_borrowed_str(v),
1289 Content::ByteBuf(v) => visitor.visit_byte_buf(v),
1290 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
1291 Content::Seq(v) => visit_content_seq(v, visitor),
1292 _ => Err(self.invalid_type(&visitor)),
1293 }
1294 }
1295
1296 fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1297 where
1298 V: Visitor<'de>,
1299 {
1300 match self.content {
1301 Content::None => visitor.visit_none(),
1302 Content::Some(v) => visitor.visit_some(ContentDeserializer::new(*v)),
1303 Content::Unit => visitor.visit_unit(),
1304 _ => visitor.visit_some(self),
1305 }
1306 }
1307
1308 fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1309 where
1310 V: Visitor<'de>,
1311 {
1312 match self.content {
1313 Content::Unit => visitor.visit_unit(),
1314
1315 Content::Map(ref v) if v.is_empty() => visitor.visit_unit(),
1325 _ => Err(self.invalid_type(&visitor)),
1326 }
1327 }
1328
1329 fn deserialize_unit_struct<V>(
1330 self,
1331 _name: &'static str,
1332 visitor: V,
1333 ) -> Result<V::Value, Self::Error>
1334 where
1335 V: Visitor<'de>,
1336 {
1337 match self.content {
1338 Content::Map(ref v) if v.is_empty() => visitor.visit_unit(),
1353 Content::Seq(ref v) if v.is_empty() => visitor.visit_unit(),
1354 _ => self.deserialize_any(visitor),
1355 }
1356 }
1357
1358 fn deserialize_newtype_struct<V>(
1359 self,
1360 _name: &str,
1361 visitor: V,
1362 ) -> Result<V::Value, Self::Error>
1363 where
1364 V: Visitor<'de>,
1365 {
1366 match self.content {
1367 Content::Newtype(v) => visitor.visit_newtype_struct(ContentDeserializer::new(*v)),
1368 _ => visitor.visit_newtype_struct(self),
1369 }
1370 }
1371
1372 fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1373 where
1374 V: Visitor<'de>,
1375 {
1376 match self.content {
1377 Content::Seq(v) => visit_content_seq(v, visitor),
1378 _ => Err(self.invalid_type(&visitor)),
1379 }
1380 }
1381
1382 fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
1383 where
1384 V: Visitor<'de>,
1385 {
1386 self.deserialize_seq(visitor)
1387 }
1388
1389 fn deserialize_tuple_struct<V>(
1390 self,
1391 _name: &'static str,
1392 _len: usize,
1393 visitor: V,
1394 ) -> Result<V::Value, Self::Error>
1395 where
1396 V: Visitor<'de>,
1397 {
1398 self.deserialize_seq(visitor)
1399 }
1400
1401 fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1402 where
1403 V: Visitor<'de>,
1404 {
1405 match self.content {
1406 Content::Map(v) => visit_content_map(v, visitor),
1407 _ => Err(self.invalid_type(&visitor)),
1408 }
1409 }
1410
1411 fn deserialize_struct<V>(
1412 self,
1413 _name: &'static str,
1414 _fields: &'static [&'static str],
1415 visitor: V,
1416 ) -> Result<V::Value, Self::Error>
1417 where
1418 V: Visitor<'de>,
1419 {
1420 match self.content {
1421 Content::Seq(v) => visit_content_seq(v, visitor),
1422 Content::Map(v) => visit_content_map(v, visitor),
1423 _ => Err(self.invalid_type(&visitor)),
1424 }
1425 }
1426
1427 fn deserialize_enum<V>(
1428 self,
1429 _name: &str,
1430 _variants: &'static [&'static str],
1431 visitor: V,
1432 ) -> Result<V::Value, Self::Error>
1433 where
1434 V: Visitor<'de>,
1435 {
1436 let (variant, value) = match self.content {
1437 Content::Map(value) => {
1438 let mut iter = value.into_iter();
1439 let (variant, value) = match iter.next() {
1440 Some(v) => v,
1441 None => {
1442 return Err(de::Error::invalid_value(
1443 de::Unexpected::Map,
1444 &"map with a single key",
1445 ));
1446 }
1447 };
1448 if iter.next().is_some() {
1450 return Err(de::Error::invalid_value(
1451 de::Unexpected::Map,
1452 &"map with a single key",
1453 ));
1454 }
1455 (variant, Some(value))
1456 }
1457 s @ Content::String(_) | s @ Content::Str(_) => (s, None),
1458 other => {
1459 return Err(de::Error::invalid_type(
1460 other.unexpected(),
1461 &"string or map",
1462 ));
1463 }
1464 };
1465
1466 visitor.visit_enum(EnumDeserializer::new(variant, value))
1467 }
1468
1469 fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1470 where
1471 V: Visitor<'de>,
1472 {
1473 match self.content {
1474 Content::String(v) => visitor.visit_string(v),
1475 Content::Str(v) => visitor.visit_borrowed_str(v),
1476 Content::ByteBuf(v) => visitor.visit_byte_buf(v),
1477 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
1478 Content::U8(v) => visitor.visit_u8(v),
1479 Content::U64(v) => visitor.visit_u64(v),
1480 _ => Err(self.invalid_type(&visitor)),
1481 }
1482 }
1483
1484 fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1485 where
1486 V: Visitor<'de>,
1487 {
1488 drop(self);
1489 visitor.visit_unit()
1490 }
1491
1492 fn __deserialize_content<V>(
1493 self,
1494 _: actually_private::T,
1495 visitor: V,
1496 ) -> Result<Content<'de>, Self::Error>
1497 where
1498 V: Visitor<'de, Value = Content<'de>>,
1499 {
1500 let _ = visitor;
1501 Ok(self.content)
1502 }
1503 }
1504
1505 impl<'de, E> ContentDeserializer<'de, E> {
1506 pub fn new(content: Content<'de>) -> Self {
1508 ContentDeserializer {
1509 content,
1510 err: PhantomData,
1511 }
1512 }
1513 }
1514
1515 pub struct EnumDeserializer<'de, E>
1516 where
1517 E: de::Error,
1518 {
1519 variant: Content<'de>,
1520 value: Option<Content<'de>>,
1521 err: PhantomData<E>,
1522 }
1523
1524 impl<'de, E> EnumDeserializer<'de, E>
1525 where
1526 E: de::Error,
1527 {
1528 pub fn new(variant: Content<'de>, value: Option<Content<'de>>) -> EnumDeserializer<'de, E> {
1529 EnumDeserializer {
1530 variant,
1531 value,
1532 err: PhantomData,
1533 }
1534 }
1535 }
1536
1537 impl<'de, E> de::EnumAccess<'de> for EnumDeserializer<'de, E>
1538 where
1539 E: de::Error,
1540 {
1541 type Error = E;
1542 type Variant = VariantDeserializer<'de, Self::Error>;
1543
1544 fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), E>
1545 where
1546 V: de::DeserializeSeed<'de>,
1547 {
1548 let visitor = VariantDeserializer {
1549 value: self.value,
1550 err: PhantomData,
1551 };
1552 seed.deserialize(ContentDeserializer::new(self.variant))
1553 .map(|v| (v, visitor))
1554 }
1555 }
1556
1557 pub struct VariantDeserializer<'de, E>
1558 where
1559 E: de::Error,
1560 {
1561 value: Option<Content<'de>>,
1562 err: PhantomData<E>,
1563 }
1564
1565 impl<'de, E> de::VariantAccess<'de> for VariantDeserializer<'de, E>
1566 where
1567 E: de::Error,
1568 {
1569 type Error = E;
1570
1571 fn unit_variant(self) -> Result<(), E> {
1572 match self.value {
1573 Some(value) => de::Deserialize::deserialize(ContentDeserializer::new(value)),
1574 None => Ok(()),
1575 }
1576 }
1577
1578 fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, E>
1579 where
1580 T: de::DeserializeSeed<'de>,
1581 {
1582 match self.value {
1583 Some(value) => seed.deserialize(ContentDeserializer::new(value)),
1584 None => Err(de::Error::invalid_type(
1585 de::Unexpected::UnitVariant,
1586 &"newtype variant",
1587 )),
1588 }
1589 }
1590
1591 fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
1592 where
1593 V: de::Visitor<'de>,
1594 {
1595 match self.value {
1596 Some(Content::Seq(v)) => {
1597 de::Deserializer::deserialize_any(SeqDeserializer::new(v.into_iter()), visitor)
1598 }
1599 Some(other) => Err(de::Error::invalid_type(
1600 other.unexpected(),
1601 &"tuple variant",
1602 )),
1603 None => Err(de::Error::invalid_type(
1604 de::Unexpected::UnitVariant,
1605 &"tuple variant",
1606 )),
1607 }
1608 }
1609
1610 fn struct_variant<V>(
1611 self,
1612 _fields: &'static [&'static str],
1613 visitor: V,
1614 ) -> Result<V::Value, Self::Error>
1615 where
1616 V: de::Visitor<'de>,
1617 {
1618 match self.value {
1619 Some(Content::Map(v)) => {
1620 de::Deserializer::deserialize_any(MapDeserializer::new(v.into_iter()), visitor)
1621 }
1622 Some(Content::Seq(v)) => {
1623 de::Deserializer::deserialize_any(SeqDeserializer::new(v.into_iter()), visitor)
1624 }
1625 Some(other) => Err(de::Error::invalid_type(
1626 other.unexpected(),
1627 &"struct variant",
1628 )),
1629 None => Err(de::Error::invalid_type(
1630 de::Unexpected::UnitVariant,
1631 &"struct variant",
1632 )),
1633 }
1634 }
1635 }
1636
1637 pub struct ContentRefDeserializer<'a, 'de: 'a, E> {
1639 content: &'a Content<'de>,
1640 err: PhantomData<E>,
1641 }
1642
1643 impl<'a, 'de, E> ContentRefDeserializer<'a, 'de, E>
1644 where
1645 E: de::Error,
1646 {
1647 #[cold]
1648 fn invalid_type(self, exp: &Expected) -> E {
1649 de::Error::invalid_type(self.content.unexpected(), exp)
1650 }
1651
1652 fn deserialize_integer<V>(self, visitor: V) -> Result<V::Value, E>
1653 where
1654 V: Visitor<'de>,
1655 {
1656 match *self.content {
1657 Content::U8(v) => visitor.visit_u8(v),
1658 Content::U16(v) => visitor.visit_u16(v),
1659 Content::U32(v) => visitor.visit_u32(v),
1660 Content::U64(v) => visitor.visit_u64(v),
1661 Content::I8(v) => visitor.visit_i8(v),
1662 Content::I16(v) => visitor.visit_i16(v),
1663 Content::I32(v) => visitor.visit_i32(v),
1664 Content::I64(v) => visitor.visit_i64(v),
1665 _ => Err(self.invalid_type(&visitor)),
1666 }
1667 }
1668
1669 fn deserialize_float<V>(self, visitor: V) -> Result<V::Value, E>
1670 where
1671 V: Visitor<'de>,
1672 {
1673 match *self.content {
1674 Content::F32(v) => visitor.visit_f32(v),
1675 Content::F64(v) => visitor.visit_f64(v),
1676 Content::U8(v) => visitor.visit_u8(v),
1677 Content::U16(v) => visitor.visit_u16(v),
1678 Content::U32(v) => visitor.visit_u32(v),
1679 Content::U64(v) => visitor.visit_u64(v),
1680 Content::I8(v) => visitor.visit_i8(v),
1681 Content::I16(v) => visitor.visit_i16(v),
1682 Content::I32(v) => visitor.visit_i32(v),
1683 Content::I64(v) => visitor.visit_i64(v),
1684 _ => Err(self.invalid_type(&visitor)),
1685 }
1686 }
1687 }
1688
1689 fn visit_content_seq_ref<'a, 'de, V, E>(
1690 content: &'a [Content<'de>],
1691 visitor: V,
1692 ) -> Result<V::Value, E>
1693 where
1694 V: Visitor<'de>,
1695 E: de::Error,
1696 {
1697 let seq = content.iter().map(ContentRefDeserializer::new);
1698 let mut seq_visitor = SeqDeserializer::new(seq);
1699 let value = tri!(visitor.visit_seq(&mut seq_visitor));
1700 tri!(seq_visitor.end());
1701 Ok(value)
1702 }
1703
1704 fn visit_content_map_ref<'a, 'de, V, E>(
1705 content: &'a [(Content<'de>, Content<'de>)],
1706 visitor: V,
1707 ) -> Result<V::Value, E>
1708 where
1709 V: Visitor<'de>,
1710 E: de::Error,
1711 {
1712 let map = content.iter().map(|(k, v)| {
1713 (
1714 ContentRefDeserializer::new(k),
1715 ContentRefDeserializer::new(v),
1716 )
1717 });
1718 let mut map_visitor = MapDeserializer::new(map);
1719 let value = tri!(visitor.visit_map(&mut map_visitor));
1720 tri!(map_visitor.end());
1721 Ok(value)
1722 }
1723
1724 impl<'de, 'a, E> Deserializer<'de> for ContentRefDeserializer<'a, 'de, E>
1727 where
1728 E: de::Error,
1729 {
1730 type Error = E;
1731
1732 fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, E>
1733 where
1734 V: Visitor<'de>,
1735 {
1736 match *self.content {
1737 Content::Bool(v) => visitor.visit_bool(v),
1738 Content::U8(v) => visitor.visit_u8(v),
1739 Content::U16(v) => visitor.visit_u16(v),
1740 Content::U32(v) => visitor.visit_u32(v),
1741 Content::U64(v) => visitor.visit_u64(v),
1742 Content::I8(v) => visitor.visit_i8(v),
1743 Content::I16(v) => visitor.visit_i16(v),
1744 Content::I32(v) => visitor.visit_i32(v),
1745 Content::I64(v) => visitor.visit_i64(v),
1746 Content::F32(v) => visitor.visit_f32(v),
1747 Content::F64(v) => visitor.visit_f64(v),
1748 Content::Char(v) => visitor.visit_char(v),
1749 Content::String(ref v) => visitor.visit_str(v),
1750 Content::Str(v) => visitor.visit_borrowed_str(v),
1751 Content::ByteBuf(ref v) => visitor.visit_bytes(v),
1752 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
1753 Content::Unit => visitor.visit_unit(),
1754 Content::None => visitor.visit_none(),
1755 Content::Some(ref v) => visitor.visit_some(ContentRefDeserializer::new(v)),
1756 Content::Newtype(ref v) => {
1757 visitor.visit_newtype_struct(ContentRefDeserializer::new(v))
1758 }
1759 Content::Seq(ref v) => visit_content_seq_ref(v, visitor),
1760 Content::Map(ref v) => visit_content_map_ref(v, visitor),
1761 }
1762 }
1763
1764 fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1765 where
1766 V: Visitor<'de>,
1767 {
1768 match *self.content {
1769 Content::Bool(v) => visitor.visit_bool(v),
1770 _ => Err(self.invalid_type(&visitor)),
1771 }
1772 }
1773
1774 fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1775 where
1776 V: Visitor<'de>,
1777 {
1778 self.deserialize_integer(visitor)
1779 }
1780
1781 fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1782 where
1783 V: Visitor<'de>,
1784 {
1785 self.deserialize_integer(visitor)
1786 }
1787
1788 fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1789 where
1790 V: Visitor<'de>,
1791 {
1792 self.deserialize_integer(visitor)
1793 }
1794
1795 fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1796 where
1797 V: Visitor<'de>,
1798 {
1799 self.deserialize_integer(visitor)
1800 }
1801
1802 fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1803 where
1804 V: Visitor<'de>,
1805 {
1806 self.deserialize_integer(visitor)
1807 }
1808
1809 fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1810 where
1811 V: Visitor<'de>,
1812 {
1813 self.deserialize_integer(visitor)
1814 }
1815
1816 fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1817 where
1818 V: Visitor<'de>,
1819 {
1820 self.deserialize_integer(visitor)
1821 }
1822
1823 fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1824 where
1825 V: Visitor<'de>,
1826 {
1827 self.deserialize_integer(visitor)
1828 }
1829
1830 fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1831 where
1832 V: Visitor<'de>,
1833 {
1834 self.deserialize_float(visitor)
1835 }
1836
1837 fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1838 where
1839 V: Visitor<'de>,
1840 {
1841 self.deserialize_float(visitor)
1842 }
1843
1844 fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1845 where
1846 V: Visitor<'de>,
1847 {
1848 match *self.content {
1849 Content::Char(v) => visitor.visit_char(v),
1850 Content::String(ref v) => visitor.visit_str(v),
1851 Content::Str(v) => visitor.visit_borrowed_str(v),
1852 _ => Err(self.invalid_type(&visitor)),
1853 }
1854 }
1855
1856 fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1857 where
1858 V: Visitor<'de>,
1859 {
1860 match *self.content {
1861 Content::String(ref v) => visitor.visit_str(v),
1862 Content::Str(v) => visitor.visit_borrowed_str(v),
1863 Content::ByteBuf(ref v) => visitor.visit_bytes(v),
1864 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
1865 _ => Err(self.invalid_type(&visitor)),
1866 }
1867 }
1868
1869 fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1870 where
1871 V: Visitor<'de>,
1872 {
1873 self.deserialize_str(visitor)
1874 }
1875
1876 fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1877 where
1878 V: Visitor<'de>,
1879 {
1880 match *self.content {
1881 Content::String(ref v) => visitor.visit_str(v),
1882 Content::Str(v) => visitor.visit_borrowed_str(v),
1883 Content::ByteBuf(ref v) => visitor.visit_bytes(v),
1884 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
1885 Content::Seq(ref v) => visit_content_seq_ref(v, visitor),
1886 _ => Err(self.invalid_type(&visitor)),
1887 }
1888 }
1889
1890 fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1891 where
1892 V: Visitor<'de>,
1893 {
1894 self.deserialize_bytes(visitor)
1895 }
1896
1897 fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, E>
1898 where
1899 V: Visitor<'de>,
1900 {
1901 match *self.content {
1904 Content::None => visitor.visit_none(),
1905 Content::Some(ref v) => visitor.visit_some(ContentRefDeserializer::new(v)),
1906 Content::Unit => visitor.visit_unit(),
1907 _ => visitor.visit_some(self),
1915 }
1916 }
1917
1918 fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1919 where
1920 V: Visitor<'de>,
1921 {
1922 match *self.content {
1923 Content::Unit => visitor.visit_unit(),
1924 _ => Err(self.invalid_type(&visitor)),
1925 }
1926 }
1927
1928 fn deserialize_unit_struct<V>(
1929 self,
1930 _name: &'static str,
1931 visitor: V,
1932 ) -> Result<V::Value, Self::Error>
1933 where
1934 V: Visitor<'de>,
1935 {
1936 self.deserialize_unit(visitor)
1937 }
1938
1939 fn deserialize_newtype_struct<V>(self, _name: &str, visitor: V) -> Result<V::Value, E>
1940 where
1941 V: Visitor<'de>,
1942 {
1943 match *self.content {
1946 Content::Newtype(ref v) => {
1947 visitor.visit_newtype_struct(ContentRefDeserializer::new(v))
1948 }
1949 _ => visitor.visit_newtype_struct(self),
1959 }
1960 }
1961
1962 fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1963 where
1964 V: Visitor<'de>,
1965 {
1966 match *self.content {
1967 Content::Seq(ref v) => visit_content_seq_ref(v, visitor),
1968 _ => Err(self.invalid_type(&visitor)),
1969 }
1970 }
1971
1972 fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
1973 where
1974 V: Visitor<'de>,
1975 {
1976 self.deserialize_seq(visitor)
1977 }
1978
1979 fn deserialize_tuple_struct<V>(
1980 self,
1981 _name: &'static str,
1982 _len: usize,
1983 visitor: V,
1984 ) -> Result<V::Value, Self::Error>
1985 where
1986 V: Visitor<'de>,
1987 {
1988 self.deserialize_seq(visitor)
1989 }
1990
1991 fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1992 where
1993 V: Visitor<'de>,
1994 {
1995 match *self.content {
1996 Content::Map(ref v) => visit_content_map_ref(v, visitor),
1997 _ => Err(self.invalid_type(&visitor)),
1998 }
1999 }
2000
2001 fn deserialize_struct<V>(
2002 self,
2003 _name: &'static str,
2004 _fields: &'static [&'static str],
2005 visitor: V,
2006 ) -> Result<V::Value, Self::Error>
2007 where
2008 V: Visitor<'de>,
2009 {
2010 match *self.content {
2011 Content::Seq(ref v) => visit_content_seq_ref(v, visitor),
2012 Content::Map(ref v) => visit_content_map_ref(v, visitor),
2013 _ => Err(self.invalid_type(&visitor)),
2014 }
2015 }
2016
2017 fn deserialize_enum<V>(
2018 self,
2019 _name: &str,
2020 _variants: &'static [&'static str],
2021 visitor: V,
2022 ) -> Result<V::Value, Self::Error>
2023 where
2024 V: Visitor<'de>,
2025 {
2026 let (variant, value) = match *self.content {
2027 Content::Map(ref value) => {
2028 let mut iter = value.iter();
2029 let (variant, value) = match iter.next() {
2030 Some(v) => v,
2031 None => {
2032 return Err(de::Error::invalid_value(
2033 de::Unexpected::Map,
2034 &"map with a single key",
2035 ));
2036 }
2037 };
2038 if iter.next().is_some() {
2040 return Err(de::Error::invalid_value(
2041 de::Unexpected::Map,
2042 &"map with a single key",
2043 ));
2044 }
2045 (variant, Some(value))
2046 }
2047 ref s @ Content::String(_) | ref s @ Content::Str(_) => (s, None),
2048 ref other => {
2049 return Err(de::Error::invalid_type(
2050 other.unexpected(),
2051 &"string or map",
2052 ));
2053 }
2054 };
2055
2056 visitor.visit_enum(EnumRefDeserializer {
2057 variant,
2058 value,
2059 err: PhantomData,
2060 })
2061 }
2062
2063 fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2064 where
2065 V: Visitor<'de>,
2066 {
2067 match *self.content {
2068 Content::String(ref v) => visitor.visit_str(v),
2069 Content::Str(v) => visitor.visit_borrowed_str(v),
2070 Content::ByteBuf(ref v) => visitor.visit_bytes(v),
2071 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
2072 Content::U8(v) => visitor.visit_u8(v),
2073 Content::U64(v) => visitor.visit_u64(v),
2074 _ => Err(self.invalid_type(&visitor)),
2075 }
2076 }
2077
2078 fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2079 where
2080 V: Visitor<'de>,
2081 {
2082 visitor.visit_unit()
2083 }
2084
2085 fn __deserialize_content<V>(
2086 self,
2087 _: actually_private::T,
2088 visitor: V,
2089 ) -> Result<Content<'de>, Self::Error>
2090 where
2091 V: Visitor<'de, Value = Content<'de>>,
2092 {
2093 let _ = visitor;
2094 Ok(self.content.clone())
2095 }
2096 }
2097
2098 impl<'a, 'de, E> ContentRefDeserializer<'a, 'de, E> {
2099 pub fn new(content: &'a Content<'de>) -> Self {
2101 ContentRefDeserializer {
2102 content,
2103 err: PhantomData,
2104 }
2105 }
2106 }
2107
2108 impl<'a, 'de: 'a, E> Copy for ContentRefDeserializer<'a, 'de, E> {}
2109
2110 impl<'a, 'de: 'a, E> Clone for ContentRefDeserializer<'a, 'de, E> {
2111 fn clone(&self) -> Self {
2112 *self
2113 }
2114 }
2115
2116 struct EnumRefDeserializer<'a, 'de: 'a, E>
2117 where
2118 E: de::Error,
2119 {
2120 variant: &'a Content<'de>,
2121 value: Option<&'a Content<'de>>,
2122 err: PhantomData<E>,
2123 }
2124
2125 impl<'de, 'a, E> de::EnumAccess<'de> for EnumRefDeserializer<'a, 'de, E>
2126 where
2127 E: de::Error,
2128 {
2129 type Error = E;
2130 type Variant = VariantRefDeserializer<'a, 'de, Self::Error>;
2131
2132 fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
2133 where
2134 V: de::DeserializeSeed<'de>,
2135 {
2136 let visitor = VariantRefDeserializer {
2137 value: self.value,
2138 err: PhantomData,
2139 };
2140 seed.deserialize(ContentRefDeserializer::new(self.variant))
2141 .map(|v| (v, visitor))
2142 }
2143 }
2144
2145 struct VariantRefDeserializer<'a, 'de: 'a, E>
2146 where
2147 E: de::Error,
2148 {
2149 value: Option<&'a Content<'de>>,
2150 err: PhantomData<E>,
2151 }
2152
2153 impl<'de, 'a, E> de::VariantAccess<'de> for VariantRefDeserializer<'a, 'de, E>
2154 where
2155 E: de::Error,
2156 {
2157 type Error = E;
2158
2159 fn unit_variant(self) -> Result<(), E> {
2160 match self.value {
2161 Some(value) => de::Deserialize::deserialize(ContentRefDeserializer::new(value)),
2162 None => Ok(()),
2167 }
2168 }
2169
2170 fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, E>
2171 where
2172 T: de::DeserializeSeed<'de>,
2173 {
2174 match self.value {
2175 Some(value) => seed.deserialize(ContentRefDeserializer::new(value)),
2181 None => Err(de::Error::invalid_type(
2182 de::Unexpected::UnitVariant,
2183 &"newtype variant",
2184 )),
2185 }
2186 }
2187
2188 fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
2189 where
2190 V: de::Visitor<'de>,
2191 {
2192 match self.value {
2193 Some(Content::Seq(v)) => visit_content_seq_ref(v, visitor),
2200 Some(other) => Err(de::Error::invalid_type(
2201 other.unexpected(),
2202 &"tuple variant",
2203 )),
2204 None => Err(de::Error::invalid_type(
2205 de::Unexpected::UnitVariant,
2206 &"tuple variant",
2207 )),
2208 }
2209 }
2210
2211 fn struct_variant<V>(
2212 self,
2213 _fields: &'static [&'static str],
2214 visitor: V,
2215 ) -> Result<V::Value, Self::Error>
2216 where
2217 V: de::Visitor<'de>,
2218 {
2219 match self.value {
2220 Some(Content::Map(v)) => visit_content_map_ref(v, visitor),
2223 Some(Content::Seq(v)) => visit_content_seq_ref(v, visitor),
2227 Some(other) => Err(de::Error::invalid_type(
2228 other.unexpected(),
2229 &"struct variant",
2230 )),
2231 None => Err(de::Error::invalid_type(
2232 de::Unexpected::UnitVariant,
2233 &"struct variant",
2234 )),
2235 }
2236 }
2237 }
2238
2239 impl<'de, E> de::IntoDeserializer<'de, E> for ContentDeserializer<'de, E>
2240 where
2241 E: de::Error,
2242 {
2243 type Deserializer = Self;
2244
2245 fn into_deserializer(self) -> Self {
2246 self
2247 }
2248 }
2249
2250 impl<'de, 'a, E> de::IntoDeserializer<'de, E> for ContentRefDeserializer<'a, 'de, E>
2251 where
2252 E: de::Error,
2253 {
2254 type Deserializer = Self;
2255
2256 fn into_deserializer(self) -> Self {
2257 self
2258 }
2259 }
2260
2261 pub struct InternallyTaggedUnitVisitor<'a> {
2265 type_name: &'a str,
2266 variant_name: &'a str,
2267 }
2268
2269 impl<'a> InternallyTaggedUnitVisitor<'a> {
2270 pub fn new(type_name: &'a str, variant_name: &'a str) -> Self {
2272 InternallyTaggedUnitVisitor {
2273 type_name,
2274 variant_name,
2275 }
2276 }
2277 }
2278
2279 impl<'de, 'a> Visitor<'de> for InternallyTaggedUnitVisitor<'a> {
2280 type Value = ();
2281
2282 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
2283 write!(
2284 formatter,
2285 "unit variant {}::{}",
2286 self.type_name, self.variant_name
2287 )
2288 }
2289
2290 fn visit_seq<S>(self, _: S) -> Result<(), S::Error>
2291 where
2292 S: SeqAccess<'de>,
2293 {
2294 Ok(())
2295 }
2296
2297 fn visit_map<M>(self, mut access: M) -> Result<(), M::Error>
2298 where
2299 M: MapAccess<'de>,
2300 {
2301 while tri!(access.next_entry::<IgnoredAny, IgnoredAny>()).is_some() {}
2302 Ok(())
2303 }
2304 }
2305
2306 pub struct UntaggedUnitVisitor<'a> {
2310 type_name: &'a str,
2311 variant_name: &'a str,
2312 }
2313
2314 impl<'a> UntaggedUnitVisitor<'a> {
2315 pub fn new(type_name: &'a str, variant_name: &'a str) -> Self {
2317 UntaggedUnitVisitor {
2318 type_name,
2319 variant_name,
2320 }
2321 }
2322 }
2323
2324 impl<'de, 'a> Visitor<'de> for UntaggedUnitVisitor<'a> {
2325 type Value = ();
2326
2327 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
2328 write!(
2329 formatter,
2330 "unit variant {}::{}",
2331 self.type_name, self.variant_name
2332 )
2333 }
2334
2335 fn visit_unit<E>(self) -> Result<(), E>
2336 where
2337 E: de::Error,
2338 {
2339 Ok(())
2340 }
2341
2342 fn visit_none<E>(self) -> Result<(), E>
2343 where
2344 E: de::Error,
2345 {
2346 Ok(())
2347 }
2348 }
2349}
2350
2351pub trait IdentifierDeserializer<'de, E: Error> {
2364 type Deserializer: Deserializer<'de, Error = E>;
2365
2366 fn from(self) -> Self::Deserializer;
2367}
2368
2369pub struct Borrowed<'de, T: 'de + ?Sized>(pub &'de T);
2370
2371impl<'de, E> IdentifierDeserializer<'de, E> for u64
2372where
2373 E: Error,
2374{
2375 type Deserializer = <u64 as IntoDeserializer<'de, E>>::Deserializer;
2376
2377 fn from(self) -> Self::Deserializer {
2378 self.into_deserializer()
2379 }
2380}
2381
2382pub struct StrDeserializer<'a, E> {
2383 value: &'a str,
2384 marker: PhantomData<E>,
2385}
2386
2387impl<'de, 'a, E> Deserializer<'de> for StrDeserializer<'a, E>
2388where
2389 E: Error,
2390{
2391 type Error = E;
2392
2393 fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2394 where
2395 V: Visitor<'de>,
2396 {
2397 visitor.visit_str(self.value)
2398 }
2399
2400 forward_to_deserialize_any! {
2401 bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
2402 bytes byte_buf option unit unit_struct newtype_struct seq tuple
2403 tuple_struct map struct enum identifier ignored_any
2404 }
2405}
2406
2407pub struct BorrowedStrDeserializer<'de, E> {
2408 value: &'de str,
2409 marker: PhantomData<E>,
2410}
2411
2412impl<'de, E> Deserializer<'de> for BorrowedStrDeserializer<'de, E>
2413where
2414 E: Error,
2415{
2416 type Error = E;
2417
2418 fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2419 where
2420 V: Visitor<'de>,
2421 {
2422 visitor.visit_borrowed_str(self.value)
2423 }
2424
2425 forward_to_deserialize_any! {
2426 bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
2427 bytes byte_buf option unit unit_struct newtype_struct seq tuple
2428 tuple_struct map struct enum identifier ignored_any
2429 }
2430}
2431
2432impl<'a, E> IdentifierDeserializer<'a, E> for &'a str
2433where
2434 E: Error,
2435{
2436 type Deserializer = StrDeserializer<'a, E>;
2437
2438 fn from(self) -> Self::Deserializer {
2439 StrDeserializer {
2440 value: self,
2441 marker: PhantomData,
2442 }
2443 }
2444}
2445
2446impl<'de, E> IdentifierDeserializer<'de, E> for Borrowed<'de, str>
2447where
2448 E: Error,
2449{
2450 type Deserializer = BorrowedStrDeserializer<'de, E>;
2451
2452 fn from(self) -> Self::Deserializer {
2453 BorrowedStrDeserializer {
2454 value: self.0,
2455 marker: PhantomData,
2456 }
2457 }
2458}
2459
2460impl<'a, E> IdentifierDeserializer<'a, E> for &'a [u8]
2461where
2462 E: Error,
2463{
2464 type Deserializer = BytesDeserializer<'a, E>;
2465
2466 fn from(self) -> Self::Deserializer {
2467 BytesDeserializer::new(self)
2468 }
2469}
2470
2471impl<'de, E> IdentifierDeserializer<'de, E> for Borrowed<'de, [u8]>
2472where
2473 E: Error,
2474{
2475 type Deserializer = BorrowedBytesDeserializer<'de, E>;
2476
2477 fn from(self) -> Self::Deserializer {
2478 BorrowedBytesDeserializer::new(self.0)
2479 }
2480}
2481
2482#[cfg(any(feature = "std", feature = "alloc"))]
2483pub struct FlatMapDeserializer<'a, 'de: 'a, E>(
2484 pub &'a mut Vec<Option<(Content<'de>, Content<'de>)>>,
2485 pub PhantomData<E>,
2486);
2487
2488#[cfg(any(feature = "std", feature = "alloc"))]
2489impl<'a, 'de, E> FlatMapDeserializer<'a, 'de, E>
2490where
2491 E: Error,
2492{
2493 fn deserialize_other<V>() -> Result<V, E> {
2494 Err(Error::custom("can only flatten structs and maps"))
2495 }
2496}
2497
2498#[cfg(any(feature = "std", feature = "alloc"))]
2499macro_rules! forward_to_deserialize_other {
2500 ($($func:ident ($($arg:ty),*))*) => {
2501 $(
2502 fn $func<V>(self, $(_: $arg,)* _visitor: V) -> Result<V::Value, Self::Error>
2503 where
2504 V: Visitor<'de>,
2505 {
2506 Self::deserialize_other()
2507 }
2508 )*
2509 }
2510}
2511
2512#[cfg(any(feature = "std", feature = "alloc"))]
2513impl<'a, 'de, E> Deserializer<'de> for FlatMapDeserializer<'a, 'de, E>
2514where
2515 E: Error,
2516{
2517 type Error = E;
2518
2519 fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2520 where
2521 V: Visitor<'de>,
2522 {
2523 self.deserialize_map(visitor)
2524 }
2525
2526 fn deserialize_enum<V>(
2527 self,
2528 name: &'static str,
2529 variants: &'static [&'static str],
2530 visitor: V,
2531 ) -> Result<V::Value, Self::Error>
2532 where
2533 V: Visitor<'de>,
2534 {
2535 for entry in self.0 {
2536 if let Some((key, value)) = flat_map_take_entry(entry, variants) {
2537 return visitor.visit_enum(EnumDeserializer::new(key, Some(value)));
2538 }
2539 }
2540
2541 Err(Error::custom(format_args!(
2542 "no variant of enum {} found in flattened data",
2543 name
2544 )))
2545 }
2546
2547 fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2548 where
2549 V: Visitor<'de>,
2550 {
2551 visitor.visit_map(FlatMapAccess {
2552 iter: self.0.iter(),
2553 pending_content: None,
2554 _marker: PhantomData,
2555 })
2556 }
2557
2558 fn deserialize_struct<V>(
2559 self,
2560 _: &'static str,
2561 fields: &'static [&'static str],
2562 visitor: V,
2563 ) -> Result<V::Value, Self::Error>
2564 where
2565 V: Visitor<'de>,
2566 {
2567 visitor.visit_map(FlatStructAccess {
2568 iter: self.0.iter_mut(),
2569 pending_content: None,
2570 fields,
2571 _marker: PhantomData,
2572 })
2573 }
2574
2575 fn deserialize_newtype_struct<V>(self, _name: &str, visitor: V) -> Result<V::Value, Self::Error>
2576 where
2577 V: Visitor<'de>,
2578 {
2579 visitor.visit_newtype_struct(self)
2580 }
2581
2582 fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2583 where
2584 V: Visitor<'de>,
2585 {
2586 match visitor.__private_visit_untagged_option(self) {
2587 Ok(value) => Ok(value),
2588 Err(()) => Self::deserialize_other(),
2589 }
2590 }
2591
2592 fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2593 where
2594 V: Visitor<'de>,
2595 {
2596 visitor.visit_unit()
2597 }
2598
2599 fn deserialize_unit_struct<V>(
2600 self,
2601 _name: &'static str,
2602 visitor: V,
2603 ) -> Result<V::Value, Self::Error>
2604 where
2605 V: Visitor<'de>,
2606 {
2607 visitor.visit_unit()
2608 }
2609
2610 fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
2611 where
2612 V: Visitor<'de>,
2613 {
2614 visitor.visit_unit()
2615 }
2616
2617 forward_to_deserialize_other! {
2618 deserialize_bool()
2619 deserialize_i8()
2620 deserialize_i16()
2621 deserialize_i32()
2622 deserialize_i64()
2623 deserialize_u8()
2624 deserialize_u16()
2625 deserialize_u32()
2626 deserialize_u64()
2627 deserialize_f32()
2628 deserialize_f64()
2629 deserialize_char()
2630 deserialize_str()
2631 deserialize_string()
2632 deserialize_bytes()
2633 deserialize_byte_buf()
2634 deserialize_seq()
2635 deserialize_tuple(usize)
2636 deserialize_tuple_struct(&'static str, usize)
2637 deserialize_identifier()
2638 }
2639}
2640
2641#[cfg(any(feature = "std", feature = "alloc"))]
2642struct FlatMapAccess<'a, 'de: 'a, E> {
2643 iter: slice::Iter<'a, Option<(Content<'de>, Content<'de>)>>,
2644 pending_content: Option<&'a Content<'de>>,
2645 _marker: PhantomData<E>,
2646}
2647
2648#[cfg(any(feature = "std", feature = "alloc"))]
2649impl<'a, 'de, E> MapAccess<'de> for FlatMapAccess<'a, 'de, E>
2650where
2651 E: Error,
2652{
2653 type Error = E;
2654
2655 fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
2656 where
2657 T: DeserializeSeed<'de>,
2658 {
2659 for item in &mut self.iter {
2660 if let Some((ref key, ref content)) = *item {
2662 self.pending_content = Some(content);
2667 return seed.deserialize(ContentRefDeserializer::new(key)).map(Some);
2668 }
2669 }
2670 Ok(None)
2671 }
2672
2673 fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error>
2674 where
2675 T: DeserializeSeed<'de>,
2676 {
2677 match self.pending_content.take() {
2678 Some(value) => seed.deserialize(ContentRefDeserializer::new(value)),
2679 None => Err(Error::custom("value is missing")),
2680 }
2681 }
2682}
2683
2684#[cfg(any(feature = "std", feature = "alloc"))]
2685struct FlatStructAccess<'a, 'de: 'a, E> {
2686 iter: slice::IterMut<'a, Option<(Content<'de>, Content<'de>)>>,
2687 pending_content: Option<Content<'de>>,
2688 fields: &'static [&'static str],
2689 _marker: PhantomData<E>,
2690}
2691
2692#[cfg(any(feature = "std", feature = "alloc"))]
2693impl<'a, 'de, E> MapAccess<'de> for FlatStructAccess<'a, 'de, E>
2694where
2695 E: Error,
2696{
2697 type Error = E;
2698
2699 fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
2700 where
2701 T: DeserializeSeed<'de>,
2702 {
2703 for entry in self.iter.by_ref() {
2704 if let Some((key, content)) = flat_map_take_entry(entry, self.fields) {
2705 self.pending_content = Some(content);
2706 return seed.deserialize(ContentDeserializer::new(key)).map(Some);
2707 }
2708 }
2709 Ok(None)
2710 }
2711
2712 fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error>
2713 where
2714 T: DeserializeSeed<'de>,
2715 {
2716 match self.pending_content.take() {
2717 Some(value) => seed.deserialize(ContentDeserializer::new(value)),
2718 None => Err(Error::custom("value is missing")),
2719 }
2720 }
2721}
2722
2723#[cfg(any(feature = "std", feature = "alloc"))]
2726fn flat_map_take_entry<'de>(
2727 entry: &mut Option<(Content<'de>, Content<'de>)>,
2728 recognized: &[&str],
2729) -> Option<(Content<'de>, Content<'de>)> {
2730 let is_recognized = match entry {
2734 None => false,
2735 Some((k, _v)) => k.as_str().map_or(false, |name| recognized.contains(&name)),
2736 };
2737
2738 if is_recognized {
2739 entry.take()
2740 } else {
2741 None
2742 }
2743}
2744
2745pub struct AdjacentlyTaggedEnumVariantSeed<F> {
2746 pub enum_name: &'static str,
2747 pub variants: &'static [&'static str],
2748 pub fields_enum: PhantomData<F>,
2749}
2750
2751pub struct AdjacentlyTaggedEnumVariantVisitor<F> {
2752 enum_name: &'static str,
2753 fields_enum: PhantomData<F>,
2754}
2755
2756impl<'de, F> Visitor<'de> for AdjacentlyTaggedEnumVariantVisitor<F>
2757where
2758 F: Deserialize<'de>,
2759{
2760 type Value = F;
2761
2762 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
2763 write!(formatter, "variant of enum {}", self.enum_name)
2764 }
2765
2766 fn visit_enum<A>(self, data: A) -> Result<Self::Value, A::Error>
2767 where
2768 A: EnumAccess<'de>,
2769 {
2770 let (variant, variant_access) = tri!(data.variant());
2771 tri!(variant_access.unit_variant());
2772 Ok(variant)
2773 }
2774}
2775
2776impl<'de, F> DeserializeSeed<'de> for AdjacentlyTaggedEnumVariantSeed<F>
2777where
2778 F: Deserialize<'de>,
2779{
2780 type Value = F;
2781
2782 fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
2783 where
2784 D: Deserializer<'de>,
2785 {
2786 deserializer.deserialize_enum(
2787 self.enum_name,
2788 self.variants,
2789 AdjacentlyTaggedEnumVariantVisitor {
2790 enum_name: self.enum_name,
2791 fields_enum: PhantomData,
2792 },
2793 )
2794 }
2795}