rustledger_core/lib.rs
1//! Core types for rustledger
2//!
3//! This crate provides the fundamental types used throughout the rustledger project:
4//!
5//! - [`Amount`] - A decimal number with a currency
6//! - [`Cost`] - Acquisition cost of a position (lot)
7//! - [`CostSpec`] - Specification for matching or creating costs
8//! - [`Position`] - Units held at a cost
9//! - [`Inventory`] - A collection of positions with booking support
10//! - [`BookingMethod`] - How to match lots when reducing positions
11//! - [`Directive`] - All directive types (Transaction, Balance, Open, etc.)
12//!
13//! # Example
14//!
15//! ```
16//! use rustledger_core::{Amount, Cost, Position, Inventory, BookingMethod};
17//! use rust_decimal_macros::dec;
18//! use chrono::NaiveDate;
19//!
20//! // Create an inventory
21//! let mut inv = Inventory::new();
22//!
23//! // Add a stock position with cost
24//! let cost = Cost::new(dec!(150.00), "USD")
25//! .with_date(NaiveDate::from_ymd_opt(2024, 1, 15).unwrap());
26//! inv.add(Position::with_cost(Amount::new(dec!(10), "AAPL"), cost));
27//!
28//! // Check holdings
29//! assert_eq!(inv.units("AAPL"), dec!(10));
30//!
31//! // Sell some shares using FIFO
32//! let result = inv.reduce(
33//! &Amount::new(dec!(-5), "AAPL"),
34//! None,
35//! BookingMethod::Fifo,
36//! ).unwrap();
37//!
38//! assert_eq!(inv.units("AAPL"), dec!(5));
39//! assert_eq!(result.cost_basis.unwrap().number, dec!(750.00)); // 5 * 150
40//! ```
41
42#![forbid(unsafe_code)]
43#![warn(missing_docs)]
44
45pub mod amount;
46pub mod cost;
47pub mod directive;
48pub mod format;
49pub mod intern;
50pub mod inventory;
51pub mod position;
52
53pub use amount::{Amount, IncompleteAmount};
54pub use cost::{Cost, CostSpec};
55pub use directive::{
56 sort_directives, Balance, Close, Commodity, Custom, Directive, DirectivePriority, Document,
57 Event, MetaValue, Metadata, Note, Open, Pad, Posting, Price, PriceAnnotation, Query,
58 Transaction,
59};
60pub use format::{format_directive, FormatConfig};
61pub use inventory::{BookingError, BookingMethod, BookingResult, Inventory};
62pub use position::Position;
63
64// Re-export commonly used external types
65pub use chrono::NaiveDate;
66pub use rust_decimal::Decimal;