Crate beancount_parser_2
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_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<f64> = beancount_parser_2::parse::<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.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
- Types to represent beancount metadata
Structs
- Account
- Amount
- Balance assertion
- An beancount option
- Main struct representing a parsed beancount file.
- Close account directive
- Error that may be returned by the various
TryFrom
/TryInto
implementation to signify that the value cannot be converted to the desired type - Cost of a posting
- Currency
- A date
- A beancount “directive”
- Error returned in case of invalid beancount syntax found
- An event
- Transaction link
- Open account directive
- Pad directive
- A transaction posting
- Price directive
- Transaction tag
- A transaction
Enums
- Directive specific content
- Price of a posting
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