pub struct Money { /* private fields */ }Expand description
Value Object: Money
Represents a monetary amount with currency in the paywall system. Amounts are stored as the smallest unit (e.g., cents for USD, lamports for SOL).
Domain Rules:
- Amount must be non-negative
- Currency must be specified
- Immutable once created
- Operations preserve currency (cannot add USD to SOL)
This is a Value Object:
- Defined by its value, not identity
- Immutable
- Self-validating
- Compared by value equality
Implementations§
Source§impl Money
impl Money
Sourcepub fn new(amount: u64, currency: Currency) -> Self
pub fn new(amount: u64, currency: Currency) -> Self
Create a new Money value from the smallest unit
§Examples
use allsource_core::domain::value_objects::{Money, Currency};
// 50 cents
let money = Money::new(50, Currency::USD);
assert_eq!(money.amount(), 50);
assert_eq!(money.currency(), Currency::USD);Sourcepub fn from_decimal(decimal: f64, currency: Currency) -> Self
pub fn from_decimal(decimal: f64, currency: Currency) -> Self
Create a Money value from a decimal amount
§Examples
use allsource_core::domain::value_objects::{Money, Currency};
// $0.50
let money = Money::from_decimal(0.50, Currency::USD);
assert_eq!(money.amount(), 50);Sourcepub fn usdc_decimal(amount: f64) -> Self
pub fn usdc_decimal(amount: f64) -> Self
Create USDC Money from decimal
Sourcepub fn as_decimal(&self) -> f64
pub fn as_decimal(&self) -> f64
Get the amount as a decimal
Sourcepub fn is_positive(&self) -> bool
pub fn is_positive(&self) -> bool
Check if this amount is positive (non-zero)
Sourcepub fn at_least(&self, minimum: &Money) -> Result<()>
pub fn at_least(&self, minimum: &Money) -> Result<()>
Check if this amount is at least a minimum value
Sourcepub fn percentage(&self, percent: u64) -> Self
pub fn percentage(&self, percent: u64) -> Self
Calculate a percentage of this amount
§Examples
use allsource_core::domain::value_objects::{Money, Currency};
let money = Money::usd_cents(1000); // $10.00
let fee = money.percentage(7); // 7% = 70 cents
assert_eq!(fee.amount(), 70);Sourcepub fn subtract_percentage(&self, percent: u64) -> Self
pub fn subtract_percentage(&self, percent: u64) -> Self
Subtract a percentage and return the remainder
§Examples
use allsource_core::domain::value_objects::{Money, Currency};
let money = Money::usd_cents(1000); // $10.00
let after_fee = money.subtract_percentage(7); // 7% fee = $9.30
assert_eq!(after_fee.amount(), 930);Trait Implementations§
Source§impl<'de> Deserialize<'de> for Money
impl<'de> Deserialize<'de> for Money
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
impl Copy for Money
impl Eq for Money
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 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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.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> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
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.