1#![doc(hidden)]
2use std::{borrow::Cow, fmt, marker::PhantomData};
6
7use serde::{
8 de::{self, EnumAccess, Expected, MapAccess, SeqAccess, Unexpected, Visitor},
9 Deserialize, Deserializer,
10};
11
12use super::key_de::KeyDeserializer;
13
14#[derive(Debug, Clone)]
19pub enum Content<'de> {
20 Bool(bool),
21
22 U8(u8),
23 U16(u16),
24 U32(u32),
25 U64(u64),
26
27 I8(i8),
28 I16(i16),
29 I32(i32),
30 I64(i64),
31
32 F32(f32),
33 F64(f64),
34
35 Char(char),
36 String(String),
37 Str(&'de str),
38 ByteBuf(Vec<u8>),
39 Bytes(&'de [u8]),
40
41 None,
42 Some(Box<Content<'de>>),
43
44 Unit,
45 Newtype(Box<Content<'de>>),
46 Seq(Vec<Content<'de>>),
47 Map(Vec<(Cow<'de, str>, Content<'de>)>),
48}
49
50impl Content<'_> {
51 #[cold]
52 fn unexpected(&self) -> Unexpected<'_> {
53 match *self {
54 Content::Bool(b) => Unexpected::Bool(b),
55 Content::U8(n) => Unexpected::Unsigned(n as u64),
56 Content::U16(n) => Unexpected::Unsigned(n as u64),
57 Content::U32(n) => Unexpected::Unsigned(n as u64),
58 Content::U64(n) => Unexpected::Unsigned(n),
59 Content::I8(n) => Unexpected::Signed(n as i64),
60 Content::I16(n) => Unexpected::Signed(n as i64),
61 Content::I32(n) => Unexpected::Signed(n as i64),
62 Content::I64(n) => Unexpected::Signed(n),
63 Content::F32(f) => Unexpected::Float(f as f64),
64 Content::F64(f) => Unexpected::Float(f),
65 Content::Char(c) => Unexpected::Char(c),
66 Content::String(ref s) => Unexpected::Str(s),
67 Content::Str(s) => Unexpected::Str(s),
68 Content::ByteBuf(ref b) => Unexpected::Bytes(b),
69 Content::Bytes(b) => Unexpected::Bytes(b),
70 Content::None | Content::Some(_) => Unexpected::Option,
71 Content::Unit => Unexpected::Unit,
72 Content::Newtype(_) => Unexpected::NewtypeStruct,
73 Content::Seq(_) => Unexpected::Seq,
74 Content::Map(_) => Unexpected::Map,
75 }
76 }
77}
78
79impl<'de> Deserialize<'de> for Content<'de> {
80 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
81 where
82 D: Deserializer<'de>,
83 {
84 deserializer.deserialize_any(ContentVisitor { value: PhantomData })
85 }
86}
87
88struct ContentVisitor<'de> {
89 value: PhantomData<Content<'de>>,
90}
91
92impl<'de> Visitor<'de> for ContentVisitor<'de> {
93 type Value = Content<'de>;
94
95 fn expecting(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
96 fmt.write_str("any value")
97 }
98
99 fn visit_bool<F>(self, value: bool) -> Result<Self::Value, F>
100 where
101 F: de::Error,
102 {
103 Ok(Content::Bool(value))
104 }
105
106 fn visit_i8<F>(self, value: i8) -> Result<Self::Value, F>
107 where
108 F: de::Error,
109 {
110 Ok(Content::I8(value))
111 }
112
113 fn visit_i16<F>(self, value: i16) -> Result<Self::Value, F>
114 where
115 F: de::Error,
116 {
117 Ok(Content::I16(value))
118 }
119
120 fn visit_i32<F>(self, value: i32) -> Result<Self::Value, F>
121 where
122 F: de::Error,
123 {
124 Ok(Content::I32(value))
125 }
126
127 fn visit_i64<F>(self, value: i64) -> Result<Self::Value, F>
128 where
129 F: de::Error,
130 {
131 Ok(Content::I64(value))
132 }
133
134 fn visit_u8<F>(self, value: u8) -> Result<Self::Value, F>
135 where
136 F: de::Error,
137 {
138 Ok(Content::U8(value))
139 }
140
141 fn visit_u16<F>(self, value: u16) -> Result<Self::Value, F>
142 where
143 F: de::Error,
144 {
145 Ok(Content::U16(value))
146 }
147
148 fn visit_u32<F>(self, value: u32) -> Result<Self::Value, F>
149 where
150 F: de::Error,
151 {
152 Ok(Content::U32(value))
153 }
154
155 fn visit_u64<F>(self, value: u64) -> Result<Self::Value, F>
156 where
157 F: de::Error,
158 {
159 Ok(Content::U64(value))
160 }
161
162 fn visit_f32<F>(self, value: f32) -> Result<Self::Value, F>
163 where
164 F: de::Error,
165 {
166 Ok(Content::F32(value))
167 }
168
169 fn visit_f64<F>(self, value: f64) -> Result<Self::Value, F>
170 where
171 F: de::Error,
172 {
173 Ok(Content::F64(value))
174 }
175
176 fn visit_char<F>(self, value: char) -> Result<Self::Value, F>
177 where
178 F: de::Error,
179 {
180 Ok(Content::Char(value))
181 }
182
183 fn visit_str<F>(self, value: &str) -> Result<Self::Value, F>
184 where
185 F: de::Error,
186 {
187 Ok(Content::String(value.into()))
188 }
189
190 fn visit_borrowed_str<F>(self, value: &'de str) -> Result<Self::Value, F>
191 where
192 F: de::Error,
193 {
194 Ok(Content::Str(value))
195 }
196
197 fn visit_string<F>(self, value: String) -> Result<Self::Value, F>
198 where
199 F: de::Error,
200 {
201 Ok(Content::String(value))
202 }
203
204 fn visit_bytes<F>(self, value: &[u8]) -> Result<Self::Value, F>
205 where
206 F: de::Error,
207 {
208 Ok(Content::ByteBuf(value.into()))
209 }
210
211 fn visit_borrowed_bytes<F>(self, value: &'de [u8]) -> Result<Self::Value, F>
212 where
213 F: de::Error,
214 {
215 Ok(Content::Bytes(value))
216 }
217
218 fn visit_byte_buf<F>(self, value: Vec<u8>) -> Result<Self::Value, F>
219 where
220 F: de::Error,
221 {
222 Ok(Content::ByteBuf(value))
223 }
224
225 fn visit_unit<F>(self) -> Result<Self::Value, F>
226 where
227 F: de::Error,
228 {
229 Ok(Content::Unit)
230 }
231
232 fn visit_none<F>(self) -> Result<Self::Value, F>
233 where
234 F: de::Error,
235 {
236 Ok(Content::None)
237 }
238
239 fn visit_some<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
240 where
241 D: Deserializer<'de>,
242 {
243 Deserialize::deserialize(deserializer).map(|v| Content::Some(Box::new(v)))
244 }
245
246 fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
247 where
248 D: Deserializer<'de>,
249 {
250 Deserialize::deserialize(deserializer).map(|v| Content::Newtype(Box::new(v)))
251 }
252
253 fn visit_seq<V>(self, mut visitor: V) -> Result<Self::Value, V::Error>
254 where
255 V: SeqAccess<'de>,
256 {
257 let mut vec = Vec::with_capacity(size_hint::cautious(visitor.size_hint()));
258 while let Some(e) = visitor.next_element()? {
259 vec.push(e);
260 }
261 Ok(Content::Seq(vec))
262 }
263
264 fn visit_map<V>(self, mut visitor: V) -> Result<Self::Value, V::Error>
265 where
266 V: MapAccess<'de>,
267 {
268 let mut vec = Vec::with_capacity(size_hint::cautious(visitor.size_hint()));
269 while let Some(kv) = visitor.next_entry()? {
270 vec.push(kv);
271 }
272 Ok(Content::Map(vec))
273 }
274
275 fn visit_enum<V>(self, _visitor: V) -> Result<Self::Value, V::Error>
276 where
277 V: EnumAccess<'de>,
278 {
279 Err(de::Error::custom(
280 "fragment deserialization doesn't support enum input",
281 ))
282 }
283}
284
285pub struct ContentDeserializer<'de, E> {
287 content: Content<'de>,
288 err: PhantomData<E>,
289}
290
291impl<'de, E> ContentDeserializer<'de, E>
292where
293 E: de::Error,
294{
295 pub fn new(content: Content<'de>) -> Self {
296 ContentDeserializer {
297 content,
298 err: PhantomData,
299 }
300 }
301
302 #[cold]
303 fn invalid_type(self, exp: &dyn Expected) -> E {
304 de::Error::invalid_type(self.content.unexpected(), exp)
305 }
306
307 fn deserialize_integer<V>(self, visitor: V) -> Result<V::Value, E>
308 where
309 V: Visitor<'de>,
310 {
311 match self.content {
312 Content::U8(v) => visitor.visit_u8(v),
313 Content::U16(v) => visitor.visit_u16(v),
314 Content::U32(v) => visitor.visit_u32(v),
315 Content::U64(v) => visitor.visit_u64(v),
316 Content::I8(v) => visitor.visit_i8(v),
317 Content::I16(v) => visitor.visit_i16(v),
318 Content::I32(v) => visitor.visit_i32(v),
319 Content::I64(v) => visitor.visit_i64(v),
320 _ => Err(self.invalid_type(&visitor)),
321 }
322 }
323
324 fn deserialize_float<V>(self, visitor: V) -> Result<V::Value, E>
325 where
326 V: Visitor<'de>,
327 {
328 match self.content {
329 Content::F32(v) => visitor.visit_f32(v),
330 Content::F64(v) => visitor.visit_f64(v),
331 Content::U8(v) => visitor.visit_u8(v),
332 Content::U16(v) => visitor.visit_u16(v),
333 Content::U32(v) => visitor.visit_u32(v),
334 Content::U64(v) => visitor.visit_u64(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 _ => Err(self.invalid_type(&visitor)),
340 }
341 }
342}
343
344fn visit_content_seq<'de, V, E>(content: Vec<Content<'de>>, visitor: V) -> Result<V::Value, E>
345where
346 V: Visitor<'de>,
347 E: de::Error,
348{
349 let seq = content.into_iter().map(ContentDeserializer::new);
350 let mut seq_visitor = de::value::SeqDeserializer::new(seq);
351 let value = visitor.visit_seq(&mut seq_visitor)?;
352 seq_visitor.end()?;
353 Ok(value)
354}
355
356fn visit_content_map<'de, V, E>(
357 content: Vec<(Cow<'de, str>, Content<'de>)>,
358 visitor: V,
359) -> Result<V::Value, E>
360where
361 V: Visitor<'de>,
362 E: de::Error,
363{
364 let map = content
365 .into_iter()
366 .map(|(k, v)| (KeyDeserializer::new(k), ContentDeserializer::new(v)));
367 let mut map_visitor = de::value::MapDeserializer::new(map);
368 let value = visitor.visit_map(&mut map_visitor)?;
369 map_visitor.end()?;
370 Ok(value)
371}
372
373impl<'de, E> Deserializer<'de> for ContentDeserializer<'de, E>
374where
375 E: de::Error,
376{
377 type Error = E;
378
379 fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
380 where
381 V: Visitor<'de>,
382 {
383 match self.content {
384 Content::Bool(v) => visitor.visit_bool(v),
385 Content::U8(v) => visitor.visit_u8(v),
386 Content::U16(v) => visitor.visit_u16(v),
387 Content::U32(v) => visitor.visit_u32(v),
388 Content::U64(v) => visitor.visit_u64(v),
389 Content::I8(v) => visitor.visit_i8(v),
390 Content::I16(v) => visitor.visit_i16(v),
391 Content::I32(v) => visitor.visit_i32(v),
392 Content::I64(v) => visitor.visit_i64(v),
393 Content::F32(v) => visitor.visit_f32(v),
394 Content::F64(v) => visitor.visit_f64(v),
395 Content::Char(v) => visitor.visit_char(v),
396 Content::String(v) => visitor.visit_string(v),
397 Content::Str(v) => visitor.visit_borrowed_str(v),
398 Content::ByteBuf(v) => visitor.visit_byte_buf(v),
399 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
400 Content::Unit => visitor.visit_unit(),
401 Content::None => visitor.visit_none(),
402 Content::Some(v) => visitor.visit_some(ContentDeserializer::new(*v)),
403 Content::Newtype(v) => visitor.visit_newtype_struct(ContentDeserializer::new(*v)),
404 Content::Seq(v) => visit_content_seq(v, visitor),
405 Content::Map(v) => visit_content_map(v, visitor),
406 }
407 }
408
409 fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
410 where
411 V: Visitor<'de>,
412 {
413 match self.content {
414 Content::Bool(v) => visitor.visit_bool(v),
415 _ => Err(self.invalid_type(&visitor)),
416 }
417 }
418
419 fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
420 where
421 V: Visitor<'de>,
422 {
423 self.deserialize_integer(visitor)
424 }
425
426 fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
427 where
428 V: Visitor<'de>,
429 {
430 self.deserialize_integer(visitor)
431 }
432
433 fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
434 where
435 V: Visitor<'de>,
436 {
437 self.deserialize_integer(visitor)
438 }
439
440 fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
441 where
442 V: Visitor<'de>,
443 {
444 self.deserialize_integer(visitor)
445 }
446
447 fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
448 where
449 V: Visitor<'de>,
450 {
451 self.deserialize_integer(visitor)
452 }
453
454 fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
455 where
456 V: Visitor<'de>,
457 {
458 self.deserialize_integer(visitor)
459 }
460
461 fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
462 where
463 V: Visitor<'de>,
464 {
465 self.deserialize_integer(visitor)
466 }
467
468 fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
469 where
470 V: Visitor<'de>,
471 {
472 self.deserialize_integer(visitor)
473 }
474
475 fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
476 where
477 V: Visitor<'de>,
478 {
479 self.deserialize_float(visitor)
480 }
481
482 fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
483 where
484 V: Visitor<'de>,
485 {
486 self.deserialize_float(visitor)
487 }
488
489 fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
490 where
491 V: Visitor<'de>,
492 {
493 match self.content {
494 Content::Char(v) => visitor.visit_char(v),
495 Content::String(v) => visitor.visit_string(v),
496 Content::Str(v) => visitor.visit_borrowed_str(v),
497 _ => Err(self.invalid_type(&visitor)),
498 }
499 }
500
501 fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
502 where
503 V: Visitor<'de>,
504 {
505 self.deserialize_string(visitor)
506 }
507
508 fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
509 where
510 V: Visitor<'de>,
511 {
512 match self.content {
513 Content::String(v) => visitor.visit_string(v),
514 Content::Str(v) => visitor.visit_borrowed_str(v),
515 Content::ByteBuf(v) => visitor.visit_byte_buf(v),
516 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
517 _ => Err(self.invalid_type(&visitor)),
518 }
519 }
520
521 fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
522 where
523 V: Visitor<'de>,
524 {
525 self.deserialize_byte_buf(visitor)
526 }
527
528 fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
529 where
530 V: Visitor<'de>,
531 {
532 match self.content {
533 Content::String(v) => visitor.visit_string(v),
534 Content::Str(v) => visitor.visit_borrowed_str(v),
535 Content::ByteBuf(v) => visitor.visit_byte_buf(v),
536 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
537 Content::Seq(v) => visit_content_seq(v, visitor),
538 _ => Err(self.invalid_type(&visitor)),
539 }
540 }
541
542 fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, Self::Error>
543 where
544 V: Visitor<'de>,
545 {
546 match self.content {
547 Content::None => visitor.visit_none(),
548 Content::Some(v) => visitor.visit_some(ContentDeserializer::new(*v)),
549 Content::Unit => visitor.visit_unit(),
550 _ => visitor.visit_some(self),
551 }
552 }
553
554 fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
555 where
556 V: Visitor<'de>,
557 {
558 match self.content {
559 Content::Unit => visitor.visit_unit(),
560 _ => Err(self.invalid_type(&visitor)),
561 }
562 }
563
564 fn deserialize_unit_struct<V>(
565 self,
566 _name: &'static str,
567 visitor: V,
568 ) -> Result<V::Value, Self::Error>
569 where
570 V: Visitor<'de>,
571 {
572 match self.content {
573 Content::Map(ref v) if v.is_empty() => visitor.visit_unit(),
588 Content::Seq(ref v) if v.is_empty() => visitor.visit_unit(),
589 _ => self.deserialize_any(visitor),
590 }
591 }
592
593 fn deserialize_newtype_struct<V>(self, _name: &str, visitor: V) -> Result<V::Value, Self::Error>
594 where
595 V: Visitor<'de>,
596 {
597 match self.content {
598 Content::Newtype(v) => visitor.visit_newtype_struct(ContentDeserializer::new(*v)),
599 _ => visitor.visit_newtype_struct(self),
600 }
601 }
602
603 fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
604 where
605 V: Visitor<'de>,
606 {
607 match self.content {
608 Content::Seq(v) => visit_content_seq(v, visitor),
609 _ => Err(self.invalid_type(&visitor)),
610 }
611 }
612
613 fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
614 where
615 V: Visitor<'de>,
616 {
617 self.deserialize_seq(visitor)
618 }
619
620 fn deserialize_tuple_struct<V>(
621 self,
622 _name: &'static str,
623 _len: usize,
624 visitor: V,
625 ) -> Result<V::Value, Self::Error>
626 where
627 V: Visitor<'de>,
628 {
629 self.deserialize_seq(visitor)
630 }
631
632 fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
633 where
634 V: Visitor<'de>,
635 {
636 match self.content {
637 Content::Map(v) => visit_content_map(v, visitor),
638 _ => Err(self.invalid_type(&visitor)),
639 }
640 }
641
642 fn deserialize_struct<V>(
643 self,
644 _name: &'static str,
645 _fields: &'static [&'static str],
646 visitor: V,
647 ) -> Result<V::Value, Self::Error>
648 where
649 V: Visitor<'de>,
650 {
651 match self.content {
652 Content::Seq(v) => visit_content_seq(v, visitor),
653 Content::Map(v) => visit_content_map(v, visitor),
654 _ => Err(self.invalid_type(&visitor)),
655 }
656 }
657
658 fn deserialize_enum<V>(
659 self,
660 _name: &str,
661 _variants: &'static [&'static str],
662 visitor: V,
663 ) -> Result<V::Value, Self::Error>
664 where
665 V: Visitor<'de>,
666 {
667 self.deserialize_any(visitor)
672 }
673
674 fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
675 where
676 V: Visitor<'de>,
677 {
678 match self.content {
679 Content::String(v) => visitor.visit_string(v),
680 Content::Str(v) => visitor.visit_borrowed_str(v),
681 Content::ByteBuf(v) => visitor.visit_byte_buf(v),
682 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
683 Content::U8(v) => visitor.visit_u8(v),
684 Content::U64(v) => visitor.visit_u64(v),
685 _ => Err(self.invalid_type(&visitor)),
686 }
687 }
688
689 fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
690 where
691 V: Visitor<'de>,
692 {
693 drop(self);
694 visitor.visit_unit()
695 }
696}
697
698pub struct ContentRefDeserializer<'a, 'de, E> {
699 content: &'a Content<'de>,
700 err: PhantomData<E>,
701}
702impl<'a, 'de, E> ContentRefDeserializer<'a, 'de, E>
703where
704 E: de::Error,
705{
706 pub fn new(content: &'a Content<'de>) -> Self {
707 ContentRefDeserializer {
708 content,
709 err: PhantomData,
710 }
711 }
712
713 #[cold]
714 fn invalid_type(self, exp: &dyn Expected) -> E {
715 de::Error::invalid_type(self.content.unexpected(), exp)
716 }
717
718 fn deserialize_integer<V>(self, visitor: V) -> Result<V::Value, E>
719 where
720 V: Visitor<'de>,
721 {
722 match *self.content {
723 Content::U8(v) => visitor.visit_u8(v),
724 Content::U16(v) => visitor.visit_u16(v),
725 Content::U32(v) => visitor.visit_u32(v),
726 Content::U64(v) => visitor.visit_u64(v),
727 Content::I8(v) => visitor.visit_i8(v),
728 Content::I16(v) => visitor.visit_i16(v),
729 Content::I32(v) => visitor.visit_i32(v),
730 Content::I64(v) => visitor.visit_i64(v),
731 _ => Err(self.invalid_type(&visitor)),
732 }
733 }
734
735 fn deserialize_float<V>(self, visitor: V) -> Result<V::Value, E>
736 where
737 V: Visitor<'de>,
738 {
739 match *self.content {
740 Content::F32(v) => visitor.visit_f32(v),
741 Content::F64(v) => visitor.visit_f64(v),
742 Content::U8(v) => visitor.visit_u8(v),
743 Content::U16(v) => visitor.visit_u16(v),
744 Content::U32(v) => visitor.visit_u32(v),
745 Content::U64(v) => visitor.visit_u64(v),
746 Content::I8(v) => visitor.visit_i8(v),
747 Content::I16(v) => visitor.visit_i16(v),
748 Content::I32(v) => visitor.visit_i32(v),
749 Content::I64(v) => visitor.visit_i64(v),
750 _ => Err(self.invalid_type(&visitor)),
751 }
752 }
753}
754
755fn visit_content_seq_ref<'a, 'de, V, E>(
756 content: &'a [Content<'de>],
757 visitor: V,
758) -> Result<V::Value, E>
759where
760 V: Visitor<'de>,
761 E: de::Error,
762{
763 let seq = content.iter().map(ContentRefDeserializer::new);
764 let mut seq_visitor = de::value::SeqDeserializer::new(seq);
765 let value = visitor.visit_seq(&mut seq_visitor)?;
766 seq_visitor.end()?;
767 Ok(value)
768}
769
770fn visit_content_map_ref<'a, 'de, V, E>(
771 content: &'a [(Cow<'de, str>, Content<'de>)],
772 visitor: V,
773) -> Result<V::Value, E>
774where
775 V: Visitor<'de>,
776 E: de::Error,
777{
778 let map = content.iter().map(|(k, v)| {
779 (
780 KeyDeserializer::new(k.clone()),
781 ContentRefDeserializer::new(v),
782 )
783 });
784 let mut map_visitor = de::value::MapDeserializer::new(map);
785 let value = visitor.visit_map(&mut map_visitor)?;
786 map_visitor.end()?;
787 Ok(value)
788}
789
790impl<'de, E> Deserializer<'de> for ContentRefDeserializer<'_, 'de, E>
793where
794 E: de::Error,
795{
796 type Error = E;
797
798 fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, E>
799 where
800 V: Visitor<'de>,
801 {
802 match *self.content {
803 Content::Bool(v) => visitor.visit_bool(v),
804 Content::U8(v) => visitor.visit_u8(v),
805 Content::U16(v) => visitor.visit_u16(v),
806 Content::U32(v) => visitor.visit_u32(v),
807 Content::U64(v) => visitor.visit_u64(v),
808 Content::I8(v) => visitor.visit_i8(v),
809 Content::I16(v) => visitor.visit_i16(v),
810 Content::I32(v) => visitor.visit_i32(v),
811 Content::I64(v) => visitor.visit_i64(v),
812 Content::F32(v) => visitor.visit_f32(v),
813 Content::F64(v) => visitor.visit_f64(v),
814 Content::Char(v) => visitor.visit_char(v),
815 Content::String(ref v) => visitor.visit_str(v),
816 Content::Str(v) => visitor.visit_borrowed_str(v),
817 Content::ByteBuf(ref v) => visitor.visit_bytes(v),
818 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
819 Content::Unit => visitor.visit_unit(),
820 Content::None => visitor.visit_none(),
821 Content::Some(ref v) => visitor.visit_some(ContentRefDeserializer::new(v)),
822 Content::Newtype(ref v) => visitor.visit_newtype_struct(ContentRefDeserializer::new(v)),
823 Content::Seq(ref v) => visit_content_seq_ref(v, visitor),
824 Content::Map(ref v) => visit_content_map_ref(v, visitor),
825 }
826 }
827
828 fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, Self::Error>
829 where
830 V: Visitor<'de>,
831 {
832 match *self.content {
833 Content::Bool(v) => visitor.visit_bool(v),
834 _ => Err(self.invalid_type(&visitor)),
835 }
836 }
837
838 fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
839 where
840 V: Visitor<'de>,
841 {
842 self.deserialize_integer(visitor)
843 }
844
845 fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
846 where
847 V: Visitor<'de>,
848 {
849 self.deserialize_integer(visitor)
850 }
851
852 fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
853 where
854 V: Visitor<'de>,
855 {
856 self.deserialize_integer(visitor)
857 }
858
859 fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
860 where
861 V: Visitor<'de>,
862 {
863 self.deserialize_integer(visitor)
864 }
865
866 fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
867 where
868 V: Visitor<'de>,
869 {
870 self.deserialize_integer(visitor)
871 }
872
873 fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, Self::Error>
874 where
875 V: Visitor<'de>,
876 {
877 self.deserialize_integer(visitor)
878 }
879
880 fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
881 where
882 V: Visitor<'de>,
883 {
884 self.deserialize_integer(visitor)
885 }
886
887 fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
888 where
889 V: Visitor<'de>,
890 {
891 self.deserialize_integer(visitor)
892 }
893
894 fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, Self::Error>
895 where
896 V: Visitor<'de>,
897 {
898 self.deserialize_float(visitor)
899 }
900
901 fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, Self::Error>
902 where
903 V: Visitor<'de>,
904 {
905 self.deserialize_float(visitor)
906 }
907
908 fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, Self::Error>
909 where
910 V: Visitor<'de>,
911 {
912 match *self.content {
913 Content::Char(v) => visitor.visit_char(v),
914 Content::String(ref v) => visitor.visit_str(v),
915 Content::Str(v) => visitor.visit_borrowed_str(v),
916 _ => Err(self.invalid_type(&visitor)),
917 }
918 }
919
920 fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, Self::Error>
921 where
922 V: Visitor<'de>,
923 {
924 match *self.content {
925 Content::String(ref v) => visitor.visit_str(v),
926 Content::Str(v) => visitor.visit_borrowed_str(v),
927 Content::ByteBuf(ref v) => visitor.visit_bytes(v),
928 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
929 _ => Err(self.invalid_type(&visitor)),
930 }
931 }
932
933 fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, Self::Error>
934 where
935 V: Visitor<'de>,
936 {
937 self.deserialize_str(visitor)
938 }
939
940 fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, Self::Error>
941 where
942 V: Visitor<'de>,
943 {
944 match *self.content {
945 Content::String(ref v) => visitor.visit_str(v),
946 Content::Str(v) => visitor.visit_borrowed_str(v),
947 Content::ByteBuf(ref v) => visitor.visit_bytes(v),
948 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
949 Content::Seq(ref v) => visit_content_seq_ref(v, visitor),
950 _ => Err(self.invalid_type(&visitor)),
951 }
952 }
953
954 fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, Self::Error>
955 where
956 V: Visitor<'de>,
957 {
958 self.deserialize_bytes(visitor)
959 }
960
961 fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, E>
962 where
963 V: Visitor<'de>,
964 {
965 match *self.content {
966 Content::None => visitor.visit_none(),
967 Content::Some(ref v) => visitor.visit_some(ContentRefDeserializer::new(v)),
968 Content::Unit => visitor.visit_unit(),
969 _ => visitor.visit_some(self),
970 }
971 }
972
973 fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, Self::Error>
974 where
975 V: Visitor<'de>,
976 {
977 match *self.content {
978 Content::Unit => visitor.visit_unit(),
979 _ => Err(self.invalid_type(&visitor)),
980 }
981 }
982
983 fn deserialize_unit_struct<V>(
984 self,
985 _name: &'static str,
986 visitor: V,
987 ) -> Result<V::Value, Self::Error>
988 where
989 V: Visitor<'de>,
990 {
991 self.deserialize_unit(visitor)
992 }
993
994 fn deserialize_newtype_struct<V>(self, _name: &str, visitor: V) -> Result<V::Value, E>
995 where
996 V: Visitor<'de>,
997 {
998 match *self.content {
999 Content::Newtype(ref v) => visitor.visit_newtype_struct(ContentRefDeserializer::new(v)),
1000 _ => visitor.visit_newtype_struct(self),
1001 }
1002 }
1003
1004 fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1005 where
1006 V: Visitor<'de>,
1007 {
1008 match *self.content {
1009 Content::Seq(ref v) => visit_content_seq_ref(v, visitor),
1010 _ => Err(self.invalid_type(&visitor)),
1011 }
1012 }
1013
1014 fn deserialize_tuple<V>(self, _len: usize, visitor: V) -> Result<V::Value, Self::Error>
1015 where
1016 V: Visitor<'de>,
1017 {
1018 self.deserialize_seq(visitor)
1019 }
1020
1021 fn deserialize_tuple_struct<V>(
1022 self,
1023 _name: &'static str,
1024 _len: usize,
1025 visitor: V,
1026 ) -> Result<V::Value, Self::Error>
1027 where
1028 V: Visitor<'de>,
1029 {
1030 self.deserialize_seq(visitor)
1031 }
1032
1033 fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1034 where
1035 V: Visitor<'de>,
1036 {
1037 match *self.content {
1038 Content::Map(ref v) => visit_content_map_ref(v, visitor),
1039 _ => Err(self.invalid_type(&visitor)),
1040 }
1041 }
1042
1043 fn deserialize_struct<V>(
1044 self,
1045 _name: &'static str,
1046 _fields: &'static [&'static str],
1047 visitor: V,
1048 ) -> Result<V::Value, Self::Error>
1049 where
1050 V: Visitor<'de>,
1051 {
1052 match *self.content {
1053 Content::Seq(ref v) => visit_content_seq_ref(v, visitor),
1054 Content::Map(ref v) => visit_content_map_ref(v, visitor),
1055 _ => Err(self.invalid_type(&visitor)),
1056 }
1057 }
1058
1059 fn deserialize_enum<V>(
1060 self,
1061 _name: &str,
1062 _variants: &'static [&'static str],
1063 visitor: V,
1064 ) -> Result<V::Value, Self::Error>
1065 where
1066 V: Visitor<'de>,
1067 {
1068 self.deserialize_any(visitor)
1073 }
1074
1075 fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1076 where
1077 V: Visitor<'de>,
1078 {
1079 match *self.content {
1080 Content::String(ref v) => visitor.visit_str(v),
1081 Content::Str(v) => visitor.visit_borrowed_str(v),
1082 Content::ByteBuf(ref v) => visitor.visit_bytes(v),
1083 Content::Bytes(v) => visitor.visit_borrowed_bytes(v),
1084 Content::U8(v) => visitor.visit_u8(v),
1085 Content::U64(v) => visitor.visit_u64(v),
1086 _ => Err(self.invalid_type(&visitor)),
1087 }
1088 }
1089
1090 fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
1091 where
1092 V: Visitor<'de>,
1093 {
1094 visitor.visit_unit()
1095 }
1096}
1097
1098impl<'de, E> de::IntoDeserializer<'de, E> for ContentDeserializer<'de, E>
1099where
1100 E: de::Error,
1101{
1102 type Deserializer = Self;
1103
1104 fn into_deserializer(self) -> Self {
1105 self
1106 }
1107}
1108
1109impl<'de, E> de::IntoDeserializer<'de, E> for ContentRefDeserializer<'_, 'de, E>
1110where
1111 E: de::Error,
1112{
1113 type Deserializer = Self;
1114
1115 fn into_deserializer(self) -> Self {
1116 self
1117 }
1118}
1119
1120mod size_hint {
1121 #[inline]
1122 pub fn cautious(hint: Option<usize>) -> usize {
1123 std::cmp::min(hint.unwrap_or(0), 4096)
1124 }
1125}