Coverage Report

Created: 2026-01-09 07:04

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