Skip to main content

atomr_money/
error.rs

1//! Errors raised by money arithmetic and construction.
2
3use thiserror::Error;
4
5/// Failures from [`Money`](crate::Money) operations. Arithmetic is always
6/// checked: currency mismatch and overflow are errors, never panics or silent
7/// truncation.
8#[derive(Debug, Clone, PartialEq, Eq, Error)]
9#[non_exhaustive]
10pub enum MoneyError {
11    /// Two operands carried different currencies.
12    #[error("currency mismatch: {lhs} vs {rhs}")]
13    CurrencyMismatch { lhs: String, rhs: String },
14
15    /// The result did not fit in the backing decimal.
16    #[error("decimal overflow")]
17    Overflow,
18
19    /// An ISO 4217 code was not a 3-letter ASCII alphabetic string.
20    #[error("invalid currency code: {0:?}")]
21    BadCurrencyCode(String),
22
23    /// A string amount could not be parsed as a decimal.
24    #[error("invalid decimal amount: {0}")]
25    BadAmount(String),
26}