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_2::{BeancountFile, DirectiveContent};

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

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

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, Some("Coffee beans"));
assert_eq!(trx.postings[0].account.as_str(), "Expenses:Groceries");
assert_eq!(trx.postings[0].amount.unwrap().value, 10.0);
assert_eq!(trx.postings[0].amount.unwrap().currency.as_str(), "CHF");
assert_eq!(trx.postings[1].account.as_str(), "Assets:Checking");
assert_eq!(trx.postings[1].amount, None);

Structs

Enums

Traits

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

Functions