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::IncompleteAmount;
pub use cost::Cost;
pub use cost::CostSpec;
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::Query;
pub use directive::Transaction;
pub use directive::sort_directives;
pub use display_context::DisplayContext;
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 format::FormatConfig;
pub use format::format_directive;
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::ReductionScope;
pub use position::Position;
pub use intern::AsDecimal;
pub use intern::AsInternedStr;
pub use intern::AsNaiveDate;

Modules§

amount
Amount type representing a decimal number with a currency.
cost
Cost and cost specification types.
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.
intern
String interning for accounts and currencies.
inventory
Inventory type representing a collection of positions.
position
Position type representing units held at a cost.
synthetic
Synthetic beancount file generation for testing.

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.