Skip to main content

rustledger_core/format/
helpers.rs

1//! Shared helper functions for formatting.
2
3use super::format_amount;
4use crate::MetaValue;
5
6/// Format a metadata value.
7pub fn format_meta_value(value: &MetaValue) -> String {
8    match value {
9        MetaValue::String(s) => format!("\"{}\"", escape_string(s)),
10        MetaValue::Account(a) => a.clone(),
11        MetaValue::Currency(c) => c.clone(),
12        MetaValue::Tag(t) => format!("#{t}"),
13        MetaValue::Link(l) => format!("^{l}"),
14        MetaValue::Date(d) => d.to_string(),
15        MetaValue::Number(n) => n.to_string(),
16        MetaValue::Amount(a) => format_amount(a),
17        MetaValue::Bool(b) => if *b { "TRUE" } else { "FALSE" }.to_string(),
18        MetaValue::None => String::new(),
19    }
20}
21
22/// Escape a string for output (handle quotes and backslashes).
23pub fn escape_string(s: &str) -> String {
24    let mut out = String::with_capacity(s.len());
25    for c in s.chars() {
26        match c {
27            '"' => out.push_str("\\\""),
28            '\\' => out.push_str("\\\\"),
29            '\n' => out.push_str("\\n"),
30            _ => out.push(c),
31        }
32    }
33    out
34}