#ledger-cli #parser #ledger #file-format #cli-input #cli-parser #line-comment

ledger-parser

Rust library for parsing ledger cli (https://www.ledger-cli.org/) input files

14 stable releases (6 major)

7.0.0 Jun 6, 2024
6.0.0 Mar 5, 2024
5.1.1 Apr 21, 2022
5.1.0 Jan 24, 2022
1.0.1 Oct 22, 2018

#257 in Parser implementations

Download history 78/week @ 2024-03-08 11/week @ 2024-03-15 28/week @ 2024-03-29 8/week @ 2024-04-05 95/week @ 2024-05-31 47/week @ 2024-06-07 3/week @ 2024-06-14 1/week @ 2024-06-21

146 downloads per month
Used in ledger-utils

Unlicense

100KB
2.5K SLoC

ledger-parser

Crates.io Version Docs.rs Version License Unlicense

Rust library for parsing Ledger-cli input files.

File format

Only a subset of the ledger-cli's file format is implemented.

Supported elements:

  • Line comments (starting with: ; # % | *)

  • Inline comments (starting with ;)

  • Transaction headers with format (minimum two spaces or one tab between DESC and NOTE):

    DATE[=EDATE] [*|!] [(CODE)] DESC  [; NOTE]
    
  • Transaction postings with format (minimum two spaces or one tab between ACCOUNT and AMOUNT):

      ACCOUNT  [AMOUNT] [= BALANCE] [; NOTE]
    
    • Virtual accounts are supported

    • Posting comments with dates, effective dates, and metadata tags are supported

      ACCOUNT  [AMOUNT] [= BALANCE]  ; [YYYY-MM-DD]
      ACCOUNT  [AMOUNT] [= BALANCE]  ; [=YYYY-MM-DD]
      ACCOUNT  [AMOUNT] [= BALANCE]  ; :TAG1:TAG2:
      ACCOUNT  [AMOUNT] [= BALANCE]  ; Tag: String Value
      ACCOUNT  [AMOUNT] [= BALANCE]  ; Tag:: -1
      ACCOUNT  [AMOUNT] [= BALANCE]  ; Tag:: 1.0
      ACCOUNT  [AMOUNT] [= BALANCE]  ; Tag:: [YYYY-MM-DD]
    
  • AMOUNT can be combined with lot and commodity prices ({}, {{}}, @, @@)

  • Commodity prices with format:

    P DATE SYMBOL PRICE
    
  • Command directives: include

Example

Parsing:

let ledger: ledger_parser::Ledger = r#"; Example 1
2018-10-01=2018-10-14 ! (123) Description
  ; Transaction comment
  TEST:Account 123  $1.20
  ; Posting comment
  TEST:Account 345  -$1.20"#
    .parse()?;

Serializing:

use ledger_parser::{ Serializer, SerializerSettings };

println!("{}", ledger);
println!("{}", ledger.to_string_pretty(&SerializerSettings::default().with_indent("\t")));

See also

  • ledger-utils - ledger-cli file processing Rust library, useful for calculating balances, creating reports etc.

Dependencies

~3MB
~56K SLoC