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