Expand description
Core types for rustledger
This crate provides the fundamental types used throughout the rustledger project:
Amount- A decimal number with a currencyCost- Acquisition cost of a position (lot)CostSpec- Specification for matching or creating costsPosition- Units held at a costInventory- A collection of positions with booking supportBookingMethod- How to match lots when reducing positionsDirective- All directive types (Transaction, Balance, Open, etc.)
§Example
use rustledger_core::{Amount, Cost, Position, Inventory, BookingMethod};
use rust_decimal_macros::dec;
use chrono::NaiveDate;
// Create an inventory
let mut inv = Inventory::new();
// Add a stock position with cost
let cost = Cost::new(dec!(150.00), "USD")
.with_date(NaiveDate::from_ymd_opt(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 * 150Re-exports§
pub use amount::Amount;pub use amount::IncompleteAmount;pub use cost::Cost;pub use cost::CostSpec;pub use directive::sort_directives;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 format::format_directive;pub use format::FormatConfig;pub use inventory::BookingError;pub use inventory::BookingMethod;pub use inventory::BookingResult;pub use inventory::Inventory;pub use position::Position;
Modules§
- amount
- Amount type representing a decimal number with a currency.
- cost
- Cost and cost specification types.
- directive
- Directive types representing all beancount 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.
Structs§
- Decimal
Decimalrepresents a 128 bit representation of a fixed-precision decimal number. The finite set of values of typeDecimalare 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.- Naive
Date - ISO 8601 calendar date without timezone. Allows for every proleptic Gregorian date from Jan 1, 262145 BCE to Dec 31, 262143 CE. Also supports the conversion from ISO 8601 ordinal and week date.