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§

metadata
Types to represent beancount metadata

Structs§

Account
Account
Amount
Amount
Balance
Balance assertion
BeanOption
An beancount option
BeancountFile
Main struct representing a parsed beancount file.
Close
Close account directive
ConversionError
Error that may be returned by the various TryFrom/TryInto implementation to signify that the value cannot be converted to the desired type
Cost
Cost of a posting
Currency
Currency
Date
A date
Directive
A beancount “directive”
Error
Error returned in case of invalid beancount syntax found
Event
An event
Link
Transaction link
Open
Open account directive
Pad
Pad directive
Posting
A transaction posting
Price
Price directive
Tag
Transaction tag
Transaction
A transaction

Enums§

DirectiveContent
Directive specific content
Entry
Entry in the beancount syntax
PostingPrice
Price of a posting
ReadFileError
Error returned when reading a beancount file from disk

Traits§

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

Functions§

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