mod fmt;
use crate::fmt::{CODE_FORMAT, CODE_FORMAT_MINOR, SYMBOL_FORMAT, SYMBOL_FORMAT_MINOR};
use fmt::format_obj_money;
use crate::macros::dec;
use crate::{Decimal, MoneyError};
use rust_decimal::MathematicalOps;
use rust_decimal::prelude::ToPrimitive;
pub trait ObjMoney {
fn amount(&self) -> Decimal;
fn code(&self) -> &str;
fn symbol(&self) -> &str;
fn name(&self) -> &str;
fn minor_unit(&self) -> u16;
fn thousand_separator(&self) -> &str;
fn decimal_separator(&self) -> &str;
fn minor_unit_symbol(&self) -> &str;
#[inline]
fn minor_amount(&self) -> Result<i128, MoneyError> {
self.amount()
.checked_mul(
dec!(10)
.checked_powu(self.minor_unit().into())
.ok_or(MoneyError::OverflowError)?,
)
.ok_or(MoneyError::OverflowError)?
.to_i128()
.ok_or(MoneyError::OverflowError)
}
#[inline]
fn is_zero(&self) -> bool {
self.amount().is_zero()
}
#[inline]
fn is_positive(&self) -> bool {
self.amount().is_sign_positive()
}
#[inline]
fn is_negative(&self) -> bool {
self.amount().is_sign_negative()
}
#[inline]
fn scale(&self) -> u32 {
self.amount().scale()
}
#[inline]
fn fraction(&self) -> Decimal {
self.amount().fract()
}
#[inline]
fn mantissa(&self) -> i128 {
self.amount().mantissa()
}
fn format_code(&self) -> String {
format_obj_money(
self.amount(),
self.code(),
self.symbol(),
self.minor_unit_symbol(),
self.minor_unit(),
self.thousand_separator(),
self.decimal_separator(),
CODE_FORMAT,
)
}
fn format_symbol(&self) -> String {
format_obj_money(
self.amount(),
self.code(),
self.symbol(),
self.minor_unit_symbol(),
self.minor_unit(),
self.thousand_separator(),
self.decimal_separator(),
SYMBOL_FORMAT,
)
}
fn format_code_minor(&self) -> String {
format_obj_money(
self.amount(),
self.code(),
self.symbol(),
self.minor_unit_symbol(),
self.minor_unit(),
self.thousand_separator(),
self.decimal_separator(),
CODE_FORMAT_MINOR,
)
}
fn format_symbol_minor(&self) -> String {
format_obj_money(
self.amount(),
self.code(),
self.symbol(),
self.minor_unit_symbol(),
self.minor_unit(),
self.thousand_separator(),
self.decimal_separator(),
SYMBOL_FORMAT_MINOR,
)
}
}
mod money_impl;
#[cfg(feature = "raw_money")]
mod raw_money_impl;
#[cfg(test)]
mod obj_money_test;