Skip to main content

break_eternity/
error.rs

1//! Error types for the crate.
2
3use std::num::ParseFloatError;
4
5/// Identifies the category of an arithmetic error.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7pub enum ArithmeticErrorKind {
8    /// Operation is mathematically undefined (e.g., 0/0, +inf + -inf).
9    Undefined,
10    /// Division by zero.
11    DivisionByZero,
12    /// Negative base with non-integer exponent.
13    NegativeBase,
14    /// Domain error (e.g., ln of non-positive, lambertw for z < -1/e).
15    OutOfDomain,
16    /// Numerical iteration diverged.
17    IterationDiverged,
18}
19
20/// An error produced by a `checked_*` arithmetic operation on [`crate::Decimal`].
21#[derive(Debug, thiserror::Error)]
22#[error("arithmetic error in {op}: {kind:?}")]
23pub struct ArithmeticError {
24    /// The kind of arithmetic error.
25    pub kind: ArithmeticErrorKind,
26    /// The name of the operation that failed.
27    pub op: &'static str,
28}
29
30/// Error type for all errors in this crate.
31#[derive(Debug, thiserror::Error)]
32pub enum BreakEternityError {
33    /// An error that occurs when `f_gamma` and `lambertw` fails to converge a number (more than 100 iterations).
34    #[error("Iteration failed to converge: {z}")]
35    IterationFailedConverging {
36        /// The number that failed to converge.
37        z: f64,
38    },
39
40    /// An error that occurs when a String cannot be parsed to a Decimal.
41    #[error("Error while parsing \"{parsed}\": {error}")]
42    ParseError {
43        /// The string that failed to parse.
44        parsed: String,
45        /// The error that occurred.
46        error: ParseFloatError,
47    },
48
49    /// An error that occurs when lambertw is called with a number less than -1.
50    #[error("lambertw is undefined for results < -1")]
51    LambertWError,
52
53    /// An arithmetic error from a `checked_*` operation.
54    #[error(transparent)]
55    Arithmetic(#[from] ArithmeticError),
56}