Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ pub struct CostConfig {
pub critical_threshold: Option<f64>,
pub critical_style: Option<String>,
pub format: Option<String>,
/// Currency symbol to display instead of `$`. Defaults to `$`.
pub currency_symbol: Option<String>,
/// Conversion rate from USD to the target currency. Defaults to 1.0 (no conversion).
pub conversion_rate: Option<f64>,
// Sub-field per-display configs (map to [cship.cost.total_cost_usd] etc.)
pub total_cost_usd: Option<CostSubfieldConfig>,
pub total_duration_ms: Option<CostSubfieldConfig>,
Expand Down
34 changes: 33 additions & 1 deletion src/modules/cost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ pub fn render(ctx: &Context, cfg: &CshipConfig) -> Option<String> {

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);
Expand Down Expand Up @@ -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);
Expand Down
Loading