alloy_rlp/
error.rs

1use core::fmt;
2
3/// RLP result type.
4pub type Result<T, E = Error> = core::result::Result<T, E>;
5
6/// RLP error type.
7#[derive(Clone, Copy, Debug, Eq, PartialEq)]
8pub enum Error {
9    /// Numeric Overflow.
10    Overflow,
11    /// Leading zero disallowed.
12    LeadingZero,
13    /// Overran input while decoding.
14    InputTooShort,
15    /// Expected single byte, but got invalid value.
16    NonCanonicalSingleByte,
17    /// Expected size, but got invalid value.
18    NonCanonicalSize,
19    /// Expected a payload of a specific size, got an unexpected size.
20    UnexpectedLength,
21    /// Expected another type, got a string instead.
22    UnexpectedString,
23    /// Expected another type, got a list instead.
24    UnexpectedList,
25    /// Got an unexpected number of items in a list.
26    ListLengthMismatch {
27        /// Expected length.
28        expected: usize,
29        /// Actual length.
30        got: usize,
31    },
32    /// Custom error.
33    Custom(&'static str),
34}
35
36#[cfg(feature = "std")]
37impl std::error::Error for Error {}
38
39impl fmt::Display for Error {
40    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41        match self {
42            Self::Overflow => f.write_str("overflow"),
43            Self::LeadingZero => f.write_str("leading zero"),
44            Self::InputTooShort => f.write_str("input too short"),
45            Self::NonCanonicalSingleByte => f.write_str("non-canonical single byte"),
46            Self::NonCanonicalSize => f.write_str("non-canonical size"),
47            Self::UnexpectedLength => f.write_str("unexpected length"),
48            Self::UnexpectedString => f.write_str("unexpected string"),
49            Self::UnexpectedList => f.write_str("unexpected list"),
50            Self::ListLengthMismatch { got, expected } => {
51                write!(f, "unexpected list length (got {got}, expected {expected})")
52            }
53            Self::Custom(err) => f.write_str(err),
54        }
55    }
56}