mod euro;
pub use euro::*;
pub trait CurrencyImpl
{
fn value(&self) -> i32;
fn currency(&self) -> &str;
fn pad(
&self,
pad: usize,
) -> String;
fn idn(
&self
) -> (
u32,
u32,
bool,
);
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Currency<'r>(
pub i32,
pub &'r str,
);
impl<'r> std::fmt::Display for Currency<'r>
{
fn fmt(
&self,
f: &mut std::fmt::Formatter<'_>,
) -> std::fmt::Result
{
let amount = self.0;
let currency = self.1;
let integral = (amount / 100).unsigned_abs();
let decimal = amount.unsigned_abs() - (100 * integral);
let negative = if amount < 0 { "-" } else { "" };
f.write_str(&format!("{negative}{integral}.{decimal:02}{currency}"))
}
}
impl<'r> CurrencyImpl for Currency<'r>
{
fn pad(
&self,
pad: usize,
) -> String
{
let amount = self.0;
let currency = self.1;
let integral = (amount / 100).unsigned_abs();
let decimal = amount.unsigned_abs() - (100 * integral);
let negative = if amount < 0 { "-" } else { " " };
format!(
"{negative}{integral: >pad$}.{decimal:02}{currency}",
pad = pad
)
}
fn idn(
&self
) -> (
u32,
u32,
bool,
)
{
let amount = self.0;
let integral = (amount / 100).unsigned_abs();
let decimal = amount.unsigned_abs() - (100 * integral);
let negative = amount < 0;
(
integral, decimal, negative,
)
}
fn value(&self) -> i32
{
self.0
}
fn currency(&self) -> &str
{
self.1
}
}