Skip to main content

Crate rustledger_core

Crate rustledger_core 

Source
Expand description

Core types for rustledger

This crate provides the fundamental types used throughout the rustledger project:

  • Amount - A decimal number with a currency
  • Cost - Acquisition cost of a position (lot)
  • CostSpec - Specification for matching or creating costs
  • Position - Units held at a cost
  • Inventory - A collection of positions with booking support
  • BookingMethod - How to match lots when reducing positions
  • Directive - All directive types (Transaction, Balance, Open, etc.)

§Example

use rustledger_core::{Amount, Cost, Position, Inventory, BookingMethod};
use rust_decimal_macros::dec;

// Create an inventory
let mut inv = Inventory::new();

// Add a stock position with cost
let cost = Cost::new(dec!(150.00), "USD")
    .with_date(rustledger_core::naive_date(2024, 1, 15).unwrap());
inv.add(Position::with_cost(Amount::new(dec!(10), "AAPL"), cost));

// Check holdings
assert_eq!(inv.units("AAPL"), dec!(10));

// Sell some shares using FIFO
let result = inv.reduce(
    &Amount::new(dec!(-5), "AAPL"),
    None,
    BookingMethod::Fifo,
).unwrap();

assert_eq!(inv.units("AAPL"), dec!(5));
assert_eq!(result.cost_basis.unwrap().number, dec!(750.00)); // 5 * 150

Re-exports§

pub use amount::Amount;
pub use amount::AmountParseError;
pub use amount::AmountParseErrorReason;
pub use amount::IncompleteAmount;
pub use calendar::CalendarPeriod;
pub use calendar::quarter_index0;
pub use cost::BookedCost;
pub use cost::BookedCostInvariantError;
pub use cost::Cost;
pub use cost::CostNumber;
pub use cost::CostSpec;
pub use decimal::add_python_scale;
pub use decimal::checked_add_python_scale;
pub use decimal::checked_div_python_scale;
pub use decimal::checked_sub_python_scale;
pub use decimal::negate_python;
pub use decimal::round_dp_python;
pub use decimal::sub_python_scale;
pub use directive::Balance;
pub use directive::Close;
pub use directive::Commodity;
pub use directive::Custom;
pub use directive::Directive;
pub use directive::DirectivePriority;
pub use directive::Document;
pub use directive::Event;
pub use directive::MetaValue;
pub use directive::Metadata;
pub use directive::Note;
pub use directive::Open;
pub use directive::Pad;
pub use directive::Posting;
pub use directive::Price;
pub use directive::PriceAnnotation;
pub use directive::PriceKind;
pub use directive::Query;
pub use directive::Transaction;
pub use directive::booking_sort_key;
pub use directive::meta_value_as_bool;
pub use directive::parse_bool_word;
pub use directive::parse_precision_meta;
pub use directive::sort_directives;
pub use display_context::DEFAULT_CURRENCY;
pub use display_context::DisplayContext;
pub use display_context::OutputSurface;
pub use display_context::Precision;
pub use extract::DEFAULT_CURRENCIES;
pub use extract::extract_accounts;
pub use extract::extract_accounts_iter;
pub use extract::extract_currencies;
pub use extract::extract_currencies_iter;
pub use extract::extract_payees;
pub use extract::extract_payees_iter;
pub use extract::extract_tags;
pub use extract::extract_tags_iter;
pub use format::Alignment;
pub use format::FormatConfig;
pub use format::FormatLine;
pub use format::format_directive_lines;
pub use format::format_directives;
pub use format::format_posting_line;
pub use format::posting_format_line;
pub use format::render_lines;
pub use format::resolve_alignment;
pub use identifiers::ACCOUNT_TYPES;
pub use identifiers::Account;
pub use identifiers::AccountTypeKind;
pub use identifiers::AccountTypes;
pub use identifiers::Currency;
pub use identifiers::Tag;
pub use identifiers::account_type;
pub use identifiers::is_default_account_root;
pub use identifiers::is_subaccount_or_equal;
pub use implicit_prices::extract_per_unit_price;
pub use intern::InternedStr;
pub use intern::StringInterner;
pub use inventory::AccountedBookingError;
pub use inventory::BookingError;
pub use inventory::BookingMethod;
pub use inventory::BookingResult;
pub use inventory::Inventory;
pub use inventory::OverflowError;
pub use inventory::ReductionScope;
pub use inventory::sum_account_and_subaccounts;
pub use meta_json::json_to_meta_value;
pub use meta_json::meta_value_to_json;
pub use meta_json::meta_value_type_tag;
pub use position::Position;
pub use span::SYNTHESIZED_FILE_ID;
pub use span::ShiftSpans;
pub use span::Span;
pub use span::Spanned;
pub use visit::visit_accounts;
pub use visit::visit_currencies;
pub use intern::AsDecimal;
pub use intern::AsInternedStr;
pub use intern::AsNaiveDate;

Modules§

amount
Amount type representing a decimal number with a currency.
calendar
Calendar period truncation — the single definition of “which month/quarter/ week/year is this date in, and when does that period start”.
cost
Cost and cost specification types.
decimal
Decimal arithmetic with Python decimal scale semantics.
directive
Directive types representing all beancount directives.
display_context
Display context for formatting numbers with consistent precision.
extract
Extract unique accounts, currencies, and payees from directives.
format
Beancount file formatter.
identifiers
Domain-typed identifiers: Account, Currency, Tag, Link.
implicit_prices
Shared implicit-price extraction logic.
intern
String interning for accounts and currencies.
inventory
Inventory type representing a collection of positions.
meta_json
Canonical JSON wire codec for MetaValue.
position
Position type representing units held at a cost.
shift_spans_impls
ShiftSpans impls for every type reachable from a Directive.
span
Source location tracking.
synthetic
Synthetic beancount file generation for testing.
visit
Visitors for AST positions that carry a currency or an account name.

Macros§

impl_shift_spans_noop
Helper macro for declaring ShiftSpans no-op impls on leaf types.

Structs§

Decimal
Decimal represents a 128 bit representation of a fixed-precision decimal number. The finite set of values of type Decimal are of the form m / 10e, where m is an integer such that -296 < m < 296, and e is an integer between 0 and 28 inclusive.

Functions§

naive_date
Construct a NaiveDate from (year, month, day) with i32/u32 arguments.

Type Aliases§

NaiveDate
Calendar date without timezone. Alias for jiff::civil::Date.