Skip to main content

dashu_base/
error.rs

1//! Error types.
2
3use core::fmt::{self, Display, Formatter};
4
5/// Number out of bounds.
6#[derive(Clone, Copy, Debug, Eq, PartialEq)]
7pub enum ConversionError {
8    /// The number is not in the representation range
9    OutOfBounds,
10    /// The conversion will cause a loss of precision
11    LossOfPrecision,
12}
13
14impl Display for ConversionError {
15    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
16        match self {
17            ConversionError::OutOfBounds => f.write_str("number out of bounds"),
18            ConversionError::LossOfPrecision => f.write_str("number can't be converted losslessly"),
19        }
20    }
21}
22
23#[cfg(feature = "std")]
24impl std::error::Error for ConversionError {}
25
26/// Error parsing a number.
27#[derive(Clone, Copy, Debug, Eq, PartialEq)]
28pub enum ParseError {
29    /// No digits in the string.
30    NoDigits,
31    /// Invalid digit for a given radix.
32    InvalidDigit,
33    /// The radix is not supported.
34    UnsupportedRadix,
35    /// The radices of different components of the number are different
36    InconsistentRadix,
37    /// The input is structurally malformed, e.g. an unclosed repeating group in a
38    /// decimal literal or multiple `/` separators in a rational.
39    InvalidSyntax,
40}
41
42impl Display for ParseError {
43    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
44        match self {
45            ParseError::NoDigits => f.write_str("no digits"),
46            ParseError::InvalidDigit => f.write_str("invalid digit"),
47            ParseError::UnsupportedRadix => f.write_str("unsupported radix"),
48            ParseError::InconsistentRadix => f.write_str("inconsistent radix"),
49            ParseError::InvalidSyntax => f.write_str("invalid syntax"),
50        }
51    }
52}
53
54#[cfg(feature = "std")]
55impl std::error::Error for ParseError {}