Skip to main content

write_ledger

Function write_ledger 

Source
pub fn write_ledger<W>(
    entries: impl IntoIterator<Item = Transaction>,
    writer: &mut W,
) -> Result<()>
where W: Write,
👎Deprecated since 2.3.0:

use LedgerFrontend.write_journal(hir, writer) instead; write_ledger only handles transactions (no prices or assertions) and will be removed in v3.0

Expand description

Write a sequence of resolution::Transaction values to writer in canonical Ledger source text format.

Each transaction is formatted using its std::fmt::Display impl and separated from the next by a blank line. The output is suitable for appending to or creating a .ledger source file and round-trips correctly through the parser: write_ledger(txns) -> parse -> resolve should yield semantically equivalent transactions.

§Errors

Returns an std::io::Error if any write to writer fails.

§Example

let txns = vec![
    Transaction::new(NaiveDate::from_ymd_opt(2024, 1, 15).unwrap(), "Groceries")
        .with_posting(Posting::new("Expenses:Food").with_amount((
            rust_decimal::Decimal::from(50u32), "$",
        )))
        .with_posting(Posting::new("Assets:Checking")),
];
let mut out = Vec::new();
#[allow(deprecated)]
doppio::write_ledger(txns, &mut out).unwrap();
let text = String::from_utf8(out).unwrap();
assert!(text.starts_with("2024-01-15 Groceries"));