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