From 3cc0d1bf2a6f84a2b192d695da30b2786eb31dd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cbronwynbarnett=E2=80=9D?= <“bronwyn.barnett@ovo.com> Date: Tue, 17 Mar 2026 16:34:32 +0000 Subject: [PATCH] Add currency_symbol and conversion_rate config to cost module MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allows users to configure a custom currency symbol (e.g. £ instead of $) and an optional conversion rate from USD to their local currency. Config example: [cship.cost] currency_symbol = "£" conversion_rate = 0.79 Co-Authored-By: Claude Opus 4.6 (1M context) --- src/config.rs | 4 ++++ src/modules/cost.rs | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/config.rs b/src/config.rs index 3273077..ea53c33 100644 --- a/src/config.rs +++ b/src/config.rs @@ -47,6 +47,10 @@ pub struct CostConfig { pub critical_threshold: Option, pub critical_style: Option, pub format: Option, + /// Currency symbol to display instead of `$`. Defaults to `$`. + pub currency_symbol: Option, + /// Conversion rate from USD to the target currency. Defaults to 1.0 (no conversion). + pub conversion_rate: Option, // Sub-field per-display configs (map to [cship.cost.total_cost_usd] etc.) pub total_cost_usd: Option, pub total_duration_ms: Option, diff --git a/src/modules/cost.rs b/src/modules/cost.rs index f44851c..22950fd 100644 --- a/src/modules/cost.rs +++ b/src/modules/cost.rs @@ -29,7 +29,10 @@ pub fn render(ctx: &Context, cfg: &CshipConfig) -> Option { let symbol = cost_cfg.and_then(|c| c.symbol.as_deref()); let style = cost_cfg.and_then(|c| c.style.as_deref()); - let formatted = format!("${:.2}", val); + let currency_symbol = cost_cfg.and_then(|c| c.currency_symbol.as_deref()).unwrap_or("$"); + let conversion_rate = cost_cfg.and_then(|c| c.conversion_rate).unwrap_or(1.0); + let converted_val = val * conversion_rate; + let formatted = format!("{}{:.2}", currency_symbol, converted_val); // Extract threshold variables FIRST (before format check) let warn_threshold = cost_cfg.and_then(|c| c.warn_threshold); @@ -323,6 +326,35 @@ mod tests { assert_eq!(result, Some("$0.01".to_string())); } + #[test] + fn test_cost_renders_custom_currency_symbol() { + let ctx = ctx_with_cost(1.50); + let cfg = CshipConfig { + cost: Some(CostConfig { + currency_symbol: Some("£".to_string()), + ..Default::default() + }), + ..Default::default() + }; + let result = render(&ctx, &cfg); + assert_eq!(result, Some("£1.50".to_string())); + } + + #[test] + fn test_cost_renders_with_conversion_rate() { + let ctx = ctx_with_cost(1.00); + let cfg = CshipConfig { + cost: Some(CostConfig { + currency_symbol: Some("£".to_string()), + conversion_rate: Some(0.79), + ..Default::default() + }), + ..Default::default() + }; + let result = render(&ctx, &cfg); + assert_eq!(result, Some("£0.79".to_string())); + } + #[test] fn test_cost_disabled_returns_none() { let ctx = ctx_with_cost(5.0);