Skip to main content

precision_core/
error.rs

1//! Error types for decimal operations.
2
3use core::fmt;
4use serde::{Deserialize, Serialize};
5
6/// Error returned when an arithmetic operation fails.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
8pub enum ArithmeticError {
9    /// Result exceeds maximum representable value.
10    Overflow,
11    /// Result is smaller than minimum representable value.
12    Underflow,
13    /// Division by zero attempted.
14    DivisionByZero,
15    /// Scale exceeds maximum precision.
16    ScaleExceeded,
17    /// Square root of negative number attempted.
18    NegativeSqrt,
19    /// Logarithm of zero attempted.
20    LogOfZero,
21    /// Logarithm of negative number attempted.
22    LogOfNegative,
23}
24
25impl fmt::Display for ArithmeticError {
26    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            Self::Overflow => write!(f, "arithmetic overflow"),
29            Self::Underflow => write!(f, "arithmetic underflow"),
30            Self::DivisionByZero => write!(f, "division by zero"),
31            Self::ScaleExceeded => write!(f, "scale exceeds maximum precision"),
32            Self::NegativeSqrt => write!(f, "square root of negative number"),
33            Self::LogOfZero => write!(f, "logarithm of zero"),
34            Self::LogOfNegative => write!(f, "logarithm of negative number"),
35        }
36    }
37}
38
39/// Error returned when parsing a decimal from a string fails.
40#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
41pub enum ParseError {
42    /// Input string is empty.
43    Empty,
44    /// Invalid character in input.
45    InvalidCharacter,
46    /// Multiple decimal points in input.
47    MultipleDecimalPoints,
48    /// Value exceeds representable range.
49    OutOfRange,
50}
51
52impl fmt::Display for ParseError {
53    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54        match self {
55            Self::Empty => write!(f, "empty string"),
56            Self::InvalidCharacter => write!(f, "invalid character"),
57            Self::MultipleDecimalPoints => write!(f, "multiple decimal points"),
58            Self::OutOfRange => write!(f, "value out of range"),
59        }
60    }
61}