use std::collections::HashMap;
use crate::core::CalcError;
#[derive(Clone, Debug)]
pub struct RateTable {
pub base: String,
pub date: String,
pub rates: HashMap<String, f64>,
pub source: String,
}
pub fn convert(value: f64, from: &str, to: &str, table: &RateTable) -> Result<f64, CalcError> {
if from == to {
return Ok(value);
}
let rate_from = get_rate(from, table)?;
let rate_to = get_rate(to, table)?;
Ok(value / rate_from * rate_to)
}
pub fn get_rate(code: &str, table: &RateTable) -> Result<f64, CalcError> {
if code == table.base {
return Ok(1.0);
}
table
.rates
.get(code)
.copied()
.ok_or_else(|| unknown_currency_error(code, table))
}
pub(crate) fn date_from_unix_days(secs: u64) -> String {
let days = (secs / 86_400) as i64;
let (year, month, day) = civil_from_days(days);
format!("{:04}-{:02}-{:02}", year, month, day)
}
fn civil_from_days(z: i64) -> (i64, u32, u32) {
let z = z + 719_468;
let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
let doe = (z - era * 146_097) as u64; let yoe = (doe - doe / 1_460 + doe / 36_524 - doe / 146_096) / 365; let year = yoe as i64 + era * 400;
let doy = doe - (365 * yoe + yoe / 4 - yoe / 100); let mp = (5 * doy + 2) / 153; let day = (doy - (153 * mp + 2) / 5 + 1) as u32; let month = if mp < 10 { mp + 3 } else { mp - 9 }; let year = if month <= 2 { year + 1 } else { year };
(year, month as u32, day)
}
pub fn unknown_currency_error(code: &str, table: &RateTable) -> CalcError {
CalcError::domain(format!(
"unknown currency: {}, {} currencies supported (base: {})",
code,
table.rates.len() + 1,
table.base
))
.with_i18n(
"msg.fx.unknown_currency",
vec![("code".to_string(), code.to_string())],
)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::ErrorKind;
fn eur_table() -> RateTable {
let mut rates = HashMap::new();
rates.insert("USD".to_string(), 1.08);
rates.insert("CNY".to_string(), 7.85);
rates.insert("GBP".to_string(), 0.85);
rates.insert("JPY".to_string(), 165.0);
RateTable {
base: "EUR".to_string(),
date: "2026-07-25".to_string(),
rates,
source: "frankfurter".to_string(),
}
}
#[test]
fn test_convert_same_currency() {
let table = eur_table();
assert_eq!(convert(100.0, "USD", "USD", &table).unwrap(), 100.0);
}
#[test]
fn test_convert_triangle() {
let table = eur_table();
let r = convert(100.0, "USD", "CNY", &table).unwrap();
assert!((r - 100.0 / 1.08 * 7.85).abs() < 1e-9);
}
#[test]
fn test_convert_with_base_from() {
let table = eur_table();
let r = convert(100.0, "EUR", "USD", &table).unwrap();
assert!((r - 108.0).abs() < 1e-9);
}
#[test]
fn test_convert_with_base_to() {
let table = eur_table();
let r = convert(108.0, "USD", "EUR", &table).unwrap();
assert!((r - 100.0).abs() < 1e-9);
}
#[test]
fn test_convert_unknown_currency() {
let table = eur_table();
let err = convert(100.0, "XYZ", "USD", &table).expect_err("expected error");
assert_eq!(err.kind, ErrorKind::Domain);
assert_eq!(err.i18n_key, Some("msg.fx.unknown_currency"));
}
#[test]
fn test_get_rate_base_currency() {
let table = eur_table();
assert_eq!(get_rate("EUR", &table).unwrap(), 1.0);
}
#[test]
fn test_get_rate_known_currency() {
let table = eur_table();
assert!((get_rate("USD", &table).unwrap() - 1.08).abs() < 1e-9);
}
#[test]
fn test_get_rate_unknown_currency() {
let table = eur_table();
let err = get_rate("XYZ", &table).expect_err("expected error");
assert_eq!(err.kind, ErrorKind::Domain);
assert_eq!(err.i18n_key, Some("msg.fx.unknown_currency"));
}
#[test]
fn test_unknown_currency_error_message_includes_count() {
let table = eur_table();
let err = unknown_currency_error("XYZ", &table);
assert!(err.message.contains("XYZ"));
assert!(err.message.contains("5 currencies"));
assert_eq!(err.i18n_key, Some("msg.fx.unknown_currency"));
}
#[test]
fn test_unknown_currency_error_empty_table() {
let table = RateTable {
base: "EUR".to_string(),
date: "2026-07-25".to_string(),
rates: HashMap::new(),
source: "frankfurter".to_string(),
};
let err = unknown_currency_error("XYZ", &table);
assert!(err.message.contains("1 currencies"));
}
#[test]
fn test_rate_table_clone() {
let table = eur_table();
let cloned = table.clone();
assert_eq!(cloned.base, table.base);
assert_eq!(cloned.date, table.date);
assert_eq!(cloned.rates.len(), table.rates.len());
assert_eq!(cloned.source, table.source);
}
#[test]
fn test_rate_table_debug() {
let table = eur_table();
let s = format!("{:?}", table);
assert!(s.contains("EUR"));
}
#[test]
fn test_date_from_unix_days_epoch() {
assert_eq!(date_from_unix_days(0), "1970-01-01");
}
#[test]
fn test_date_from_unix_days_leap_day() {
assert_eq!(date_from_unix_days(1_709_164_800), "2024-02-29");
}
#[test]
fn test_date_from_unix_days_year_end() {
assert_eq!(date_from_unix_days(1_703_980_800), "2023-12-31");
}
#[test]
fn test_date_from_unix_days_year_start() {
assert_eq!(date_from_unix_days(1_704_067_200), "2024-01-01");
}
#[test]
fn test_date_from_unix_days_known_timestamp() {
assert_eq!(date_from_unix_days(1_761_234_567), "2025-10-23");
}
}