1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#[cfg(feature = "derive")]
use serde::{
    de,
    ser,
};
use std::{
    fmt,
    string::FromUtf8Error,
};

pub type Result<T, E = Error> = std::result::Result<T, E>;

#[derive(Debug, thiserror::Error)]
pub enum Error {
    Message(String),
    Eof,
    TrailingCharacters,
    TryIntoSliceError(#[from] std::array::TryFromSliceError),
    Utf8Error(#[from] std::str::Utf8Error),
    FromUtf8Error(#[from] FromUtf8Error),
    FromHexError(#[from] hex::FromHexError),
}

#[cfg(feature = "derive")]
impl ser::Error for Error {
    fn custom<T: fmt::Display>(msg: T) -> Self {
        Error::Message(msg.to_string())
    }
}

#[cfg(feature = "derive")]
impl de::Error for Error {
    fn custom<T: fmt::Display>(msg: T) -> Self {
        Error::Message(msg.to_string())
    }
}

impl fmt::Display for Error {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str(&self.to_string())
    }
}