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