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