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}
18
19impl fmt::Display for ArithmeticError {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        match self {
22            Self::Overflow => write!(f, "arithmetic overflow"),
23            Self::Underflow => write!(f, "arithmetic underflow"),
24            Self::DivisionByZero => write!(f, "division by zero"),
25            Self::ScaleExceeded => write!(f, "scale exceeds maximum precision"),
26        }
27    }
28}
29
30/// Error returned when parsing a decimal from a string fails.
31#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
32pub enum ParseError {
33    /// Input string is empty.
34    Empty,
35    /// Invalid character in input.
36    InvalidCharacter,
37    /// Multiple decimal points in input.
38    MultipleDecimalPoints,
39    /// Value exceeds representable range.
40    OutOfRange,
41}
42
43impl fmt::Display for ParseError {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        match self {
46            Self::Empty => write!(f, "empty string"),
47            Self::InvalidCharacter => write!(f, "invalid character"),
48            Self::MultipleDecimalPoints => write!(f, "multiple decimal points"),
49            Self::OutOfRange => write!(f, "value out of range"),
50        }
51    }
52}