1use alloc::borrow::ToOwned;
2use alloc::string::{String, ToString};
3use alloc::vec::Vec;
4use core::fmt::{self, Debug, Display};
5use serde::de::Expected;
6
7pub struct Error {
8 imp: ErrorImpl,
9}
10
11pub(crate) fn erase<E: serde::de::Error>(err: E) -> Error {
12 serde::de::Error::custom(err)
13}
14
15pub(crate) fn unerase<E: serde::de::Error>(err: Error) -> E {
16 err.as_serde()
17}
18
19impl serde::de::StdError for Error {}
20
21impl Display for Error {
22 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
23 let error = self.as_serde::<serde::de::value::Error>();
24 Display::fmt(&error, formatter)
25 }
26}
27
28impl Debug for Error {
29 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
30 let error = self.as_serde::<serde::de::value::Error>();
31 Debug::fmt(&error, formatter)
32 }
33}
34
35enum ErrorImpl {
36 Custom(String),
37 InvalidType {
38 unexpected: Unexpected,
39 expected: String,
40 },
41 InvalidValue {
42 unexpected: Unexpected,
43 expected: String,
44 },
45 InvalidLength {
46 len: usize,
47 expected: String,
48 },
49 UnknownVariant {
50 variant: String,
51 expected: &'static [&'static str],
52 },
53 UnknownField {
54 field: String,
55 expected: &'static [&'static str],
56 },
57 MissingField {
58 field: &'static str,
59 },
60 DuplicateField {
61 field: &'static str,
62 },
63}
64
65enum Unexpected {
66 Bool(bool),
67 Unsigned(u64),
68 Signed(i64),
69 Float(f64),
70 Char(char),
71 Str(String),
72 Bytes(Vec<u8>),
73 Unit,
74 Option,
75 NewtypeStruct,
76 Seq,
77 Map,
78 Enum,
79 UnitVariant,
80 NewtypeVariant,
81 TupleVariant,
82 StructVariant,
83 Other(String),
84}
85
86impl serde::de::Error for Error {
87 fn custom<T: Display>(msg: T) -> Self {
88 let imp = ErrorImpl::Custom(msg.to_string());
89 Error { imp }
90 }
91
92 fn invalid_type(unexpected: serde::de::Unexpected, expected: &dyn Expected) -> Self {
93 let imp = ErrorImpl::InvalidType {
94 unexpected: Unexpected::from_serde(unexpected),
95 expected: expected.to_string(),
96 };
97 Error { imp }
98 }
99
100 fn invalid_value(unexpected: serde::de::Unexpected, expected: &dyn Expected) -> Self {
101 let imp = ErrorImpl::InvalidValue {
102 unexpected: Unexpected::from_serde(unexpected),
103 expected: expected.to_string(),
104 };
105 Error { imp }
106 }
107
108 fn invalid_length(len: usize, expected: &dyn Expected) -> Self {
109 let imp = ErrorImpl::InvalidLength {
110 len,
111 expected: expected.to_string(),
112 };
113 Error { imp }
114 }
115
116 fn unknown_variant(variant: &str, expected: &'static [&'static str]) -> Self {
117 let imp = ErrorImpl::UnknownVariant {
118 variant: variant.to_owned(),
119 expected,
120 };
121 Error { imp }
122 }
123
124 fn unknown_field(field: &str, expected: &'static [&'static str]) -> Self {
125 let imp = ErrorImpl::UnknownField {
126 field: field.to_owned(),
127 expected,
128 };
129 Error { imp }
130 }
131
132 fn missing_field(field: &'static str) -> Self {
133 let imp = ErrorImpl::MissingField { field };
134 Error { imp }
135 }
136
137 fn duplicate_field(field: &'static str) -> Self {
138 let imp = ErrorImpl::DuplicateField { field };
139 Error { imp }
140 }
141}
142
143impl Error {
144 fn as_serde<E: serde::de::Error>(&self) -> E {
145 match &self.imp {
146 ErrorImpl::Custom(msg) => E::custom(msg),
147 ErrorImpl::InvalidType {
148 unexpected,
149 expected,
150 } => E::invalid_type(unexpected.as_serde(), &expected.as_str()),
151 ErrorImpl::InvalidValue {
152 unexpected,
153 expected,
154 } => E::invalid_value(unexpected.as_serde(), &expected.as_str()),
155 ErrorImpl::InvalidLength { len, expected } => {
156 E::invalid_length(*len, &expected.as_str())
157 }
158 ErrorImpl::UnknownVariant { variant, expected } => {
159 E::unknown_variant(variant, expected)
160 }
161 ErrorImpl::UnknownField { field, expected } => E::unknown_field(field, expected),
162 ErrorImpl::MissingField { field } => E::missing_field(field),
163 ErrorImpl::DuplicateField { field } => E::duplicate_field(field),
164 }
165 }
166}
167
168impl Unexpected {
169 fn from_serde(unexpected: serde::de::Unexpected) -> Self {
170 match unexpected {
171 serde::de::Unexpected::Bool(value) => Unexpected::Bool(value),
172 serde::de::Unexpected::Unsigned(value) => Unexpected::Unsigned(value),
173 serde::de::Unexpected::Signed(value) => Unexpected::Signed(value),
174 serde::de::Unexpected::Float(value) => Unexpected::Float(value),
175 serde::de::Unexpected::Char(value) => Unexpected::Char(value),
176 serde::de::Unexpected::Str(value) => Unexpected::Str(value.to_owned()),
177 serde::de::Unexpected::Bytes(value) => Unexpected::Bytes(value.to_owned()),
178 serde::de::Unexpected::Unit => Unexpected::Unit,
179 serde::de::Unexpected::Option => Unexpected::Option,
180 serde::de::Unexpected::NewtypeStruct => Unexpected::NewtypeStruct,
181 serde::de::Unexpected::Seq => Unexpected::Seq,
182 serde::de::Unexpected::Map => Unexpected::Map,
183 serde::de::Unexpected::Enum => Unexpected::Enum,
184 serde::de::Unexpected::UnitVariant => Unexpected::UnitVariant,
185 serde::de::Unexpected::NewtypeVariant => Unexpected::NewtypeVariant,
186 serde::de::Unexpected::TupleVariant => Unexpected::TupleVariant,
187 serde::de::Unexpected::StructVariant => Unexpected::StructVariant,
188 serde::de::Unexpected::Other(msg) => Unexpected::Other(msg.to_owned()),
189 }
190 }
191
192 fn as_serde(&self) -> serde::de::Unexpected {
193 match self {
194 Unexpected::Bool(value) => serde::de::Unexpected::Bool(*value),
195 Unexpected::Unsigned(value) => serde::de::Unexpected::Unsigned(*value),
196 Unexpected::Signed(value) => serde::de::Unexpected::Signed(*value),
197 Unexpected::Float(value) => serde::de::Unexpected::Float(*value),
198 Unexpected::Char(value) => serde::de::Unexpected::Char(*value),
199 Unexpected::Str(value) => serde::de::Unexpected::Str(value),
200 Unexpected::Bytes(value) => serde::de::Unexpected::Bytes(value),
201 Unexpected::Unit => serde::de::Unexpected::Unit,
202 Unexpected::Option => serde::de::Unexpected::Option,
203 Unexpected::NewtypeStruct => serde::de::Unexpected::NewtypeStruct,
204 Unexpected::Seq => serde::de::Unexpected::Seq,
205 Unexpected::Map => serde::de::Unexpected::Map,
206 Unexpected::Enum => serde::de::Unexpected::Enum,
207 Unexpected::UnitVariant => serde::de::Unexpected::UnitVariant,
208 Unexpected::NewtypeVariant => serde::de::Unexpected::NewtypeVariant,
209 Unexpected::TupleVariant => serde::de::Unexpected::TupleVariant,
210 Unexpected::StructVariant => serde::de::Unexpected::StructVariant,
211 Unexpected::Other(msg) => serde::de::Unexpected::Other(msg),
212 }
213 }
214}