Skip to main content

Ledger

Struct Ledger 

Source
pub struct Ledger {
    pub directives: Vec<Spanned<Directive>>,
    pub options: Options,
    pub plugins: Vec<Plugin>,
    pub source_map: SourceMap,
    pub errors: Vec<LedgerError>,
    pub display_context: DisplayContext,
}
Expand description

A fully processed ledger.

This is the result of loading and processing a beancount file, equivalent to the tuple returned by Python’s loader.load_file().

Fields§

§directives: Vec<Spanned<Directive>>

Processed directives in source-faithful form: sorted by date, booked (cost specs resolved, interpolations applied), and plugin-rewritten. Pad directives remain as Pad; they are not pre-expanded into synthesized transactions.

Consumers split into two groups:

  • Source-faithful consumers (stats, journal, formatter, LSP, BQL FROM #entries WHERE type = 'pad' audits, source-mapped diagnostics) iterate this field directly. Pads count as Pads.
  • Balance-computing consumers (holdings, balances, balsheet, networth, income, FFI query.execute/batch, WASM expandPads/query) call Ledger::balance_view to get the directive stream MERGED with synthesized P-flag transactions for each pad-balance pair. This is the only way to get pad effects into per-account inventory math.

The two views are derived from the same source; there is no drift possible because Ledger::balance_view is a pure function of self.directives.

§options: Options

Options parsed from the file.

§plugins: Vec<Plugin>

Plugins declared in the file.

§source_map: SourceMap

Source map for error reporting.

§errors: Vec<LedgerError>

Errors encountered during processing.

§display_context: DisplayContext

Display context for formatting numbers.

Implementations§

Source§

impl Ledger

Source

pub fn balance_view(&self) -> Vec<Directive>

Return the directive stream merged with synthesized pad-equivalent transactions, suitable for inventory / balance math.

For each Pad directive followed (in date order) by a Balance assertion on the same account, a Transaction with flag = 'P' is added to the view carrying the postings needed to make the balance match. A multi-currency pad produces one synth transaction per currency.

Original Pad directives are preserved in the view. Synth transactions are added alongside, not in place of. This matters for two reasons:

  1. BQL queries against the #entries table (SELECT * FROM #entries WHERE type = 'pad') can still enumerate the pad directives the user authored. A REPLACE-style expansion would silently zero those out. (BQL’s default SELECT path operates on postings; pads have no postings, so a default SELECT never matches them regardless of this view shape.)
  2. Multi-pad cases (issue #1300) produce exactly one synth per pad-balance pair: rustledger_booking::process_pads (which merge_with_padding delegates to) only retains the most recent same-account pad in its pending-pads map, so earlier same-account pads are silently shadowed and their source_account does NOT contribute to the synth. The validator emits E2003 for shadowed pads independently; this view reflects only the effective pad.

Inventory-walking consumers iterate Directive::Transaction and ignore Pad directives, so the preserved Pads are invisible to them.

When to use this vs. Ledger.directives: any consumer that maintains running per-account inventory state and asks “what is the balance” needs this view. Any consumer that asks “what did the user write” wants the raw directives field.

§Performance

Each call clones every source directive once (O(n)). Inlines the merge logic from rustledger_booking::merge_with_padding so the already- owned booked vector can be moved into the merged output instead of cloned a second time. For short-lived CLI invocations the single clone is negligible. Long-lived processes (FFI servers, LSPs) that query the same ledger repeatedly should hoist the result above their loop. TODO(perf): memoize internally once a benchmark shows it matters.

Trait Implementations§

Source§

impl Debug for Ledger

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> ArchivePointee for T

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> LayoutRaw for T

Source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Returns the layout of the type.
Source§

impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
where T: SharedNiching<N1, N2>, N1: Niching<T>, N2: Niching<T>,

Source§

unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool

Returns whether the given value has been niched. Read more
Source§

fn resolve_niched(out: Place<NichedOption<T, N1>>)

Writes data to out indicating that a T is niched.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The metadata type for pointers and references to this type.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.