ExLedger.LedgerParser (ex_ledger v0.5.6)

Parser for ledger-cli format files.

This module serves as the main entry point and facade for ledger parsing functionality. Core parsing is delegated to specialized submodules under ExLedger.Parser.

Summary

Functions

Calculates the balance for each account by summing all postings.

Formats a balance report for transactions with optional filtering and output options.

Checks whether the ledger file at the given path parses successfully.

Checks a ledger file and returns {:ok, :valid} or {:error, reason}.

Checks whether the given ledger string parses successfully.

Formats account postings as a register report.

Formats account balances as a balance report.

Formats transactions into ledger-compatible output.

Gets all postings for a specific account with running balance.

Parses a ledger file with support for include directives and account declarations.

Builds a register view of postings with running balances.

Types

account_declaration()

@type account_declaration() :: ExLedger.Parser.Core.account_declaration()

amount()

@type amount() :: ExLedger.Parser.Core.amount()

commodity_definition()

@type commodity_definition() :: ExLedger.Parser.Core.commodity_definition()

ledger_error()

@type ledger_error() :: ExLedger.Parser.Core.ledger_error()

parse_error()

@type parse_error() :: ExLedger.Parser.Core.parse_error()

parse_error_detail()

@type parse_error_detail() :: ExLedger.Parser.Core.parse_error_detail()

parse_result()

@type parse_result() :: %{
  transactions: [transaction()],
  accounts: %{required(String.t()) => atom()},
  prices: [price_directive()],
  commodities: %{required(String.t()) => commodity_definition()}
}

posting()

@type posting() :: ExLedger.Parser.Core.posting()

price_db()

@type price_db() :: ExLedger.Parser.Price.price_db()

price_directive()

@type price_directive() :: ExLedger.Parser.Core.price_directive()

time_entry()

@type time_entry() :: ExLedger.Parser.Timeclock.time_entry()

transaction()

@type transaction() :: ExLedger.Parser.Core.transaction()

Functions

balance(transaction)

@spec balance([transaction()] | transaction()) :: %{required(String.t()) => [map()]}

Calculates the balance for each account by summing all postings.

balance_by_period(transactions, group_by \\ "none", start_date \\ nil, end_date \\ nil, account_filter \\ nil)

@spec balance_by_period(
  list(),
  String.t(),
  Date.t() | nil,
  Date.t() | nil,
  function() | nil
) :: %{
  required(String.t()) => any()
}

balance_postings(transaction_or_postings)

See ExLedger.Parser.Transaction.balance_postings/1.

balance_report(transactions, report_regex \\ nil, opts \\ [])

@spec balance_report([transaction()], Regex.t() | nil, keyword()) :: String.t()

Formats a balance report for transactions with optional filtering and output options.

budget_report(transactions, date \\ Date.utc_today())

@spec budget_report([transaction()], Date.t()) :: [map()]

build_price_db(prices)

See ExLedger.Parser.Price.build_price_db/1.

build_xact(transactions, date, payee_pattern)

@spec build_xact([transaction()], Date.t(), String.t()) ::
  {:ok, String.t()} | {:error, atom()}

can_convert?(from_currency, to_currency, date, price_db)

See ExLedger.Exchange.can_convert?/4.

check_accounts(transactions, accounts)

See ExLedger.Parser.Declarations.check_accounts/2.

check_commodities(transactions, declared_commodities)

See ExLedger.Parser.Declarations.check_commodities/2.

check_file(path)

@spec check_file(String.t()) :: boolean()

Checks whether the ledger file at the given path parses successfully.

check_file_with_error(path)

@spec check_file_with_error(String.t()) :: {:ok, :valid} | {:error, ledger_error()}

Checks a ledger file and returns {:ok, :valid} or {:error, reason}.

check_payees(transactions, declared_payees)

See ExLedger.Parser.Declarations.check_payees/2.

check_string(content, base_dir \\ ".")

@spec check_string(String.t(), String.t()) :: boolean()

Checks whether the given ledger string parses successfully.

check_tags(transactions, contents, declared_tags)

See ExLedger.Parser.Declarations.check_tags/3.

convert_amount(amount, target_currency, date, price_db)

See ExLedger.Exchange.convert_amount/4.

convert_transaction(transaction, target_currency, price_db)

See ExLedger.Exchange.convert_transaction/3.

exchange(transactions, target_currency, price_db)

See ExLedger.Exchange.exchange/3.

expand_includes(input, base_dir, seen_files \\ MapSet.new(), source_file \\ nil)

@spec expand_includes(String.t(), String.t(), MapSet.t(String.t()), String.t() | nil) ::
  {:ok, String.t()} | {:error, ledger_error()}

extract_account_declarations(input)

See ExLedger.Parser.Accounts.extract_account_declarations/1.

extract_commodity_declarations(input)

See ExLedger.Parser.Declarations.extract_commodity_declarations/1.

extract_full_commodity_declarations(input)

See ExLedger.Parser.Declarations.extract_full_commodity_declarations/1.

extract_payee_declarations(input)

See ExLedger.Parser.Declarations.extract_payee_declarations/1.

extract_price_directives(input)

See ExLedger.Parser.Price.extract_price_directives/1.

extract_tag_declarations(input)

See ExLedger.Parser.Declarations.extract_tag_declarations/1.

first_transaction(transactions)

See ExLedger.Parser.Declarations.first_transaction/1.

forecast_balance(transactions, months \\ 1)

@spec forecast_balance([transaction()], pos_integer()) :: %{
  required(String.t()) => [map()]
}

format_account_register(postings, account_name)

@spec format_account_register([map()], String.t()) :: String.t()

Formats account postings as a register report.

format_amount_for_currency(value, currency, currency_position \\ :leading)

See ExLedger.Parser.Helpers.format_amount_for_currency/3.

format_balance(balances, opts \\ false)

@spec format_balance(%{required(String.t()) => [map()]}, boolean() | keyword()) ::
  String.t()

Formats account balances as a balance report.

format_balance_by_period(map, opts \\ [])

@spec format_balance_by_period(
  %{required(String.t()) => any()},
  keyword()
) :: String.t()

format_budget_report(rows)

@spec format_budget_report([map()]) :: String.t()

format_select(fields, rows)

@spec format_select([String.t()], [map()]) :: String.t()

format_stats(stats)

@spec format_stats(map()) :: String.t()

format_timeclock_report(report)

See ExLedger.Parser.Timeclock.format_timeclock_report/1.

format_transactions(transactions)

@spec format_transactions([transaction()]) :: String.t()

Formats transactions into ledger-compatible output.

get_account_postings(transactions, account_name)

@spec get_account_postings([transaction()], String.t()) :: [map()]

Gets all postings for a specific account with running balance.

last_transaction(transactions)

See ExLedger.Parser.Declarations.last_transaction/1.

list_accounts(transactions, account_map \\ %{})

See ExLedger.Parser.Declarations.list_accounts/2.

list_commodities(transactions)

See ExLedger.Parser.Declarations.list_commodities/1.

list_payees(transactions)

See ExLedger.Parser.Declarations.list_payees/1.

list_tags(transactions)

See ExLedger.Parser.Declarations.list_tags/1.

lookup_price(from_commodity, to_commodity, date, price_db)

See ExLedger.Parser.Price.lookup_price/4.

parse_account_declaration(input)

See ExLedger.Parser.Accounts.parse_account_declaration/1.

parse_amount(amount_string)

See ExLedger.Parser.Transaction.parse_amount/1.

parse_date(date_string)

See ExLedger.Parser.Transaction.parse_date/1.

parse_ledger(input, opts \\ [])

@spec parse_ledger(
  String.t(),
  keyword()
) :: {:ok, parse_result()} | {:error, ledger_error()}

Parses a ledger file with support for include directives and account declarations.

Returns a map containing:

  • :transactions - List of parsed transactions
  • :accounts - Map of account names to their types
  • :prices - List of price directives
  • :commodities - Map of commodity symbols to their definitions

parse_note(note_string)

See ExLedger.Parser.Transaction.parse_note/1.

parse_posting(line)

See ExLedger.Parser.Transaction.parse_posting/1.

parse_price_directive(line)

See ExLedger.Parser.Price.parse_price_directive/1.

parse_timeclock_entries(input)

See ExLedger.Parser.Timeclock.parse_timeclock_entries/1.

parse_transaction(input)

See ExLedger.Parser.Transaction.parse_transaction/1.

register(transactions, account_regex \\ nil)

@spec register([transaction()], Regex.t() | nil) :: [map()]

Builds a register view of postings with running balances.

resolve_account_name(account_name, account_map)

See ExLedger.Parser.Accounts.resolve_account_name/2.

resolve_transaction_aliases(transactions, account_map)

See ExLedger.Parser.Accounts.resolve_transaction_aliases/2.

select(transactions, query)

@spec select([transaction()], String.t()) ::
  {:ok, [String.t()], [map()]} | {:error, atom()}

stats(transactions)

@spec stats([transaction()]) :: map()

timeclock_report(entries)

See ExLedger.Parser.Timeclock.timeclock_report/1.

validate_transaction(transaction)

See ExLedger.Parser.Transaction.validate_transaction/1.