pub struct Money { /* private fields */ }Expand description
A monetary amount paired with its currency.
This type ensures that amounts always carry their currency context, preventing accidental mixing of currencies in arithmetic operations.
§Example
use stateset_primitives::{Money, CurrencyCode};
use rust_decimal_macros::dec;
let price = Money::new(dec!(29.99), CurrencyCode::USD);
assert_eq!(price.amount(), dec!(29.99));
assert_eq!(price.currency(), CurrencyCode::USD);
assert_eq!(format!("{}", price), "29.99 USD");Implementations§
Source§impl Money
impl Money
Sourcepub const fn new(amount: Decimal, currency: CurrencyCode) -> Money
pub const fn new(amount: Decimal, currency: CurrencyCode) -> Money
Create a new monetary value.
Sourcepub const fn zero(currency: CurrencyCode) -> Money
pub const fn zero(currency: CurrencyCode) -> Money
Create a zero amount in the given currency.
Sourcepub const fn currency(&self) -> CurrencyCode
pub const fn currency(&self) -> CurrencyCode
Get the currency code.
Sourcepub const fn is_positive(&self) -> bool
pub const fn is_positive(&self) -> bool
Returns true if the amount is positive.
Sourcepub const fn is_negative(&self) -> bool
pub const fn is_negative(&self) -> bool
Returns true if the amount is negative.
Sourcepub fn checked_add(self, other: Money) -> Option<Money>
pub fn checked_add(self, other: Money) -> Option<Money>
Add two monetary values.
Returns None if the currencies don’t match or if the underlying
Decimal addition overflows. This method never panics.
Sourcepub fn checked_sub(self, other: Money) -> Option<Money>
pub fn checked_sub(self, other: Money) -> Option<Money>
Subtract two monetary values.
Returns None if the currencies don’t match or if the underlying
Decimal subtraction overflows. This method never panics.
Sourcepub fn checked_mul_scalar(self, factor: Decimal) -> Option<Money>
pub fn checked_mul_scalar(self, factor: Decimal) -> Option<Money>
Multiply by a scalar factor (e.g., quantity or tax rate). Currency is preserved.
Returns None if the underlying Decimal multiplication overflows.
This method never panics.