| 1 | //! Error types. |
| 2 | |
| 3 | use core::fmt::{self, Display, Formatter}; |
| 4 | |
| 5 | /// Number out of bounds. |
| 6 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 7 | pub 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 | |
| 14 | impl 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")] |
| 24 | impl std::error::Error for ConversionError {} |
| 25 | |
| 26 | /// Error parsing a number. |
| 27 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 28 | pub 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 | } |
| 38 | |
| 39 | impl Display for ParseError { |
| 40 | fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
| 41 | match self { |
| 42 | ParseError::NoDigits => f.write_str("no digits"), |
| 43 | ParseError::InvalidDigit => f.write_str("invalid digit"), |
| 44 | ParseError::UnsupportedRadix => f.write_str("unsupported radix"), |
| 45 | ParseError::InconsistentRadix => f.write_str("inconsistent radix"), |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | #[cfg(feature = "std")] |
| 51 | impl std::error::Error for ParseError {} |