echonet_lite/
error.rs

1use crate::lib::{Box, String, ToString};
2
3use crate::io;
4use crate::lib::{fmt, str::Utf8Error};
5
6/// The result of a serialization or deserialization operation.
7pub type Result<T> = ::core::result::Result<T, Error>;
8
9/// An error that can be produced during (de)serializing.
10pub type Error = Box<ErrorKind>;
11
12/// The kind of error that can be produced during a serialization or deserialization.
13#[derive(Debug)]
14pub enum ErrorKind {
15    /// If the error stems from the reader/writer that is being used
16    /// during (de)serialization, that error will be stored and returned here.
17    Io(io::Error),
18    /// Returned if the deserializer attempts to deserialize a string that is not valid utf8
19    InvalidUtf8Encoding(Utf8Error),
20    /// Returned if the deserializer attempts to deserialize a bool that was
21    /// not encoded as either a 1 or a 0
22    InvalidBoolEncoding(u8),
23    /// Returned if the deserializer attempts to deserialize a char that is not in the correct format.
24    InvalidCharEncoding,
25    /// Returned if the deserializer attempts to deserialize the tag of an enum that is
26    /// not in the expected ranges
27    InvalidTagEncoding(usize),
28    /// Serde has a deserialize_any method that lets the format hint to the
29    /// object which route to take in deserializing.
30    DeserializeAnyNotSupported,
31    /// If (de)serializing a message takes more than the provided size limit, this
32    /// error is returned.
33    SizeLimit,
34    /// echonet-lite-rs can not encode sequences of unknown length (like iterators).
35    SequenceMustHaveLength,
36    /// A custom error message from Serde.
37    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}