Skip to main content

decimal_money/
error.rs

1/// Errors that can occur in money operations.
2#[derive(Debug, thiserror::Error)]
3pub enum MoneyError {
4    /// Attempted to operate on amounts with different currencies.
5    #[error("Currency mismatch: cannot operate on {left} and {right}")]
6    CurrencyMismatch {
7        /// The left operand's currency code.
8        left: String,
9        /// The right operand's currency code.
10        right: String,
11    },
12
13    /// Arithmetic overflow occurred.
14    #[error("Overflow during operation")]
15    Overflow,
16
17    /// An invalid amount was provided.
18    #[error("Invalid amount: {0}")]
19    InvalidAmount(String),
20
21    /// Rounding error occurred.
22    #[error("Rounding error: {0}")]
23    Rounding(String),
24
25    /// Serialization or deserialization error.
26    #[error("Serialization error: {0}")]
27    Serialization(String),
28}
29
30/// A convenience result type for money operations.
31pub type Result<T> = std::result::Result<T, MoneyError>;
32
33impl From<rust_decimal::Error> for MoneyError {
34    fn from(e: rust_decimal::Error) -> Self {
35        MoneyError::InvalidAmount(e.to_string())
36    }
37}