1use crate::lib::{Box, String, ToString};
2
3use crate::io;
4use crate::lib::{fmt, str::Utf8Error};
5
6pub type Result<T> = ::core::result::Result<T, Error>;
8
9pub type Error = Box<ErrorKind>;
11
12#[derive(Debug)]
14pub enum ErrorKind {
15 Io(io::Error),
18 InvalidUtf8Encoding(Utf8Error),
20 InvalidBoolEncoding(u8),
23 InvalidCharEncoding,
25 InvalidTagEncoding(usize),
28 DeserializeAnyNotSupported,
31 SizeLimit,
34 SequenceMustHaveLength,
36 Custom(String),
38}
39
40impl From<io::Error> for Error {
41 fn from(err: io::Error) -> Error {
42 ErrorKind::Io(err).into()
43 }
44}
45
46impl fmt::Display for ErrorKind {
47 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
48 match *self {
49 ErrorKind::Io(ref ioerr) => write!(fmt, "io error: {ioerr}"),
50 ErrorKind::InvalidUtf8Encoding(ref e) => write!(fmt, "InvalidUtf8Encoding: {e}"),
51 ErrorKind::InvalidBoolEncoding(b) => {
52 write!(fmt, "InvalidBoolEncoding, expected 0 or 1, found {b}")
53 }
54 ErrorKind::InvalidCharEncoding => write!(fmt, "InvalidCharEncoding"),
55 ErrorKind::InvalidTagEncoding(tag) => {
56 write!(fmt, "InvalidTagEncoding, found {tag}")
57 }
58 ErrorKind::SequenceMustHaveLength => {
59 write!(fmt, "SequenceMustHaveLength")
60 }
61 ErrorKind::SizeLimit => write!(fmt, "SizeLimit"),
62 ErrorKind::DeserializeAnyNotSupported => {
63 write!(
64 fmt,
65 "EchonetLite-rs does not support the serde::Deserializer::deserialize_any method"
66 )
67 }
68 ErrorKind::Custom(ref s) => s.fmt(fmt),
69 }
70 }
71}
72
73impl serde::de::StdError for Error {
74 #[cfg(feature = "std")]
75 fn source(&self) -> Option<&(dyn serde::de::StdError + 'static)> {
76 match **self {
77 ErrorKind::Io(ref err) => Some(err),
78 _ => None,
79 }
80 }
81}
82
83impl serde::de::Error for Error {
84 fn custom<T: fmt::Display>(desc: T) -> Error {
85 ErrorKind::Custom(desc.to_string()).into()
86 }
87}
88
89impl serde::ser::Error for Error {
90 fn custom<T: fmt::Display>(msg: T) -> Self {
91 ErrorKind::Custom(msg.to_string()).into()
92 }
93}