alloy_primitives/signed/
errors.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
57
58
use core::fmt;
use ruint::BaseConvertError;

/// The error type that is returned when parsing a signed integer.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ParseSignedError {
    /// Error that occurs when an invalid digit is encountered while parsing.
    Ruint(ruint::ParseError),

    /// Error that occurs when the number is too large or too small (negative)
    /// and does not fit in the target signed integer.
    IntegerOverflow,
}

impl From<ruint::ParseError> for ParseSignedError {
    fn from(err: ruint::ParseError) -> Self {
        // these errors are redundant, so we coerce the more complex one to the
        // simpler one
        match err {
            ruint::ParseError::BaseConvertError(BaseConvertError::Overflow) => {
                Self::IntegerOverflow
            }
            _ => Self::Ruint(err),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for ParseSignedError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Ruint(err) => Some(err),
            Self::IntegerOverflow => None,
        }
    }
}

impl fmt::Display for ParseSignedError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Ruint(e) => e.fmt(f),
            Self::IntegerOverflow => f.write_str("number does not fit in the integer size"),
        }
    }
}

/// The error type that is returned when conversion to or from a integer fails.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct BigIntConversionError;

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

impl fmt::Display for BigIntConversionError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("output of range integer conversion attempted")
    }
}