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
use std::{
    fmt::{self, Display},
    num::IntErrorKind,
};

/// An error which can be returned when parsing a finite-field integer.
///
/// This error type closely mirrors `std::num::ParseIntError`.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ParseIntError(IntErrorKind);

impl ParseIntError {
    /// Outputs the detailed cause of parsing an integer failing.
    pub fn kind(&self) -> &IntErrorKind {
        &self.0
    }

    fn description(&self) -> &str {
        match self.0 {
            IntErrorKind::Empty => "cannot parse integer from empty string",
            IntErrorKind::InvalidDigit => "invalid digit found in string",
            IntErrorKind::PosOverflow => {
                "number too large to fit in target type"
            }
            IntErrorKind::NegOverflow => {
                "number too small to fit in target type"
            }
            IntErrorKind::Zero => "number would be zero for non-zero type",
            _ => "unknown error",
        }
    }
}

impl Display for ParseIntError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.description().fmt(f)
    }
}

impl From<std::num::ParseIntError> for ParseIntError {
    fn from(source: std::num::ParseIntError) -> Self {
        Self(source.kind().clone())
    }
}

impl From<IntErrorKind> for ParseIntError {
    fn from(source: IntErrorKind) -> Self {
        Self(source)
    }
}