1use self::utils::{get_unexpected_i128, get_unexpected_u128};
18use crate::{
19 prelude::*,
20 utils::{size_hint_cautious, size_hint_from_bounds},
21};
22
23pub(crate) enum Content<'de> {
26 Bool(bool),
27
28 U8(u8),
29 U16(u16),
30 U32(u32),
31 U64(u64),
32 U128(u128),
33
34 I8(i8),
35 I16(i16),
36 I32(i32),
37 I64(i64),
38 I128(i128),
39
40 F32(f32),
41 F64(f64),
42
43 Char(char),
44 String(String),
45 Str(&'de str),
46 ByteBuf(Vec<u8>),
47 Bytes(&'de [u8]),
48
49 None,
50 Some(Box<Content<'de>>),
51
52 Unit,
53 Newtype(Box<Content<'de>>),
54 Seq(Vec<Content<'de>>),
55 Map(Vec<(Content<'de>, Content<'de>)>),
56}
57
58impl Content<'_> {
59 #[cold]
60 fn unexpected<'a>(&'a self, buf: &'a mut [u8; 58]) -> Unexpected<'a> {
61 match *self {
62 Content::Bool(b) => Unexpected::Bool(b),
63 Content::U8(n) => Unexpected::Unsigned(u64::from(n)),
64 Content::U16(n) => Unexpected::Unsigned(u64::from(n)),
65 Content::U32(n) => Unexpected::Unsigned(u64::from(n)),
66 Content::U64(n) => Unexpected::Unsigned(n),
67 Content::U128(n) => get_unexpected_u128(n, buf),
68 Content::I8(n) => Unexpected::Signed(i64::from(n)),
69 Content::I16(n) => Unexpected::Signed(i64::from(n)),
70 Content::I32(n) => Unexpected::Signed(i64::from(n)),
71 Content::I64(n) => Unexpected::Signed(n),
72 Content::I128(n) => get_unexpected_i128(n, buf),
73 Content::F32(f) => Unexpected::Float(f64::from(f)),
74 Content::F64(f) => Unexpected::Float(f),
75 Content::Char(c) => Unexpected::Char(c),
76 Content::String(ref s) => Unexpected::Str(s),
77 Content::Str(s) => Unexpected::Str(s),
78 Content::ByteBuf(ref b) => Unexpected::Bytes(b),
79 Content::Bytes(b) => Unexpected::Bytes(b),
80 Content::None | Content::Some(_) => Unexpected::Option,
81 Content::Unit => Unexpected::Unit,
82 Content::Newtype(_) => Unexpected::NewtypeStruct,
83 Content::Seq(_) => Unexpected::Seq,
84 Content::Map(_) => Unexpected::Map,
85 }
86 }
87}
88
89impl<'de> Deserialize<'de> for Content<'de> {
90 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
91 where
92 D: Deserializer<'de>,
93 {
94 let visitor = ContentVisitor { value: PhantomData };
97 deserializer.deserialize_any(visitor)
98 }
99}
100
101struct ContentVisitor<'de> {
102 value: PhantomData<Content<'de>>,
103}
104
105impl<'de> Visitor<'de> for ContentVisitor<'de> {
106 type Value = Content<'de>;
107
108 fn expecting(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
109 fmt.write_str("any value")
110 }
111
112 fn visit_bool<F>(self, value: bool) -> Result<Self::Value, F>
113 where
114 F: DeError,
115 {
116 Ok(Content::Bool(value))
117 }
118
119 fn visit_i8<F>(self, value: i8) -> Result<Self::Value, F>
120 where
121 F: DeError,
122 {
123 Ok(Content::I8(value))
124 }
125
126 fn visit_i16<F>(self, value: i16) -> Result<Self::Value, F>
127 where
128 F: DeError,
129 {
130 Ok(Content::I16(value))
131 }
132
133 fn visit_i32<F>(self, value: i32) -> Result<Self::Value, F>
134 where
135 F: DeError,
136 {
137 Ok(Content::I32(value))
138 }
139
140 fn visit_i64<F>(self, value: i64) -> Result<Self::Value, F>
141 where
142 F: DeError,
143 {
144 Ok(Content::I64(value))
145 }
146
147 fn visit_i128<F>(self, value: i128) -> Result<Self::Value, F>
148 where
149 F: DeError,
150 {
151 Ok(Content::I128(value))
152 }
153
154 fn visit_u8<F>(self, value: u8) -> Result<Self::Value, F>
155 where
156 F: DeError,
157 {
158 Ok(Content::U8(value))
159 }
160
161 fn visit_u16<F>(self, value: u16) -> Result<Self::Value, F>
162 where
163 F: DeError,
164 {
165 Ok(Content::U16(value))
166 }
167
168 fn visit_u32<F>(self, value: u32) -> Result<Self::Value, F>
169 where
170 F: DeError,
171 {
172 Ok(Content::U32(value))
173 }
174
175 fn visit_u64<F>(self, value: u64) -> Result<Self::Value, F>
176 where
177 F: DeError,
178 {
179 Ok(Content::U64(value))
180 }
181
182 fn visit_u128<F>(self, value: u128) -> Result<Self::Value, F>
183 where
184 F: DeError,
185 {
186 Ok(Content::U128(value))
187 }
188
189 fn visit_f32<F>(self, value: f32) -> Result<Self::Value, F>
190 where
191 F: DeError,
192 {
193 Ok(Content::F32(value))
194 }
195
196 fn visit_f64<F>(self, value: f64) -> Result<Self::Value, F>
197 where
198 F: DeError,
199 {
200 Ok(Content::F64(value))
201 }
202
203 fn visit_char<F>(self, value: char) -> Result<Self::Value, F>
204 where
205 F: DeError,
206 {
207 Ok(Content::Char(value))
208 }
209
210 fn visit_str<F>(self, value: &str) -> Result<Self::Value, F>
211 where
212 F: DeError,
213 {
214 Ok(Content::String(value.into()))
215 }
216
217 fn visit_borrowed_str<F>(self, value: &'de str) -> Result<Self::Value, F>
218 where
219 F: DeError,
220 {
221 Ok(Content::Str(value))
222 }
223
224 fn visit_string<F>(self, value: String) -> Result<Self::Value, F>
225 where
226 F: DeError,
227 {
228 Ok(Content::String(value))
229 }
230
231 fn visit_bytes<F>(self, value: &[u8]) -> Result<Self::Value, F>
232 where
233 F: DeError,
234 {
235 Ok(Content::ByteBuf(value.into()))
236 }
237
238 fn visit_borrowed_bytes<F>(self, value: &'de [u8]) -> Result<Self::Value, F>
239 where
240 F: DeError,
241 {
242 Ok(Content::Bytes(value))
243 }
244
245 fn visit_byte_buf<F>(self, value: Vec<u8>) -> Result<Self::Value, F>
246 where
247 F: DeError,
248 {
249 Ok(Content::ByteBuf(value))
250 }
251
252 fn visit_unit<F>(self) -> Result<Self::Value, F>
253 where
254 F: DeError,
255 {
256 Ok(Content::Unit)
257 }
258
259 fn visit_none<F>(self) -> Result<Self::Value, F>
260 where
261 F: DeError,
262 {
263 Ok(Content::None)
264 }
265
266 fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
267 where
268 D: Deserializer<'de>,
269 {
270 Deserialize::deserialize(deserializer).map(|v| Content::Some(Box::new(v)))
271 }
272
273 fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
274 where
275 D: Deserializer<'de>,
276 {
277 Deserialize::deserialize(deserializer).map(|v| Content::Newtype(Box::new(v)))
278 }
279
280 fn visit_seq<V>(self, mut visitor: V) -> Result<Self::Value, V::Error>
281 where
282 V: SeqAccess<'de>,
283 {
284 let mut vec = Vec::with_capacity(size_hint_cautious::<Content<'_>>(visitor.size_hint()));
285 while let Some(e) = visitor.next_element()? {
286 vec.push(e);
287 }
288 Ok(Content::Seq(vec))
289 }
290
291 fn visit_map<V>(self, mut visitor: V) -> Result<Self::Value, V::Error>
292 where
293 V: MapAccess<'de>,
294 {
295 let mut vec = Vec::with_capacity(size_hint_cautious::<(Content<'_>, Content<'_>)>(
296 visitor.size_hint(),
297 ));
298 while let Some(kv) = visitor.next_entry()? {
299 vec.push(kv);
300 }
301 Ok(Content::Map(vec))
302 }
303
304 fn visit_enum<V>(self, _visitor: V) -> Result<Self::Value, V::Error>
305 where
306 V: EnumAccess<'de>,
307 {
308 Err(DeError::custom(
309 "untagged and internally tagged enums do not support enum input",
310 ))
311 }
312}
313
314pub(crate) struct ContentDeserializer<'de, E> {
315 is_human_readable: bool,
316 content: Content<'de>,
317 err: PhantomData<E>,
318}
319
320impl<'de, E> ContentDeserializer<'de, E>
321where
322 E: DeError,
323{
324 #[cold]
325 fn invalid_type(self, exp: &dyn Expected) -> E {
326 let mut buf = [0; 58];
327 DeError::invalid_type(self.content.unexpected(&mut buf), exp)
328 }
329
330 fn deserialize_integer<V>(self, visitor: V) -> Result<V::Value, E>
331 where
332 V: Visitor<'de>,
333 {
334 match self.content {
335 Content::U8(v) => visitor.visit_u8(v),
336 Content::U16(v) => visitor.visit_u16(v),
337 Content::U32(v) => visitor.visit_u32(v),
338 Content::U64(v) => visitor.visit_u64(v),
339 Content::U128(v) => visitor.visit_u128(v),
340 Content::I8(v) => visitor.visit_i8(v),
341 Content::I16(v) => visitor.visit_i16(v),
342 Content::I32(v) => visitor.visit_i32(v),
343 Content::I64(v) => visitor.visit_i64(v),
344 Content::I128(v) => visitor.visit_i128(v),
345 _ => Err(self.invalid_type(&visitor)),
346 }
347 }
348
349 fn deserialize_float<V>(self, visitor: V) -> Result<V::Value, E>
350 where
351 V: Visitor<'de>,
352 {
353 match self.content {
354 Content::F32(v) => visitor.visit_f32(v),
355 Content::F64(v) => visitor.visit_f64(v),
356 Content::U8(v) => visitor.visit_u8(v),
357 Content::U16(v) => visitor.visit_u16(v),
358 Content::U32(v) => visitor.visit_u32(v),
359 Content::U64(v) => visitor.visit_u64(v),
360 Content::U128(v) => visitor.visit_u128(v),
361 Content::I8(v) => visitor.visit_i8(v),
362 Content::I16(v) => visitor.visit_i16(v),
363 Content::I32(v) => visitor.visit_i32(v),
364 Content::I64(v) => visitor.visit_i64(v),
365 Content::I128(v) => visitor.visit_i128(v),
366 _ => Err(self.invalid_type(&visitor)),
367 }
368 }
369}
370
371fn visit_content_seq<'de, V, E>(
372 content: Vec<Content<'de>>,
373 visitor: V,
374 is_human_readable: bool,
375) -> Result<V::Value, E>
376where
377 V: Visitor<'de>,
378 E: DeError,
379{
380 let seq = content
381 .into_iter()
382 .map(|x| ContentDeserializer::new(x, is_human_readable));
383 let mut seq_visitor = serde::de::value::SeqDeserializer::new(seq);
384 let value = visitor.visit_seq(&mut seq_visitor)?;
385 seq_visitor.end()?;
386 Ok(value)
387}
388
389fn visit_content_map<'de, V, E>(
390 content: Vec<(Content<'de>, Content<'de>)>,
391 visitor: V,
392 is_human_readable: bool,
393) -> Result<V::Value, E>
394where
395 V: Visitor<'de>,
396 E: DeError,
397{
398 let map = content.into_iter().map(|(k, v)| {
399 (
400 ContentDeserializer::new(k, is_human_readable),
401 ContentDeserializer::new(v, is_human_readable),
402 )
403 });
404 let mut map_visitor = serde::de::value::MapDeserializer::new(map);
405 let value = visitor.visit_map(&mut map_visitor)?;
406 map_visitor.end()?;
407 Ok(value)
408}
409
410impl<'de, E> Deserializer<'de> for ContentDeserializer<'de, E>
413where
414 E: DeError,
415{
416 type Error = E;
417
418 #[inline]
419 fn is_human_readable(&self) -> bool {
420 self.is_human_readable
421 }
422
423 fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
424 where
425 V: Visitor<'de>,
426 {
427 match self.content {
428 Content::Bool(v) => visitor.visit_bool(v),
429 Content::U8(v) => visitor.visit_u8(v),
430 Content::U16(v) => visitor.visit_u16(v),
431 Content::U32(v) => visitor.visit_u32(v),
432 Content::U64(v) => visitor.visit_u64(v),
433 Content::U128(v) => visitor.visit_u128(v),
434 Content::I8(v) => visitor.visit_i8(v),
435 Content::I16(v) => visitor.visit_i16(v),
436 Content::I32(v) => visitor.visit_i32(v),
437 Content::I64(v) => visitor.visit_i64(v),
438 Content::I128(v) => visitor.visit_i128(v),
439 Content::F32(v) => visitor.visit_f32(v),
440 Content::F64(v) => visitor.visit_f64(v),
441 Content::Char(v) => visitor.visit_char(v),
442 Content::String(v) => visitor.visit_string(v),
443 Content::Str(v) => visitor.visit_borrowed_str(v),
444 Content::ByteBuf(v) => visitor.visit_byte_buf(v),
445 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
446 Content::Unit => visitor.visit_unit(),
447 Content::None => visitor.visit_none(),
448 Content::Some(v) => {
449 visitor.visit_some(ContentDeserializer::new(*v, self.is_human_readable))
450 }
451 Content::Newtype(v) => {
452 visitor.visit_newtype_struct(ContentDeserializer::new(*v, self.is_human_readable))
453 }
454 Content::Seq(v) => visit_content_seq(v, visitor, self.is_human_readable),
455 Content::Map(v) => visit_content_map(v, visitor, self.is_human_readable),
456 }
457 }
458
459 fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
460 where
461 V: Visitor<'de>,
462 {
463 match self.content {
464 Content::Bool(v) => visitor.visit_bool(v),
465 _ => Err(self.invalid_type(&visitor)),
466 }
467 }
468
469 fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
470 where
471 V: Visitor<'de>,
472 {
473 self.deserialize_integer(visitor)
474 }
475
476 fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
477 where
478 V: Visitor<'de>,
479 {
480 self.deserialize_integer(visitor)
481 }
482
483 fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
484 where
485 V: Visitor<'de>,
486 {
487 self.deserialize_integer(visitor)
488 }
489
490 fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
491 where
492 V: Visitor<'de>,
493 {
494 self.deserialize_integer(visitor)
495 }
496
497 fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
498 where
499 V: Visitor<'de>,
500 {
501 self.deserialize_integer(visitor)
502 }
503
504 fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
505 where
506 V: Visitor<'de>,
507 {
508 self.deserialize_integer(visitor)
509 }
510
511 fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
512 where
513 V: Visitor<'de>,
514 {
515 self.deserialize_integer(visitor)
516 }
517
518 fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
519 where
520 V: Visitor<'de>,
521 {
522 self.deserialize_integer(visitor)
523 }
524
525 fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
526 where
527 V: Visitor<'de>,
528 {
529 self.deserialize_integer(visitor)
530 }
531
532 fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
533 where
534 V: Visitor<'de>,
535 {
536 self.deserialize_integer(visitor)
537 }
538
539 fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
540 where
541 V: Visitor<'de>,
542 {
543 self.deserialize_float(visitor)
544 }
545
546 fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
547 where
548 V: Visitor<'de>,
549 {
550 self.deserialize_float(visitor)
551 }
552
553 fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
554 where
555 V: Visitor<'de>,
556 {
557 match self.content {
558 Content::Char(v) => visitor.visit_char(v),
559 Content::String(v) => visitor.visit_string(v),
560 Content::Str(v) => visitor.visit_borrowed_str(v),
561 _ => Err(self.invalid_type(&visitor)),
562 }
563 }
564
565 fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
566 where
567 V: Visitor<'de>,
568 {
569 self.deserialize_string(visitor)
570 }
571
572 fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
573 where
574 V: Visitor<'de>,
575 {
576 match self.content {
577 Content::String(v) => visitor.visit_string(v),
578 Content::Str(v) => visitor.visit_borrowed_str(v),
579 Content::ByteBuf(v) => visitor.visit_byte_buf(v),
580 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
581 _ => Err(self.invalid_type(&visitor)),
582 }
583 }
584
585 fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
586 where
587 V: Visitor<'de>,
588 {
589 self.deserialize_byte_buf(visitor)
590 }
591
592 fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
593 where
594 V: Visitor<'de>,
595 {
596 match self.content {
597 Content::String(v) => visitor.visit_string(v),
598 Content::Str(v) => visitor.visit_borrowed_str(v),
599 Content::ByteBuf(v) => visitor.visit_byte_buf(v),
600 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
601 Content::Seq(v) => visit_content_seq(v, visitor, self.is_human_readable),
602 _ => Err(self.invalid_type(&visitor)),
603 }
604 }
605
606 fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
607 where
608 V: Visitor<'de>,
609 {
610 match self.content {
611 Content::None => visitor.visit_none(),
612 Content::Some(v) => {
613 visitor.visit_some(ContentDeserializer::new(*v, self.is_human_readable))
614 }
615 Content::Unit => visitor.visit_unit(),
616 _ => visitor.visit_some(self),
617 }
618 }
619
620 fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
621 where
622 V: Visitor<'de>,
623 {
624 match self.content {
625 Content::Unit => visitor.visit_unit(),
626
627 Content::Map(ref v) if v.is_empty() => visitor.visit_unit(),
637 _ => Err(self.invalid_type(&visitor)),
638 }
639 }
640
641 fn deserialize_unit_struct<V>(
642 self,
643 _name: &'static str,
644 visitor: V,
645 ) -> Result<V::Value, Self::Error>
646 where
647 V: Visitor<'de>,
648 {
649 match self.content {
650 Content::Map(ref v) if v.is_empty() => visitor.visit_unit(),
665 Content::Seq(ref v) if v.is_empty() => visitor.visit_unit(),
666 _ => self.deserialize_any(visitor),
667 }
668 }
669
670 fn deserialize_newtype_struct<V>(self, _name: &str, visitor: V) -> Result<V::Value, Self::Error>
671 where
672 V: Visitor<'de>,
673 {
674 match self.content {
675 Content::Newtype(v) => {
676 visitor.visit_newtype_struct(ContentDeserializer::new(*v, self.is_human_readable))
677 }
678 _ => visitor.visit_newtype_struct(self),
679 }
680 }
681
682 fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
683 where
684 V: Visitor<'de>,
685 {
686 match self.content {
687 Content::Seq(v) => visit_content_seq(v, visitor, self.is_human_readable),
688 _ => Err(self.invalid_type(&visitor)),
689 }
690 }
691
692 fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
693 where
694 V: Visitor<'de>,
695 {
696 self.deserialize_seq(visitor)
697 }
698
699 fn deserialize_tuple_struct<V>(
700 self,
701 _name: &'static str,
702 _len: usize,
703 visitor: V,
704 ) -> Result<V::Value, Self::Error>
705 where
706 V: Visitor<'de>,
707 {
708 self.deserialize_seq(visitor)
709 }
710
711 fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
712 where
713 V: Visitor<'de>,
714 {
715 match self.content {
716 Content::Map(v) => visit_content_map(v, visitor, self.is_human_readable),
717 _ => Err(self.invalid_type(&visitor)),
718 }
719 }
720
721 fn deserialize_struct<V>(
722 self,
723 _name: &'static str,
724 _fields: &'static [&'static str],
725 visitor: V,
726 ) -> Result<V::Value, Self::Error>
727 where
728 V: Visitor<'de>,
729 {
730 match self.content {
731 Content::Seq(v) => visit_content_seq(v, visitor, self.is_human_readable),
732 Content::Map(v) => visit_content_map(v, visitor, self.is_human_readable),
733 _ => Err(self.invalid_type(&visitor)),
734 }
735 }
736
737 fn deserialize_enum<V>(
738 self,
739 _name: &str,
740 _variants: &'static [&'static str],
741 visitor: V,
742 ) -> Result<V::Value, Self::Error>
743 where
744 V: Visitor<'de>,
745 {
746 let (variant, value) = match self.content {
747 Content::Map(value) => {
748 let mut iter = value.into_iter();
749 let (variant, value) = match iter.next() {
750 Some(v) => v,
751 None => {
752 return Err(DeError::invalid_value(
753 Unexpected::Map,
754 &"map with a single key",
755 ));
756 }
757 };
758 if iter.next().is_some() {
760 return Err(DeError::invalid_value(
761 Unexpected::Map,
762 &"map with a single key",
763 ));
764 }
765 (variant, Some(value))
766 }
767 s @ Content::String(_) | s @ Content::Str(_) => (s, None),
768 other => {
769 let mut buf = [0; 58];
770 return Err(DeError::invalid_type(
771 other.unexpected(&mut buf),
772 &"string or map",
773 ));
774 }
775 };
776
777 visitor.visit_enum(EnumDeserializer::new(
778 variant,
779 value,
780 self.is_human_readable,
781 ))
782 }
783
784 fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
785 where
786 V: Visitor<'de>,
787 {
788 match self.content {
789 Content::String(v) => visitor.visit_string(v),
790 Content::Str(v) => visitor.visit_borrowed_str(v),
791 Content::ByteBuf(v) => visitor.visit_byte_buf(v),
792 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
793 Content::U8(v) => visitor.visit_u8(v),
794 Content::U64(v) => visitor.visit_u64(v),
795 _ => Err(self.invalid_type(&visitor)),
796 }
797 }
798
799 fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
800 where
801 V: Visitor<'de>,
802 {
803 drop(self);
804 visitor.visit_unit()
805 }
806}
807
808impl<'de, E> ContentDeserializer<'de, E> {
809 pub(crate) fn new(content: Content<'de>, is_human_readable: bool) -> Self {
811 ContentDeserializer {
812 is_human_readable,
813 content,
814 err: PhantomData,
815 }
816 }
817}
818
819struct EnumDeserializer<'de, E>
820where
821 E: DeError,
822{
823 is_human_readable: bool,
824 variant: Content<'de>,
825 value: Option<Content<'de>>,
826 err: PhantomData<E>,
827}
828
829impl<'de, E> EnumDeserializer<'de, E>
830where
831 E: DeError,
832{
833 pub fn new(
834 variant: Content<'de>,
835 value: Option<Content<'de>>,
836 is_human_readable: bool,
837 ) -> EnumDeserializer<'de, E> {
838 EnumDeserializer {
839 is_human_readable,
840 variant,
841 value,
842 err: PhantomData,
843 }
844 }
845}
846
847impl<'de, E> EnumAccess<'de> for EnumDeserializer<'de, E>
848where
849 E: DeError,
850{
851 type Error = E;
852 type Variant = VariantDeserializer<'de, Self::Error>;
853
854 fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), E>
855 where
856 V: DeserializeSeed<'de>,
857 {
858 let visitor = VariantDeserializer {
859 is_human_readable: self.is_human_readable,
860 value: self.value,
861 err: PhantomData,
862 };
863 seed.deserialize(ContentDeserializer::new(
864 self.variant,
865 self.is_human_readable,
866 ))
867 .map(|v| (v, visitor))
868 }
869}
870
871pub struct VariantDeserializer<'de, E>
872where
873 E: DeError,
874{
875 is_human_readable: bool,
876 value: Option<Content<'de>>,
877 err: PhantomData<E>,
878}
879
880impl<'de, E> VariantAccess<'de> for VariantDeserializer<'de, E>
881where
882 E: DeError,
883{
884 type Error = E;
885
886 fn unit_variant(self) -> Result<(), E> {
887 match self.value {
888 Some(value) => {
889 Deserialize::deserialize(ContentDeserializer::new(value, self.is_human_readable))
890 }
891 None => Ok(()),
892 }
893 }
894
895 fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, E>
896 where
897 T: DeserializeSeed<'de>,
898 {
899 match self.value {
900 Some(value) => {
901 seed.deserialize(ContentDeserializer::new(value, self.is_human_readable))
902 }
903 None => Err(DeError::invalid_type(
904 Unexpected::UnitVariant,
905 &"newtype variant",
906 )),
907 }
908 }
909
910 fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
911 where
912 V: Visitor<'de>,
913 {
914 match self.value {
915 Some(Content::Seq(v)) => Deserializer::deserialize_any(
916 SeqDeserializer::new(v, self.is_human_readable),
917 visitor,
918 ),
919 Some(other) => {
920 let mut buf = [0; 58];
921 Err(DeError::invalid_type(
922 other.unexpected(&mut buf),
923 &"tuple variant",
924 ))
925 }
926 None => Err(DeError::invalid_type(
927 Unexpected::UnitVariant,
928 &"tuple variant",
929 )),
930 }
931 }
932
933 fn struct_variant<V>(
934 self,
935 _fields: &'static [&'static str],
936 visitor: V,
937 ) -> Result<V::Value, Self::Error>
938 where
939 V: Visitor<'de>,
940 {
941 match self.value {
942 Some(Content::Map(v)) => Deserializer::deserialize_any(
943 MapDeserializer::new(v, self.is_human_readable),
944 visitor,
945 ),
946 Some(Content::Seq(v)) => Deserializer::deserialize_any(
947 SeqDeserializer::new(v, self.is_human_readable),
948 visitor,
949 ),
950 Some(other) => {
951 let mut buf = [0; 58];
952 Err(DeError::invalid_type(
953 other.unexpected(&mut buf),
954 &"struct variant",
955 ))
956 }
957 None => Err(DeError::invalid_type(
958 Unexpected::UnitVariant,
959 &"struct variant",
960 )),
961 }
962 }
963}
964
965struct SeqDeserializer<'de, E>
966where
967 E: DeError,
968{
969 is_human_readable: bool,
970 iter: <Vec<Content<'de>> as IntoIterator>::IntoIter,
971 err: PhantomData<E>,
972}
973
974impl<'de, E> SeqDeserializer<'de, E>
975where
976 E: DeError,
977{
978 fn new(vec: Vec<Content<'de>>, is_human_readable: bool) -> Self {
979 SeqDeserializer {
980 is_human_readable,
981 iter: vec.into_iter(),
982 err: PhantomData,
983 }
984 }
985}
986
987impl<'de, E> Deserializer<'de> for SeqDeserializer<'de, E>
988where
989 E: DeError,
990{
991 type Error = E;
992
993 #[inline]
994 fn is_human_readable(&self) -> bool {
995 self.is_human_readable
996 }
997
998 #[inline]
999 fn deserialize_any<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
1000 where
1001 V: Visitor<'de>,
1002 {
1003 let len = self.iter.len();
1004 if len == 0 {
1005 visitor.visit_unit()
1006 } else {
1007 let ret = visitor.visit_seq(&mut self)?;
1008 let remaining = self.iter.len();
1009 if remaining == 0 {
1010 Ok(ret)
1011 } else {
1012 Err(DeError::invalid_length(len, &"fewer elements in array"))
1013 }
1014 }
1015 }
1016
1017 forward_to_deserialize_any! {
1018 bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
1019 bytes byte_buf option unit unit_struct newtype_struct seq tuple
1020 tuple_struct map struct enum identifier ignored_any
1021 }
1022}
1023
1024impl<'de, E> SeqAccess<'de> for SeqDeserializer<'de, E>
1025where
1026 E: DeError,
1027{
1028 type Error = E;
1029
1030 fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
1031 where
1032 T: DeserializeSeed<'de>,
1033 {
1034 match self.iter.next() {
1035 Some(value) => seed
1036 .deserialize(ContentDeserializer::new(value, self.is_human_readable))
1037 .map(Some),
1038 None => Ok(None),
1039 }
1040 }
1041
1042 fn size_hint(&self) -> Option<usize> {
1043 size_hint_from_bounds(&self.iter)
1044 }
1045}
1046
1047struct MapDeserializer<'de, E>
1048where
1049 E: DeError,
1050{
1051 is_human_readable: bool,
1052 iter: <Vec<(Content<'de>, Content<'de>)> as IntoIterator>::IntoIter,
1053 value: Option<Content<'de>>,
1054 err: PhantomData<E>,
1055}
1056
1057impl<'de, E> MapDeserializer<'de, E>
1058where
1059 E: DeError,
1060{
1061 fn new(map: Vec<(Content<'de>, Content<'de>)>, is_human_readable: bool) -> Self {
1062 MapDeserializer {
1063 is_human_readable,
1064 iter: map.into_iter(),
1065 value: None,
1066 err: PhantomData,
1067 }
1068 }
1069}
1070
1071impl<'de, E> MapAccess<'de> for MapDeserializer<'de, E>
1072where
1073 E: DeError,
1074{
1075 type Error = E;
1076
1077 fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
1078 where
1079 T: DeserializeSeed<'de>,
1080 {
1081 match self.iter.next() {
1082 Some((key, value)) => {
1083 self.value = Some(value);
1084 seed.deserialize(ContentDeserializer::new(key, self.is_human_readable))
1085 .map(Some)
1086 }
1087 None => Ok(None),
1088 }
1089 }
1090
1091 fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error>
1092 where
1093 T: DeserializeSeed<'de>,
1094 {
1095 match self.value.take() {
1096 Some(value) => {
1097 seed.deserialize(ContentDeserializer::new(value, self.is_human_readable))
1098 }
1099 None => Err(DeError::custom("value is missing")),
1100 }
1101 }
1102
1103 fn size_hint(&self) -> Option<usize> {
1104 size_hint_from_bounds(&self.iter)
1105 }
1106}
1107
1108impl<'de, E> Deserializer<'de> for MapDeserializer<'de, E>
1109where
1110 E: DeError,
1111{
1112 type Error = E;
1113
1114 #[inline]
1115 fn is_human_readable(&self) -> bool {
1116 self.is_human_readable
1117 }
1118
1119 #[inline]
1120 fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1121 where
1122 V: Visitor<'de>,
1123 {
1124 visitor.visit_map(self)
1125 }
1126
1127 forward_to_deserialize_any! {
1128 bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
1129 bytes byte_buf option unit unit_struct newtype_struct seq tuple
1130 tuple_struct map struct enum identifier ignored_any
1131 }
1132}
1133
1134pub struct ContentRefDeserializer<'a, 'de, E> {
1136 is_human_readable: bool,
1137 content: &'a Content<'de>,
1138 err: PhantomData<E>,
1139}
1140
1141impl<'de, E> ContentRefDeserializer<'_, 'de, E>
1142where
1143 E: DeError,
1144{
1145 #[cold]
1146 fn invalid_type(self, exp: &dyn Expected) -> E {
1147 let mut buf = [0; 58];
1148 DeError::invalid_type(self.content.unexpected(&mut buf), exp)
1149 }
1150
1151 fn deserialize_integer<V>(self, visitor: V) -> Result<V::Value, E>
1152 where
1153 V: Visitor<'de>,
1154 {
1155 match *self.content {
1156 Content::U8(v) => visitor.visit_u8(v),
1157 Content::U16(v) => visitor.visit_u16(v),
1158 Content::U32(v) => visitor.visit_u32(v),
1159 Content::U64(v) => visitor.visit_u64(v),
1160 Content::U128(v) => visitor.visit_u128(v),
1161 Content::I8(v) => visitor.visit_i8(v),
1162 Content::I16(v) => visitor.visit_i16(v),
1163 Content::I32(v) => visitor.visit_i32(v),
1164 Content::I64(v) => visitor.visit_i64(v),
1165 Content::I128(v) => visitor.visit_i128(v),
1166 _ => Err(self.invalid_type(&visitor)),
1167 }
1168 }
1169
1170 fn deserialize_float<V>(self, visitor: V) -> Result<V::Value, E>
1171 where
1172 V: Visitor<'de>,
1173 {
1174 match *self.content {
1175 Content::F32(v) => visitor.visit_f32(v),
1176 Content::F64(v) => visitor.visit_f64(v),
1177 Content::U8(v) => visitor.visit_u8(v),
1178 Content::U16(v) => visitor.visit_u16(v),
1179 Content::U32(v) => visitor.visit_u32(v),
1180 Content::U64(v) => visitor.visit_u64(v),
1181 Content::U128(v) => visitor.visit_u128(v),
1182 Content::I8(v) => visitor.visit_i8(v),
1183 Content::I16(v) => visitor.visit_i16(v),
1184 Content::I32(v) => visitor.visit_i32(v),
1185 Content::I64(v) => visitor.visit_i64(v),
1186 Content::I128(v) => visitor.visit_i128(v),
1187 _ => Err(self.invalid_type(&visitor)),
1188 }
1189 }
1190}
1191
1192fn visit_content_seq_ref<'a, 'de, V, E>(
1193 content: &'a [Content<'de>],
1194 visitor: V,
1195 is_human_readable: bool,
1196) -> Result<V::Value, E>
1197where
1198 V: Visitor<'de>,
1199 E: DeError,
1200{
1201 let seq = content
1202 .iter()
1203 .map(|x| ContentRefDeserializer::new(x, is_human_readable));
1204 let mut seq_visitor = serde::de::value::SeqDeserializer::new(seq);
1205 let value = visitor.visit_seq(&mut seq_visitor)?;
1206 seq_visitor.end()?;
1207 Ok(value)
1208}
1209
1210fn visit_content_map_ref<'a, 'de, V, E>(
1211 content: &'a [(Content<'de>, Content<'de>)],
1212 visitor: V,
1213 is_human_readable: bool,
1214) -> Result<V::Value, E>
1215where
1216 V: Visitor<'de>,
1217 E: DeError,
1218{
1219 let map = content.iter().map(|(k, v)| {
1220 (
1221 ContentRefDeserializer::new(k, is_human_readable),
1222 ContentRefDeserializer::new(v, is_human_readable),
1223 )
1224 });
1225 let mut map_visitor = serde::de::value::MapDeserializer::new(map);
1226 let value = visitor.visit_map(&mut map_visitor)?;
1227 map_visitor.end()?;
1228 Ok(value)
1229}
1230
1231impl<'de, E> Deserializer<'de> for ContentRefDeserializer<'_, 'de, E>
1234where
1235 E: DeError,
1236{
1237 type Error = E;
1238
1239 #[inline]
1240 fn is_human_readable(&self) -> bool {
1241 self.is_human_readable
1242 }
1243
1244 fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, E>
1245 where
1246 V: Visitor<'de>,
1247 {
1248 match *self.content {
1249 Content::Bool(v) => visitor.visit_bool(v),
1250 Content::U8(v) => visitor.visit_u8(v),
1251 Content::U16(v) => visitor.visit_u16(v),
1252 Content::U32(v) => visitor.visit_u32(v),
1253 Content::U64(v) => visitor.visit_u64(v),
1254 Content::U128(v) => visitor.visit_u128(v),
1255 Content::I8(v) => visitor.visit_i8(v),
1256 Content::I16(v) => visitor.visit_i16(v),
1257 Content::I32(v) => visitor.visit_i32(v),
1258 Content::I64(v) => visitor.visit_i64(v),
1259 Content::I128(v) => visitor.visit_i128(v),
1260 Content::F32(v) => visitor.visit_f32(v),
1261 Content::F64(v) => visitor.visit_f64(v),
1262 Content::Char(v) => visitor.visit_char(v),
1263 Content::String(ref v) => visitor.visit_str(v),
1264 Content::Str(v) => visitor.visit_borrowed_str(v),
1265 Content::ByteBuf(ref v) => visitor.visit_bytes(v),
1266 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
1267 Content::Unit => visitor.visit_unit(),
1268 Content::None => visitor.visit_none(),
1269 Content::Some(ref v) => {
1270 visitor.visit_some(ContentRefDeserializer::new(v, self.is_human_readable))
1271 }
1272 Content::Newtype(ref v) => {
1273 visitor.visit_newtype_struct(ContentRefDeserializer::new(v, self.is_human_readable))
1274 }
1275 Content::Seq(ref v) => visit_content_seq_ref(v, visitor, self.is_human_readable),
1276 Content::Map(ref v) => visit_content_map_ref(v, visitor, self.is_human_readable),
1277 }
1278 }
1279
1280 fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1281 where
1282 V: Visitor<'de>,
1283 {
1284 match *self.content {
1285 Content::Bool(v) => visitor.visit_bool(v),
1286 _ => Err(self.invalid_type(&visitor)),
1287 }
1288 }
1289
1290 fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1291 where
1292 V: Visitor<'de>,
1293 {
1294 self.deserialize_integer(visitor)
1295 }
1296
1297 fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1298 where
1299 V: Visitor<'de>,
1300 {
1301 self.deserialize_integer(visitor)
1302 }
1303
1304 fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1305 where
1306 V: Visitor<'de>,
1307 {
1308 self.deserialize_integer(visitor)
1309 }
1310
1311 fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1312 where
1313 V: Visitor<'de>,
1314 {
1315 self.deserialize_integer(visitor)
1316 }
1317
1318 fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1319 where
1320 V: Visitor<'de>,
1321 {
1322 self.deserialize_integer(visitor)
1323 }
1324
1325 fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1326 where
1327 V: Visitor<'de>,
1328 {
1329 self.deserialize_integer(visitor)
1330 }
1331
1332 fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1333 where
1334 V: Visitor<'de>,
1335 {
1336 self.deserialize_integer(visitor)
1337 }
1338
1339 fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1340 where
1341 V: Visitor<'de>,
1342 {
1343 self.deserialize_integer(visitor)
1344 }
1345
1346 fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1347 where
1348 V: Visitor<'de>,
1349 {
1350 self.deserialize_integer(visitor)
1351 }
1352
1353 fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1354 where
1355 V: Visitor<'de>,
1356 {
1357 self.deserialize_integer(visitor)
1358 }
1359
1360 fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1361 where
1362 V: Visitor<'de>,
1363 {
1364 self.deserialize_float(visitor)
1365 }
1366
1367 fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1368 where
1369 V: Visitor<'de>,
1370 {
1371 self.deserialize_float(visitor)
1372 }
1373
1374 fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1375 where
1376 V: Visitor<'de>,
1377 {
1378 match *self.content {
1379 Content::Char(v) => visitor.visit_char(v),
1380 Content::String(ref v) => visitor.visit_str(v),
1381 Content::Str(v) => visitor.visit_borrowed_str(v),
1382 _ => Err(self.invalid_type(&visitor)),
1383 }
1384 }
1385
1386 fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1387 where
1388 V: Visitor<'de>,
1389 {
1390 match *self.content {
1391 Content::String(ref v) => visitor.visit_str(v),
1392 Content::Str(v) => visitor.visit_borrowed_str(v),
1393 Content::ByteBuf(ref v) => visitor.visit_bytes(v),
1394 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
1395 _ => Err(self.invalid_type(&visitor)),
1396 }
1397 }
1398
1399 fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1400 where
1401 V: Visitor<'de>,
1402 {
1403 self.deserialize_str(visitor)
1404 }
1405
1406 fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1407 where
1408 V: Visitor<'de>,
1409 {
1410 match *self.content {
1411 Content::String(ref v) => visitor.visit_str(v),
1412 Content::Str(v) => visitor.visit_borrowed_str(v),
1413 Content::ByteBuf(ref v) => visitor.visit_bytes(v),
1414 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
1415 Content::Seq(ref v) => visit_content_seq_ref(v, visitor, self.is_human_readable),
1416 _ => Err(self.invalid_type(&visitor)),
1417 }
1418 }
1419
1420 fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1421 where
1422 V: Visitor<'de>,
1423 {
1424 self.deserialize_bytes(visitor)
1425 }
1426
1427 fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, E>
1428 where
1429 V: Visitor<'de>,
1430 {
1431 match *self.content {
1432 Content::None => visitor.visit_none(),
1433 Content::Some(ref v) => {
1434 visitor.visit_some(ContentRefDeserializer::new(v, self.is_human_readable))
1435 }
1436 Content::Unit => visitor.visit_unit(),
1437 _ => visitor.visit_some(self),
1438 }
1439 }
1440
1441 fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1442 where
1443 V: Visitor<'de>,
1444 {
1445 match *self.content {
1446 Content::Unit => visitor.visit_unit(),
1447 _ => Err(self.invalid_type(&visitor)),
1448 }
1449 }
1450
1451 fn deserialize_unit_struct<V>(
1452 self,
1453 _name: &'static str,
1454 visitor: V,
1455 ) -> Result<V::Value, Self::Error>
1456 where
1457 V: Visitor<'de>,
1458 {
1459 self.deserialize_unit(visitor)
1460 }
1461
1462 fn deserialize_newtype_struct<V>(self, _name: &str, visitor: V) -> Result<V::Value, E>
1463 where
1464 V: Visitor<'de>,
1465 {
1466 match *self.content {
1467 Content::Newtype(ref v) => {
1468 visitor.visit_newtype_struct(ContentRefDeserializer::new(v, self.is_human_readable))
1469 }
1470 _ => visitor.visit_newtype_struct(self),
1471 }
1472 }
1473
1474 fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1475 where
1476 V: Visitor<'de>,
1477 {
1478 match *self.content {
1479 Content::Seq(ref v) => visit_content_seq_ref(v, visitor, self.is_human_readable),
1480 _ => Err(self.invalid_type(&visitor)),
1481 }
1482 }
1483
1484 fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
1485 where
1486 V: Visitor<'de>,
1487 {
1488 self.deserialize_seq(visitor)
1489 }
1490
1491 fn deserialize_tuple_struct<V>(
1492 self,
1493 _name: &'static str,
1494 _len: usize,
1495 visitor: V,
1496 ) -> Result<V::Value, Self::Error>
1497 where
1498 V: Visitor<'de>,
1499 {
1500 self.deserialize_seq(visitor)
1501 }
1502
1503 fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1504 where
1505 V: Visitor<'de>,
1506 {
1507 match *self.content {
1508 Content::Map(ref v) => visit_content_map_ref(v, visitor, self.is_human_readable),
1509 _ => Err(self.invalid_type(&visitor)),
1510 }
1511 }
1512
1513 fn deserialize_struct<V>(
1514 self,
1515 _name: &'static str,
1516 _fields: &'static [&'static str],
1517 visitor: V,
1518 ) -> Result<V::Value, Self::Error>
1519 where
1520 V: Visitor<'de>,
1521 {
1522 match *self.content {
1523 Content::Seq(ref v) => visit_content_seq_ref(v, visitor, self.is_human_readable),
1524 Content::Map(ref v) => visit_content_map_ref(v, visitor, self.is_human_readable),
1525 _ => Err(self.invalid_type(&visitor)),
1526 }
1527 }
1528
1529 fn deserialize_enum<V>(
1530 self,
1531 _name: &str,
1532 _variants: &'static [&'static str],
1533 visitor: V,
1534 ) -> Result<V::Value, Self::Error>
1535 where
1536 V: Visitor<'de>,
1537 {
1538 let (variant, value) = match *self.content {
1539 Content::Map(ref value) => {
1540 let mut iter = value.iter();
1541 let (variant, value) = match iter.next() {
1542 Some(v) => v,
1543 None => {
1544 return Err(DeError::invalid_value(
1545 Unexpected::Map,
1546 &"map with a single key",
1547 ));
1548 }
1549 };
1550 if iter.next().is_some() {
1552 return Err(DeError::invalid_value(
1553 Unexpected::Map,
1554 &"map with a single key",
1555 ));
1556 }
1557 (variant, Some(value))
1558 }
1559 ref s @ Content::String(_) | ref s @ Content::Str(_) => (s, None),
1560 ref other => {
1561 let mut buf = [0; 58];
1562 return Err(DeError::invalid_type(
1563 other.unexpected(&mut buf),
1564 &"string or map",
1565 ));
1566 }
1567 };
1568
1569 visitor.visit_enum(EnumRefDeserializer {
1570 is_human_readable: self.is_human_readable,
1571 variant,
1572 value,
1573 err: PhantomData,
1574 })
1575 }
1576
1577 fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1578 where
1579 V: Visitor<'de>,
1580 {
1581 match *self.content {
1582 Content::String(ref v) => visitor.visit_str(v),
1583 Content::Str(v) => visitor.visit_borrowed_str(v),
1584 Content::ByteBuf(ref v) => visitor.visit_bytes(v),
1585 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
1586 Content::U8(v) => visitor.visit_u8(v),
1587 Content::U64(v) => visitor.visit_u64(v),
1588 _ => Err(self.invalid_type(&visitor)),
1589 }
1590 }
1591
1592 fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1593 where
1594 V: Visitor<'de>,
1595 {
1596 visitor.visit_unit()
1597 }
1598}
1599
1600impl<'a, 'de, E> ContentRefDeserializer<'a, 'de, E> {
1601 pub(crate) fn new(content: &'a Content<'de>, is_human_readable: bool) -> Self {
1603 ContentRefDeserializer {
1604 is_human_readable,
1605 content,
1606 err: PhantomData,
1607 }
1608 }
1609}
1610
1611struct EnumRefDeserializer<'a, 'de, E>
1612where
1613 E: DeError,
1614{
1615 is_human_readable: bool,
1616 variant: &'a Content<'de>,
1617 value: Option<&'a Content<'de>>,
1618 err: PhantomData<E>,
1619}
1620
1621impl<'de, 'a, E> EnumAccess<'de> for EnumRefDeserializer<'a, 'de, E>
1622where
1623 E: DeError,
1624{
1625 type Error = E;
1626 type Variant = VariantRefDeserializer<'a, 'de, Self::Error>;
1627
1628 fn variant_seed<V>(self, seed: V) -> Result<(V::Value, Self::Variant), Self::Error>
1629 where
1630 V: DeserializeSeed<'de>,
1631 {
1632 let visitor = VariantRefDeserializer {
1633 is_human_readable: self.is_human_readable,
1634 value: self.value,
1635 err: PhantomData,
1636 };
1637 seed.deserialize(ContentRefDeserializer::new(
1638 self.variant,
1639 self.is_human_readable,
1640 ))
1641 .map(|v| (v, visitor))
1642 }
1643}
1644
1645struct VariantRefDeserializer<'a, 'de, E>
1646where
1647 E: DeError,
1648{
1649 is_human_readable: bool,
1650 value: Option<&'a Content<'de>>,
1651 err: PhantomData<E>,
1652}
1653
1654impl<'de, E> VariantAccess<'de> for VariantRefDeserializer<'_, 'de, E>
1655where
1656 E: DeError,
1657{
1658 type Error = E;
1659
1660 fn unit_variant(self) -> Result<(), E> {
1661 match self.value {
1662 Some(value) => {
1663 Deserialize::deserialize(ContentRefDeserializer::new(value, self.is_human_readable))
1664 }
1665 None => Ok(()),
1666 }
1667 }
1668
1669 fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value, E>
1670 where
1671 T: DeserializeSeed<'de>,
1672 {
1673 match self.value {
1674 Some(value) => {
1675 seed.deserialize(ContentRefDeserializer::new(value, self.is_human_readable))
1676 }
1677 None => Err(DeError::invalid_type(
1678 Unexpected::UnitVariant,
1679 &"newtype variant",
1680 )),
1681 }
1682 }
1683
1684 fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
1685 where
1686 V: Visitor<'de>,
1687 {
1688 match self.value {
1689 Some(Content::Seq(v)) => Deserializer::deserialize_any(
1690 SeqRefDeserializer::new(v, self.is_human_readable),
1691 visitor,
1692 ),
1693 Some(other) => {
1694 let mut buf = [0; 58];
1695 Err(DeError::invalid_type(
1696 other.unexpected(&mut buf),
1697 &"tuple variant",
1698 ))
1699 }
1700 None => Err(DeError::invalid_type(
1701 Unexpected::UnitVariant,
1702 &"tuple variant",
1703 )),
1704 }
1705 }
1706
1707 fn struct_variant<V>(
1708 self,
1709 _fields: &'static [&'static str],
1710 visitor: V,
1711 ) -> Result<V::Value, Self::Error>
1712 where
1713 V: Visitor<'de>,
1714 {
1715 match self.value {
1716 Some(Content::Map(v)) => Deserializer::deserialize_any(
1717 MapRefDeserializer::new(v, self.is_human_readable),
1718 visitor,
1719 ),
1720 Some(Content::Seq(v)) => Deserializer::deserialize_any(
1721 SeqRefDeserializer::new(v, self.is_human_readable),
1722 visitor,
1723 ),
1724 Some(other) => {
1725 let mut buf = [0; 58];
1726 Err(DeError::invalid_type(
1727 other.unexpected(&mut buf),
1728 &"struct variant",
1729 ))
1730 }
1731 None => Err(DeError::invalid_type(
1732 Unexpected::UnitVariant,
1733 &"struct variant",
1734 )),
1735 }
1736 }
1737}
1738
1739struct SeqRefDeserializer<'a, 'de, E>
1740where
1741 E: DeError,
1742{
1743 is_human_readable: bool,
1744 iter: <&'a [Content<'de>] as IntoIterator>::IntoIter,
1745 err: PhantomData<E>,
1746}
1747
1748impl<'a, 'de, E> SeqRefDeserializer<'a, 'de, E>
1749where
1750 E: DeError,
1751{
1752 fn new(slice: &'a [Content<'de>], is_human_readable: bool) -> Self {
1753 SeqRefDeserializer {
1754 is_human_readable,
1755 iter: slice.iter(),
1756 err: PhantomData,
1757 }
1758 }
1759}
1760
1761impl<'de, E> Deserializer<'de> for SeqRefDeserializer<'_, 'de, E>
1762where
1763 E: DeError,
1764{
1765 type Error = E;
1766
1767 #[inline]
1768 fn is_human_readable(&self) -> bool {
1769 self.is_human_readable
1770 }
1771
1772 #[inline]
1773 fn deserialize_any<V>(mut self, visitor: V) -> Result<V::Value, Self::Error>
1774 where
1775 V: Visitor<'de>,
1776 {
1777 let len = self.iter.len();
1778 if len == 0 {
1779 visitor.visit_unit()
1780 } else {
1781 let ret = visitor.visit_seq(&mut self)?;
1782 let remaining = self.iter.len();
1783 if remaining == 0 {
1784 Ok(ret)
1785 } else {
1786 Err(DeError::invalid_length(len, &"fewer elements in array"))
1787 }
1788 }
1789 }
1790
1791 forward_to_deserialize_any! {
1792 bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
1793 bytes byte_buf option unit unit_struct newtype_struct seq tuple
1794 tuple_struct map struct enum identifier ignored_any
1795 }
1796}
1797
1798impl<'de, E> SeqAccess<'de> for SeqRefDeserializer<'_, 'de, E>
1799where
1800 E: DeError,
1801{
1802 type Error = E;
1803
1804 fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
1805 where
1806 T: DeserializeSeed<'de>,
1807 {
1808 match self.iter.next() {
1809 Some(value) => seed
1810 .deserialize(ContentRefDeserializer::new(value, self.is_human_readable))
1811 .map(Some),
1812 None => Ok(None),
1813 }
1814 }
1815
1816 fn size_hint(&self) -> Option<usize> {
1817 size_hint_from_bounds(&self.iter)
1818 }
1819}
1820
1821struct MapRefDeserializer<'a, 'de, E>
1822where
1823 E: DeError,
1824{
1825 is_human_readable: bool,
1826 iter: <&'a [(Content<'de>, Content<'de>)] as IntoIterator>::IntoIter,
1827 value: Option<&'a Content<'de>>,
1828 err: PhantomData<E>,
1829}
1830
1831impl<'a, 'de, E> MapRefDeserializer<'a, 'de, E>
1832where
1833 E: DeError,
1834{
1835 fn new(map: &'a [(Content<'de>, Content<'de>)], is_human_readable: bool) -> Self {
1836 MapRefDeserializer {
1837 is_human_readable,
1838 iter: map.iter(),
1839 value: None,
1840 err: PhantomData,
1841 }
1842 }
1843}
1844
1845impl<'de, E> MapAccess<'de> for MapRefDeserializer<'_, 'de, E>
1846where
1847 E: DeError,
1848{
1849 type Error = E;
1850
1851 fn next_key_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>, Self::Error>
1852 where
1853 T: DeserializeSeed<'de>,
1854 {
1855 match self.iter.next() {
1856 Some((key, value)) => {
1857 self.value = Some(value);
1858 seed.deserialize(ContentRefDeserializer::new(key, self.is_human_readable))
1859 .map(Some)
1860 }
1861 None => Ok(None),
1862 }
1863 }
1864
1865 fn next_value_seed<T>(&mut self, seed: T) -> Result<T::Value, Self::Error>
1866 where
1867 T: DeserializeSeed<'de>,
1868 {
1869 match self.value.take() {
1870 Some(value) => {
1871 seed.deserialize(ContentRefDeserializer::new(value, self.is_human_readable))
1872 }
1873 None => Err(DeError::custom("value is missing")),
1874 }
1875 }
1876
1877 fn size_hint(&self) -> Option<usize> {
1878 size_hint_from_bounds(&self.iter)
1879 }
1880}
1881
1882impl<'de, E> Deserializer<'de> for MapRefDeserializer<'_, 'de, E>
1883where
1884 E: DeError,
1885{
1886 type Error = E;
1887
1888 #[inline]
1889 fn is_human_readable(&self) -> bool {
1890 self.is_human_readable
1891 }
1892
1893 #[inline]
1894 fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1895 where
1896 V: Visitor<'de>,
1897 {
1898 visitor.visit_map(self)
1899 }
1900
1901 forward_to_deserialize_any! {
1902 bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
1903 bytes byte_buf option unit unit_struct newtype_struct seq tuple
1904 tuple_struct map struct enum identifier ignored_any
1905 }
1906}
1907
1908impl<'de, E> IntoDeserializer<'de, E> for ContentDeserializer<'de, E>
1909where
1910 E: DeError,
1911{
1912 type Deserializer = Self;
1913
1914 fn into_deserializer(self) -> Self {
1915 self
1916 }
1917}
1918
1919impl<'de, E> IntoDeserializer<'de, E> for ContentRefDeserializer<'_, 'de, E>
1920where
1921 E: DeError,
1922{
1923 type Deserializer = Self;
1924
1925 fn into_deserializer(self) -> Self {
1926 self
1927 }
1928}