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