Crate ledger_rs_lib

source ·
Expand description

Ledger-rs library

Ledger-cli functionality implemented in Rust

Early work-in-progress.

The basic functionality demo:

Given a basic.ledger text file, with the contents

2023-04-21 Supermarket
    Expenses:Food  20 EUR
    Assets:Cash

you can use the library to parse the transactions from the file and provide a basic report on account balances

    let actual = ledger_rs_lib::run_command("b -f tests/basic.ledger");

    assert!(!actual.is_empty());
    assert_eq!(5, actual.len());
    assert_eq!("Account  has balance 0 EUR", actual[0]);
    assert_eq!("Account Assets has balance -20 EUR", actual[1]);
    assert_eq!("Account Assets:Cash has balance -20 EUR", actual[2]);
    assert_eq!("Account Expenses has balance 20 EUR", actual[3]);
    assert_eq!("Account Expenses:Food has balance 20 EUR", actual[4]);

Modules

  • Account definition and operations
  • Amount and the Decimal numeric type
  • Commodity definition
  • Commodity price history
  • Journal The main model object. The journal files are parsed into the Journal structure. Provides methods for fetching and iterating over the contained elements (transactions, posts, accounts…).
  • Parser with iterators
  • Commodity Pool
  • Posting
  • Reports module containing the report definitions
  • Scanner scans the input text and returns tokens (groups of characters) back for parsing. Scans/tokenizes the journal files. There are scanner functions for every element of the journal.
  • Handy utility functions
  • Transaction module

Functions

  • Parse input and return the model structure.
  • Parses text containing Ledger-style journal. text: &str A Ledger-style journal. The same content that is normally stored in text files journal: &mut Journal The result are stored in the given Journal instance.
  • An entry point for the CLIs. The commands and arguments sent to the CLI are processed here. This is so that 3rd-party clients can pass argv and get the same result. The arguments should be compatible with Ledger, so that the functionality is comparable.
  • A convenient entry point if you want to use a command string directly. command: &str A Ledger-style command, i.e. “balance -f journal.ledger”