Crate beancount_parser

source ·
Expand description

A parsing library for the beancount language

§Usage

Use parse to get an instance of BeancountFile.

This is generic over the decimal type. The examples use f64 as a decimal type. You may also use Decimal from the rust_decimal crate.

use beancount_parser::{BeancountFile, DirectiveContent};

let input = r#"
2023-05-20 * "Coffee beans"
  Expenses:Groceries   10 CHF
  Assets:Checking
"#;

// Parse into the `BeancountFile` struct:
let beancount: BeancountFile<f64> = input.parse()?;

let directive = &beancount.directives[0];
assert_eq!(directive.date.year, 2023);
assert_eq!(directive.date.month, 5);
assert_eq!(directive.date.day, 20);

let DirectiveContent::Transaction(trx) = &directive.content else {
    panic!("was not a transaction")
};
assert_eq!(trx.narration.as_deref(), Some("Coffee beans"));
assert_eq!(trx.postings[0].account.as_str(), "Expenses:Groceries");
assert_eq!(trx.postings[0].amount.as_ref().unwrap().value, 10.0);
assert_eq!(trx.postings[0].amount.as_ref().unwrap().currency.as_str(), "CHF");
assert_eq!(trx.postings[1].account.as_str(), "Assets:Checking");
assert_eq!(trx.postings[1].amount, None);

Modules§

Structs§

Enums§

Traits§

  • Decimal type to which amount values and expressions will be parsed into.

Functions§

  • Parse the input beancount file and return an instance of BeancountFile on success
  • Parse the beancount file and return an iterator over Result<Entry<D>, Result>
  • Read the files from disk and parse their content.