1use core::fmt;
2
3pub type Result<T> = core::result::Result<T, Error>;
4
5#[derive(Debug)]
6pub enum Error {
7 Message(&'static str),
8 MessageOwned(String),
9 Eof,
10 InvalidHeader(u8),
11 InvalidType(&'static str),
12 InvalidSize,
13 Unsupported(&'static str),
14 Mismatch(&'static str),
15}
16
17impl Error {
18 pub fn msg<T: Into<String>>(msg: T) -> Self {
19 Self::MessageOwned(msg.into())
20 }
21}
22
23impl fmt::Display for Error {
24 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25 match self {
26 Error::Message(m) => write!(f, "{}", m),
27 Error::MessageOwned(m) => write!(f, "{}", m),
28 Error::Eof => write!(f, "unexpected end of input"),
29 Error::InvalidHeader(h) => write!(f, "invalid header: 0x{h:02x}"),
30 Error::InvalidType(t) => write!(f, "invalid type: {t}"),
31 Error::InvalidSize => write!(f, "invalid size"),
32 Error::Unsupported(s) => write!(f, "unsupported: {s}"),
33 Error::Mismatch(s) => write!(f, "type mismatch: {s}"),
34 }
35 }
36}
37
38impl std::error::Error for Error {}
39
40impl serde::ser::Error for Error {
41 fn custom<T: fmt::Display>(msg: T) -> Self {
42 Error::MessageOwned(msg.to_string())
43 }
44}
45
46impl serde::de::Error for Error {
47 fn custom<T: fmt::Display>(msg: T) -> Self {
48 Error::MessageOwned(msg.to_string())
49 }
50}