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.
Trait Implementations§
impl Copy for Money
Source§impl<'de> Deserialize<'de> for Money
impl<'de> Deserialize<'de> for Money
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<Money, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<Money, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
impl Eq for Money
Source§impl Serialize for Money
impl Serialize for Money
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
impl StructuralPartialEq for Money
Auto Trait Implementations§
impl Freeze for Money
impl RefUnwindSafe for Money
impl Send for Money
impl Sync for Money
impl Unpin for Money
impl UnsafeUnpin for Money
impl UnwindSafe for Money
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.