use crate::{CurrencyCode, MinorAmount, PaymentError};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Money {
amount: MinorAmount,
currency: CurrencyCode,
}
impl Money {
pub fn new_minor(amount: i64, currency: impl AsRef<str>) -> Result<Self, PaymentError> {
Ok(Self {
amount: MinorAmount::new(amount)?,
currency: CurrencyCode::new(currency)?,
})
}
#[inline]
#[must_use]
pub const fn amount(&self) -> MinorAmount {
self.amount
}
#[inline]
#[must_use]
pub const fn currency(&self) -> &CurrencyCode {
&self.currency
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_minor_builds_money() {
let money = Money::new_minor(1_000, "usd").expect("money should be valid");
assert_eq!(money.amount().value(), 1_000);
assert_eq!(money.currency().as_str(), "USD");
}
}