Coverage Report

Created: 2026-09-28 06:56

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