arkley_numerics/
errors.rs

1use std::num::{ParseFloatError,ParseIntError};
2
3/// Represents the possible errors that can occur during parsing of a `StandardForm` number.
4#[derive(Debug)]
5pub enum ParsingStandardFormError {
6    /// Error that occurs while parsing the mantissa as a `ParseFloatError`.
7    Mantissa(ParseFloatError),
8    /// Error that occurs while parsing the exponent as a `ParseIntError`.
9    Exponent(ParseIntError),
10    /// Indicates an invalid format that doesn't match any valid `StandardForm` notation.
11    InvalidFormat,
12}
13
14impl std::fmt::Display for ParsingStandardFormError {
15    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16        match self {
17            ParsingStandardFormError::Mantissa(err) => write!(f, "Error parsing mantissa: {}", err),
18            ParsingStandardFormError::Exponent(err) => write!(f, "Error parsing exponent: {}", err),
19            ParsingStandardFormError::InvalidFormat => write!(f, "Invalid format"),
20        }
21    }
22}
23
24/// Represents the possible parsing errors that can occur when converting a string to a `Number`.
25/// 
26/// The input string is in an invalid format for both `f64` and `StandardForm` parsing.
27///
28/// Contains two specific errors:
29///
30/// - `ParseFloatError`: Represents an error that occurred while parsing the string as `f64`.
31/// - `ParsingStandardFormError`: Represents an error that occurred while parsing the string as `StandardForm`.
32    
33#[derive(Debug)]
34pub struct ParsingNumberError(pub(super) ParseFloatError,pub(super) ParsingStandardFormError);
35
36impl std::fmt::Display for ParsingNumberError {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        let float_err = &self.0;
39        let sf_err = &self.1;
40
41        write!(
42            f,
43            "Invalid input format: Couldn't parse as f64 or StandardForm due to:\n\
44             ------ Float Parsing Error ------\n\
45             {float_err}\n\
46             ------ StandardForm Parsing Error ------\n\
47             {sf_err}",
48        )
49    }
50}