alloy_rlp/
error.rs

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;

/// RLP result type.
pub type Result<T, E = Error> = core::result::Result<T, E>;

/// RLP error type.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Error {
    /// Numeric Overflow.
    Overflow,
    /// Leading zero disallowed.
    LeadingZero,
    /// Overran input while decoding.
    InputTooShort,
    /// Expected single byte, but got invalid value.
    NonCanonicalSingleByte,
    /// Expected size, but got invalid value.
    NonCanonicalSize,
    /// Expected a payload of a specific size, got an unexpected size.
    UnexpectedLength,
    /// Expected another type, got a string instead.
    UnexpectedString,
    /// Expected another type, got a list instead.
    UnexpectedList,
    /// Got an unexpected number of items in a list.
    ListLengthMismatch {
        /// Expected length.
        expected: usize,
        /// Actual length.
        got: usize,
    },
    /// Custom error.
    Custom(&'static str),
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Overflow => f.write_str("overflow"),
            Self::LeadingZero => f.write_str("leading zero"),
            Self::InputTooShort => f.write_str("input too short"),
            Self::NonCanonicalSingleByte => f.write_str("non-canonical single byte"),
            Self::NonCanonicalSize => f.write_str("non-canonical size"),
            Self::UnexpectedLength => f.write_str("unexpected length"),
            Self::UnexpectedString => f.write_str("unexpected string"),
            Self::UnexpectedList => f.write_str("unexpected list"),
            Self::ListLengthMismatch { got, expected } => {
                write!(f, "unexpected list length (got {got}, expected {expected})")
            }
            Self::Custom(err) => f.write_str(err),
        }
    }
}