1#![allow(unused)]
2
3pub mod en {
4
5 use super::make_msg;
6 use crate::lib::*;
7
8 use serde::ser;
9
10 pub struct Error {
11 #[cfg(feature = "alloc")]
12 source: Option<Box<dyn Display>>,
13
14 pub kind: ErrorKind,
15 #[cfg(not(feature = "alloc"))]
16 msg: &'static str,
17 #[cfg(feature = "alloc")]
18 msg: String,
19 }
20
21 impl Debug for Error {
22 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23 write!(f, "Error {{ ")?;
24 #[cfg(feature = "alloc")]
25 {
26 write!(f, "source: ")?;
27 if let Some(s) = self.source.as_ref() {
28 s.as_ref().fmt(f)
29 } else {
30 write!(f, "None")
31 }
32 }?;
33 write!(f, " kind: {}, msg: {} }}", self.kind, self.msg)
34 }
35 }
36
37 #[derive(Debug, Clone)]
38 pub enum ErrorKind {
39 Write,
40 Message,
41 Custom,
42 Unknow,
43 Unsupported128BitInteger,
44 }
45 impl Display for ErrorKind {
46 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47 match self {
48 ErrorKind::Write => write!(f, "Write"),
49 ErrorKind::Message => write!(f, "Message"),
50 ErrorKind::Custom => write!(f, "Custom"),
51 ErrorKind::Unknow => write!(f, "Unknow"),
52 ErrorKind::Unsupported128BitInteger => write!(f, "Unsupported128BitIntege"),
53 }
54 }
55 }
56
57 impl Display for Error {
58 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> fmt::Result {
59 #[cfg(feature = "alloc")]
60 {
61 if let Some(e) = self.source.as_ref() {
62 fmt::Display::fmt(e, f)
63 } else {
64 write!(f, "error type: {}, error msg: {}", self.kind, self.msg)
65 }
66 }
67 #[cfg(not(feature = "alloc"))]
68 {
69 write!(f, "error type: {}, error msg: {}", self.kind, self.msg)
70 }
71 }
72 }
73
74 #[cfg(feature = "std")]
75 impl ser::StdError for Error {
76 fn source(&self) -> Option<&(dyn ser::StdError + 'static)> {
77 None
78 }
79 }
80
81 impl ser::Error for Error {
82 fn custom<T>(msg: T) -> Self
83 where
84 T: Display,
85 {
86 Error {
87 #[cfg(feature = "alloc")]
88 source: None,
89 kind: ErrorKind::Custom,
90
91 #[cfg(feature = "alloc")]
92 msg: msg.to_string(),
93 #[cfg(not(feature = "alloc"))]
94 msg: "",
95 }
96 }
97 }
98
99 impl<E> From<minicbor::encode::Error<E>> for Error
100 where
101 E: Display + 'static,
102 {
103 fn from(e: minicbor::encode::Error<E>) -> Self {
104 let kind = if e.is_message() {
105 ErrorKind::Message
106 } else {
107 ErrorKind::Write
108 };
109 Error {
110 #[cfg(feature = "alloc")]
111 source: Some(Box::new(e)),
112 kind,
113 msg: make_msg(""),
114 }
115 }
116 }
117
118 pub(crate) fn make_kind_err(e: ErrorKind, msg: &'static str) -> Error {
119 Error {
120 #[cfg(feature = "alloc")]
121 source: None,
122 kind: e,
123 msg: make_msg(msg),
124 }
125 }
126}
127
128pub mod de {
129 use crate::lib::*;
130 use core::fmt::{self, Debug, Display, Formatter};
131
132 use minicbor::data::Type;
133
134 use super::make_msg;
135 use serde::de;
136
137 #[derive(Debug)]
138 pub struct Error {
139 source: Option<minicbor::decode::Error>,
140 pub kind: ErrorKind,
141 #[cfg(not(feature = "alloc"))]
142 msg: &'static str,
143 #[cfg(feature = "alloc")]
144 msg: String,
145 }
146
147 #[cfg(feature = "std")]
148 impl de::StdError for Error {
149 fn source(&self) -> Option<&(dyn de::StdError + 'static)> {
150 if let Some(e) = self.source.as_ref() {
151 Some(e)
152 } else {
153 None
154 }
155 }
156 }
157
158 #[derive(Debug, Clone)]
159 pub enum ErrorKind {
160 Message,
161 EndOfInput,
163 InvalidChar,
165 TypeMismatch(Option<Type>),
166 UnknownVariant,
168 MissingValue,
170 Unsupported128BitInteger,
172
173 Custom,
174 Unknow,
175 }
176
177 impl fmt::Display for ErrorKind {
178 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
179 match self {
180 ErrorKind::Message => write!(f, "Message"),
181 ErrorKind::EndOfInput => write!(f, "EndOfInput"),
182 ErrorKind::InvalidChar => write!(f, "InvalidChar"),
183 ErrorKind::TypeMismatch(t) => {
184 if let Some(ty) = t.as_ref() {
185 write!(f, "TypeMismatch{{ {} }}", ty)
186 } else {
187 write!(f, "TypeMismatch")
188 }
189 }
190 ErrorKind::UnknownVariant => write!(f, "UnknownVariant"),
191 ErrorKind::MissingValue => write!(f, "MissingValue"),
192 ErrorKind::Unsupported128BitInteger => write!(f, "Unsupported128BitInteger"),
193 ErrorKind::Custom => write!(f, "Custom"),
194 ErrorKind::Unknow => write!(f, "Unknow"),
195 }
196 }
197 }
198
199 impl Display for Error {
200 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
201 if let Some(e) = self.source.as_ref() {
202 core::fmt::Display::fmt(&e, f)
203 } else {
204 write!(f, "error kind: {}, msg: {}", self.kind, self.msg)
205 }
206 }
207 }
208
209 impl de::Error for Error {
210 fn custom<T>(msg: T) -> Self
211 where
212 T: Display,
213 {
214 make_custom_err(msg)
215 }
216 }
217
218 #[inline]
219 fn make_custom_err<T: Display>(msg: T) -> Error {
220 Error {
221 source: None,
222 kind: ErrorKind::Custom,
223
224 #[cfg(feature = "alloc")]
225 msg: msg.to_string(),
226 #[cfg(not(feature = "alloc"))]
227 msg: "",
228 }
229 }
230
231 impl From<minicbor::decode::Error> for Error {
232 fn from(e: minicbor::decode::Error) -> Self {
233 let kind = if e.is_end_of_input() {
234 ErrorKind::EndOfInput
235 } else if e.is_message() {
236 ErrorKind::Message
237 } else if e.is_type_mismatch() {
238 ErrorKind::TypeMismatch(None)
240 } else if e.is_unknown_variant() {
241 ErrorKind::UnknownVariant
242 } else if e.is_missing_value() {
243 ErrorKind::MissingValue
244 } else {
245 ErrorKind::Unknow
250 };
251 Error {
252 source: Some(e),
253 kind,
254 msg: make_msg(""),
255 }
256 }
257 }
258 #[inline]
259 pub(crate) fn make_kind_err(e: ErrorKind, msg: &'static str) -> Error {
260 Error {
261 source: None,
262 kind: e,
263 msg: make_msg(msg),
264 }
265 }
266 #[inline]
267 pub(crate) fn type_mismatch(t: Type, s: &'static str) -> Error {
268 Error {
269 source: None,
270 kind: ErrorKind::TypeMismatch(Some(t)),
271 msg: make_msg(s),
272 }
273 }
274}
275
276#[cfg(not(feature = "alloc"))]
277#[inline]
278fn make_msg(msg: &'static str) -> &'static str {
279 msg
280}
281
282use crate::lib::*;
283#[cfg(feature = "alloc")]
284#[inline]
285fn make_msg(msg: &str) -> String {
286 msg.into()
287}