malachite_bigint/
error.rs

1#[derive(Debug, Clone, PartialEq, Eq)]
2pub struct ParseBigIntError {
3    kind: BigIntErrorKind,
4}
5
6#[derive(Debug, Clone, PartialEq, Eq)]
7enum BigIntErrorKind {
8    Empty,
9    InvalidDigit,
10}
11
12impl ParseBigIntError {
13    const fn __description(&self) -> &str {
14        use BigIntErrorKind::*;
15        match self.kind {
16            Empty => "cannot parse integer from empty string",
17            InvalidDigit => "invalid digit found in string",
18        }
19    }
20
21    pub(crate) const fn empty() -> Self {
22        Self {
23            kind: BigIntErrorKind::Empty,
24        }
25    }
26
27    pub(crate) const fn invalid() -> Self {
28        Self {
29            kind: BigIntErrorKind::InvalidDigit,
30        }
31    }
32}
33
34impl core::fmt::Display for ParseBigIntError {
35    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
36        self.__description().fmt(f)
37    }
38}
39
40impl core::error::Error for ParseBigIntError {
41    fn description(&self) -> &str {
42        self.__description()
43    }
44}
45
46#[derive(Debug, Copy, Clone, PartialEq, Eq)]
47pub struct TryFromBigIntError<T> {
48    original: T,
49}
50
51impl<T> TryFromBigIntError<T> {
52    pub(crate) const fn new(original: T) -> Self {
53        Self { original }
54    }
55
56    #[allow(clippy::unused_self)]
57    const fn __description(&self) -> &str {
58        "out of range conversion regarding big integer attempted"
59    }
60
61    pub fn into_original(self) -> T {
62        self.original
63    }
64}
65
66impl<T> core::error::Error for TryFromBigIntError<T>
67where
68    T: core::fmt::Debug,
69{
70    fn description(&self) -> &str {
71        self.__description()
72    }
73}
74
75impl<T> core::fmt::Display for TryFromBigIntError<T> {
76    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
77        self.__description().fmt(f)
78    }
79}