use rust_decimal::Decimal;
use crate::errors::ValidationError;
use crate::traits::ValueObject;
use super::currency_code::CurrencyCode;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ExchangeRateInput {
pub from: CurrencyCode,
pub to: CurrencyCode,
pub rate: Decimal,
}
pub type ExchangeRateOutput = String;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ExchangeRate {
from: CurrencyCode,
to: CurrencyCode,
rate: Decimal,
#[cfg_attr(feature = "serde", serde(skip))]
canonical: String,
}
impl ValueObject for ExchangeRate {
type Input = ExchangeRateInput;
type Output = ExchangeRateOutput;
type Error = ValidationError;
fn new(value: Self::Input) -> Result<Self, Self::Error> {
if value.from == value.to {
return Err(ValidationError::invalid(
"ExchangeRate",
&format!("{}/{}", value.from, value.to),
));
}
if value.rate <= Decimal::ZERO {
return Err(ValidationError::invalid(
"ExchangeRate",
&value.rate.to_string(),
));
}
let canonical = format!("{}/{} {}", value.from, value.to, value.rate);
Ok(Self {
from: value.from,
to: value.to,
rate: value.rate,
canonical,
})
}
fn value(&self) -> &Self::Output {
&self.canonical
}
fn into_inner(self) -> Self::Input {
ExchangeRateInput {
from: self.from,
to: self.to,
rate: self.rate,
}
}
}
impl ExchangeRate {
pub fn from(&self) -> &CurrencyCode {
&self.from
}
pub fn to(&self) -> &CurrencyCode {
&self.to
}
pub fn rate(&self) -> &Decimal {
&self.rate
}
}
impl std::fmt::Display for ExchangeRate {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.canonical)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::traits::ValueObject;
fn eur() -> CurrencyCode {
CurrencyCode::new("EUR".into()).unwrap()
}
fn usd() -> CurrencyCode {
CurrencyCode::new("USD".into()).unwrap()
}
#[test]
fn constructs_valid_rate() {
let r = ExchangeRate::new(ExchangeRateInput {
from: eur(),
to: usd(),
rate: "1.0850".parse().unwrap(),
})
.unwrap();
assert_eq!(r.value(), "EUR/USD 1.0850");
}
#[test]
fn from_accessor() {
let r = ExchangeRate::new(ExchangeRateInput {
from: eur(),
to: usd(),
rate: "1.0850".parse().unwrap(),
})
.unwrap();
assert_eq!(r.from().value(), "EUR");
}
#[test]
fn to_accessor() {
let r = ExchangeRate::new(ExchangeRateInput {
from: eur(),
to: usd(),
rate: "1.0850".parse().unwrap(),
})
.unwrap();
assert_eq!(r.to().value(), "USD");
}
#[test]
fn rate_accessor() {
let rate_val: Decimal = "1.0850".parse().unwrap();
let r = ExchangeRate::new(ExchangeRateInput {
from: eur(),
to: usd(),
rate: rate_val,
})
.unwrap();
assert_eq!(*r.rate(), "1.0850".parse::<Decimal>().unwrap());
}
#[test]
fn rejects_zero_rate() {
assert!(
ExchangeRate::new(ExchangeRateInput {
from: eur(),
to: usd(),
rate: Decimal::ZERO,
})
.is_err()
);
}
#[test]
fn rejects_negative_rate() {
assert!(
ExchangeRate::new(ExchangeRateInput {
from: eur(),
to: usd(),
rate: "-1".parse().unwrap(),
})
.is_err()
);
}
#[test]
fn rejects_same_currency() {
assert!(
ExchangeRate::new(ExchangeRateInput {
from: eur(),
to: eur(),
rate: "1".parse().unwrap(),
})
.is_err()
);
}
#[test]
fn display_matches_value() {
let r = ExchangeRate::new(ExchangeRateInput {
from: eur(),
to: usd(),
rate: "1.0850".parse().unwrap(),
})
.unwrap();
assert_eq!(r.to_string(), r.value().to_owned());
}
}