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