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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// numera::error
//
//! Error types.
//

/// The *numera* common result type.
pub type NumeraResult<N> = core::result::Result<N, NumeraError>;

/// The *numera* common error type.
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum NumeraError {
    /// An error involving integer numbers.
    Integer(IntegerError),

    /// An error involving rational numbers.
    Rational(RationalError),

    /// An error involving real numbers.
    Real(RealError),

    /// Couldn't convert between two kinds of numbers.
    Conversion,

    /// Not implemented.
    NotImplemented,

    /// A miscellaneous error message.
    Other(&'static str),
}

/// Errors related to [`integer`][crate::number::integer]s.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum IntegerError {
    /// Invalid value `0`.
    Zero,

    /// Invalid value `>= 0`.
    ZeroOrMore,

    /// Invalid value `<= 0`.
    ZeroOrLess,

    /// Invalid value `< 0`.
    LessThanZero,

    /// Invalid value `> 0`.
    MoreThanZero,

    /// The value is too large to store in the current representation.
    Overflow,

    /// The value is too small to store in the current representation.
    Underflow,

    /// The integer is not a prime.
    NotPrime,
}

/// Errors related to `rational`s.
// Errors related to [`rational`][crate::number::rational]s.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RationalError {
    /// Invalid value `0` in the denominator.
    ZeroDenominator,

    /// The value is too large to store in the current representation of the
    /// numerator.
    NumeratorOverflow,

    /// The value is too small to store in the current representation of the
    /// numerator
    NumeratorUnderflow,

    /// The value is too large to store in the current representation of the
    /// denominator.
    DenominatorOverflow,

    /// The value is too small to store in the current representation of the
    /// denominator
    DenominatorUnderflow,
}

/// Errors related to `real`s.
// Errors related to [`real`][crate::number::real]s.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RealError {
    /// Invalid value Nan.
    NaN,

    Other, // TEMP
}

mod core_impls {
    use super::{IntegerError, NumeraError, RationalError, RealError};
    use core::{
        convert::Infallible,
        fmt::{self, Debug},
        num::{IntErrorKind, TryFromIntError},
    };

    impl fmt::Display for NumeraError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            use NumeraError::*;
            match self {
                Integer(z) => Debug::fmt(z, f),
                Rational(q) => Debug::fmt(q, f),
                Real(r) => Debug::fmt(r, f),
                Conversion => write!(f, "Couldn't convert the number."),
                NotImplemented => write!(f, "Not implemented."),
                Other(s) => write!(f, "{s}"),
            }
        }
    }
    impl fmt::Display for IntegerError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            use IntegerError::*;
            match self {
                Zero => write!(f, "Zero"),
                ZeroOrMore => write!(f, "ZeroOrMore"),
                ZeroOrLess => write!(f, "ZeroOrLess"),
                LessThanZero => write!(f, "LessThanZero"),
                MoreThanZero => write!(f, "MoreThanZero"),
                Overflow => write!(f, "Overflow"),
                Underflow => write!(f, "Underflow"),
                NotPrime => write!(f, "NotPrime"),
            }
        }
    }
    impl fmt::Display for RationalError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            use RationalError::*;
            match self {
                ZeroDenominator => write!(f, "ZeroDenominator"),
                NumeratorOverflow => write!(f, "NumeratorOverflow"),
                NumeratorUnderflow => write!(f, "NumeratorUnderflow"),
                DenominatorOverflow => write!(f, "DenominatorOverflow"),
                DenominatorUnderflow => write!(f, "DenominatorUnderflow"),
            }
        }
    }
    impl fmt::Display for RealError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            use RealError::*;
            match self {
                NaN => write!(f, "Nan"),
                _ => write!(f, "Other"),
            }
        }
    }

    impl PartialEq<IntegerError> for NumeraError {
        #[inline]
        fn eq(&self, other: &IntegerError) -> bool {
            match self {
                NumeraError::Integer(err) => err == other,
                _ => false,
            }
        }
    }
    impl PartialEq<RationalError> for NumeraError {
        #[inline]
        fn eq(&self, other: &RationalError) -> bool {
            match self {
                NumeraError::Rational(err) => err == other,
                _ => false,
            }
        }
    }
    impl PartialEq<RealError> for NumeraError {
        #[inline]
        fn eq(&self, other: &RealError) -> bool {
            match self {
                NumeraError::Real(err) => err == other,
                _ => false,
            }
        }
    }

    impl From<IntegerError> for NumeraError {
        fn from(err: IntegerError) -> Self {
            NumeraError::Integer(err)
        }
    }

    impl From<RationalError> for NumeraError {
        fn from(err: RationalError) -> Self {
            NumeraError::Rational(err)
        }
    }
    impl From<RealError> for NumeraError {
        fn from(err: RealError) -> Self {
            NumeraError::Real(err)
        }
    }

    impl From<IntErrorKind> for NumeraError {
        fn from(err: IntErrorKind) -> Self {
            use {IntErrorKind::*, NumeraError::Integer};
            match err {
                PosOverflow => Integer(IntegerError::Overflow),
                NegOverflow => Integer(IntegerError::Underflow),
                Zero => Integer(IntegerError::Zero),
                //
                Empty => NumeraError::Other("IntErrorKind::Empty"),
                InvalidDigit => NumeraError::Other("IntErrorKind::InvalidDigit"),
                _ => NumeraError::Other("IntErrorKind::_"),
            }
        }
    }
    impl From<TryFromIntError> for NumeraError {
        fn from(_err: TryFromIntError) -> Self {
            IntegerError::Overflow.into()
        }
    }

    impl From<Infallible> for NumeraError {
        fn from(_err: Infallible) -> Self {
            NumeraError::Conversion
        }
    }
}

#[cfg(feature = "dashu-base")]
mod dashu_base {
    use super::NumeraError;
    use dashu_base::error::{ConversionError, ParseError};

    impl From<ConversionError> for NumeraError {
        #[inline]
        fn from(_err: ConversionError) -> Self {
            NumeraError::Conversion
        }
    }
    // ParseError { NoDigits, InvalidDigit }
    impl From<ParseError> for NumeraError {
        #[inline]
        fn from(_err: ParseError) -> Self {
            NumeraError::Conversion
        }
    }
}

#[cfg(feature = "std")]
#[cfg_attr(feature = "nightly", doc(cfg(feature = "std")))]
mod std_impls {
    use super::{IntegerError, NumeraError, RationalError, RealError};
    use std::error::Error as StdError;

    impl StdError for NumeraError {}
    impl StdError for IntegerError {}
    impl StdError for RationalError {}
    impl StdError for RealError {}
}