serde_content/de/
error.rs1use crate::{Data, Enum, Error, Expected, Found, FoundData, Number, Struct, Value};
2use alloc::boxed::Box;
3use alloc::vec::Vec;
4
5pub trait Unexpected {
7 fn unexpected(self, expected: Expected) -> Error;
9}
10
11impl Unexpected for Number {
12 fn unexpected(self, expected: Expected) -> Error {
13 let found = Found::Number(self);
14 Error::unexpected(found, expected)
15 }
16}
17
18impl Value<'_> {
19 fn into_found(self) -> Found {
20 match self {
21 Value::Unit => Found::Unit,
22 Value::Bool(v) => Found::Bool(v),
23 Value::Number(v) => Found::Number(v),
24 Value::Char(v) => Found::Char(v),
25 Value::String(v) => Found::String(v.into_owned()),
26 Value::Bytes(v) => Found::Bytes(v.into_owned()),
27 Value::Seq(v) => {
28 let mut vec = Vec::with_capacity(v.len());
29 for value in v {
30 vec.push(value.into_found());
31 }
32 Found::Seq(vec)
33 }
34 Value::Map(v) => {
35 let mut vec = Vec::with_capacity(v.len());
36 for (key, value) in v {
37 vec.push((key.into_found(), value.into_found()));
38 }
39 Found::Map(vec)
40 }
41 Value::Option(v) => Found::Option(v.map(|x| Box::new(x.into_found()))),
42 Value::Struct(v) => Found::Struct {
43 name: v.name.into_owned(),
44 data: Box::new(v.data.into_found()),
45 },
46 Value::Enum(v) => Found::Enum {
47 name: v.name.into_owned(),
48 variant: v.variant.into_owned(),
49 data: Box::new(v.data.into_found()),
50 },
51 Value::Tuple(v) => {
52 let mut vec = Vec::with_capacity(v.len());
53 for value in v {
54 vec.push(value.into_found());
55 }
56 Found::Tuple(vec)
57 }
58 }
59 }
60}
61
62impl Data<'_> {
63 fn into_found(self) -> FoundData {
64 match self {
65 Data::Unit => FoundData::Unit,
66 Data::NewType { value } => FoundData::NewType(value.into_found()),
67 Data::Tuple { values } => {
68 FoundData::Tuple(values.into_iter().map(Value::into_found).collect())
69 }
70 Data::Struct { fields } => FoundData::Struct(
71 fields
72 .into_iter()
73 .map(|(k, v)| (k.into_owned(), v.into_found()))
74 .collect(),
75 ),
76 }
77 }
78}
79
80impl Unexpected for Value<'_> {
81 fn unexpected(self, expected: Expected) -> Error {
82 let found = self.into_found();
83 Error::unexpected(found, expected)
84 }
85}
86
87impl Unexpected for Box<Struct<'_>> {
88 fn unexpected(self, expected: Expected) -> Error {
89 let found = Found::Struct {
90 name: self.name.into_owned(),
91 data: Box::new(self.data.into_found()),
92 };
93 Error::unexpected(found, expected)
94 }
95}
96
97impl Unexpected for Box<Enum<'_>> {
98 fn unexpected(self, expected: Expected) -> Error {
99 let found = Found::Enum {
100 name: self.name.into_owned(),
101 variant: self.variant.into_owned(),
102 data: Box::new(self.data.into_found()),
103 };
104 Error::unexpected(found, expected)
105 }
106}