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