Module display_context

Module display_context 

Source
Expand description

Display context for formatting numbers with consistent precision.

This module provides the DisplayContext type which tracks the precision (number of decimal places) seen for each currency during parsing. This allows numbers to be formatted consistently - for example, if a file contains both 100 USD and 50.25 USD, both should display with 2 decimal places.

This matches Python beancount’s display_context behavior.

§Example

use rustledger_core::DisplayContext;
use rust_decimal_macros::dec;

let mut ctx = DisplayContext::new();

// Track precision from parsed numbers
ctx.update(dec!(100), "USD");      // 0 decimal places
ctx.update(dec!(50.25), "USD");    // 2 decimal places
ctx.update(dec!(1.5), "EUR");      // 1 decimal place

// Get the precision to use (maximum seen)
assert_eq!(ctx.get_precision("USD"), Some(2));
assert_eq!(ctx.get_precision("EUR"), Some(1));
assert_eq!(ctx.get_precision("GBP"), None);  // Never seen

// Format a number with the tracked precision
assert_eq!(ctx.format(dec!(100), "USD"), "100.00");
assert_eq!(ctx.format(dec!(50.25), "USD"), "50.25");
assert_eq!(ctx.format(dec!(1.5), "EUR"), "1.5");

Structs§

DisplayContext
Display context for formatting numbers with consistent precision per currency.