Coverage Report

Created: 2025-05-07 06:59

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