alloy_primitives/signed/
errors.rs

1use core::fmt;
2use ruint::BaseConvertError;
3
4/// The error type that is returned when parsing a signed integer.
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6pub enum ParseSignedError {
7    /// Error that occurs when an invalid digit is encountered while parsing.
8    Ruint(ruint::ParseError),
9
10    /// Error that occurs when the number is too large or too small (negative)
11    /// and does not fit in the target signed integer.
12    IntegerOverflow,
13}
14
15impl From<ruint::ParseError> for ParseSignedError {
16    fn from(err: ruint::ParseError) -> Self {
17        // these errors are redundant, so we coerce the more complex one to the
18        // simpler one
19        match err {
20            ruint::ParseError::BaseConvertError(BaseConvertError::Overflow) => {
21                Self::IntegerOverflow
22            }
23            _ => Self::Ruint(err),
24        }
25    }
26}
27
28impl core::error::Error for ParseSignedError {
29    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
30        match self {
31            #[cfg(feature = "std")]
32            Self::Ruint(err) => Some(err),
33            _ => None,
34        }
35    }
36}
37
38impl fmt::Display for ParseSignedError {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        match self {
41            Self::Ruint(e) => e.fmt(f),
42            Self::IntegerOverflow => f.write_str("number does not fit in the integer size"),
43        }
44    }
45}
46
47/// The error type that is returned when conversion to or from a integer fails.
48#[derive(Clone, Copy, Debug, PartialEq, Eq)]
49pub struct BigIntConversionError;
50
51impl core::error::Error for BigIntConversionError {}
52
53impl fmt::Display for BigIntConversionError {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        f.write_str("output of range integer conversion attempted")
56    }
57}