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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use core::fmt::{self, Debug};
use nano_leb128;

/// Error type for encode and decode operations.
#[derive(Clone, PartialEq)]
pub enum Error {
    LEB128DecodeError(nano_leb128::LEB128DecodeError),
    LEB128EncodeError(nano_leb128::LEB128EncodeError),
    FromUtf8Error,
    UnimplementedB3TypeError,
    DictItemWithoutKeyError,
    UnexpectedEof,
    InvalidHeader,
    UnknownError,
}

impl From<nano_leb128::LEB128DecodeError> for Error {
    fn from(err: nano_leb128::LEB128DecodeError) -> Self {
        Self::LEB128DecodeError(err)
    }
}

impl From<nano_leb128::LEB128EncodeError> for Error {
    fn from(err: nano_leb128::LEB128EncodeError) -> Self {
        Self::LEB128EncodeError(err)
    }
}

#[cfg(not(feature = "std"))]
impl From<alloc::string::FromUtf8Error> for Error {
    fn from(_: alloc::string::FromUtf8Error) -> Self {
        Self::FromUtf8Error
    }
}

#[cfg(feature = "std")]
impl From<std::string::FromUtf8Error> for Error {
    fn from(_: std::string::FromUtf8Error) -> Self {
        Self::FromUtf8Error
    }
}

impl Debug for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::LEB128DecodeError(err) => f.debug_tuple("LEB128DecodeError").field(&err).finish(),
            Self::LEB128EncodeError(err) => f.debug_tuple("LEB128EncodeError").field(&err).finish(),
            Self::FromUtf8Error => write!(f, "FromUtf8Error"),
            Self::UnimplementedB3TypeError => write!(f, "UnimplementedB3TypeError"),
            Self::DictItemWithoutKeyError => write!(f, "DictItemWithoutKeyError"),
            Self::UnexpectedEof => write!(f, "UnexpectedEof"),
            Self::InvalidHeader => write!(f, "InvalidHeader"),
            Self::UnknownError => write!(f, "UnknownError"),
        }
    }
}