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//!
19//! // Create an inventory
20//! let mut inv = Inventory::new();
21//!
22//! // Add a stock position with cost
23//! let cost = Cost::new(dec!(150.00), "USD")
24//! .with_date(rustledger_core::naive_date(2024, 1, 15).unwrap());
25//! inv.add(Position::with_cost(Amount::new(dec!(10), "AAPL"), cost));
26//!
27//! // Check holdings
28//! assert_eq!(inv.units("AAPL"), dec!(10));
29//!
30//! // Sell some shares using FIFO
31//! let result = inv.reduce(
32//! &Amount::new(dec!(-5), "AAPL"),
33//! None,
34//! BookingMethod::Fifo,
35//! ).unwrap();
36//!
37//! assert_eq!(inv.units("AAPL"), dec!(5));
38//! assert_eq!(result.cost_basis.unwrap().number, dec!(750.00)); // 5 * 150
39//! ```
40
41#![forbid(unsafe_code)]
42#![warn(missing_docs)]
43
44pub mod amount;
45pub mod cost;
46pub mod directive;
47pub mod display_context;
48pub mod extract;
49pub mod format;
50pub mod intern;
51pub mod inventory;
52pub mod position;
53pub mod synthetic;
54
55// Kani formal verification proofs (only compiled with Kani)
56#[cfg(kani)]
57mod kani_proofs;
58
59pub use amount::{Amount, IncompleteAmount};
60pub use cost::{Cost, CostSpec};
61pub use directive::{
62 Balance, Close, Commodity, Custom, Directive, DirectivePriority, Document, Event, MetaValue,
63 Metadata, Note, Open, Pad, Posting, Price, PriceAnnotation, Query, Transaction,
64 sort_directives,
65};
66pub use display_context::DisplayContext;
67pub use extract::{
68 DEFAULT_CURRENCIES, extract_accounts, extract_accounts_iter, extract_currencies,
69 extract_currencies_iter, extract_payees, extract_payees_iter,
70};
71pub use format::{FormatConfig, format_directive};
72pub use intern::{InternedStr, StringInterner};
73pub use inventory::{
74 AccountedBookingError, BookingError, BookingMethod, BookingResult, Inventory, ReductionScope,
75};
76pub use position::Position;
77
78// Re-export commonly used external types
79/// Calendar date without timezone. Alias for `jiff::civil::Date`.
80pub type NaiveDate = jiff::civil::Date;
81pub use rust_decimal::Decimal;
82
83/// Construct a [`NaiveDate`] from `(year, month, day)` with i32/u32 arguments.
84///
85/// Wraps [`jiff::civil::date`] which takes `(i16, i8, i8)`.
86/// Returns `None` if the date is invalid.
87#[must_use]
88pub fn naive_date(year: i32, month: u32, day: u32) -> Option<NaiveDate> {
89 i16::try_from(year)
90 .ok()
91 .and_then(|y| i8::try_from(month).ok().map(|m| (y, m)))
92 .and_then(|(y, m)| i8::try_from(day).ok().map(|d| (y, m, d)))
93 .and_then(|(y, m, d)| NaiveDate::new(y, m, d).ok())
94}
95
96// Re-export rkyv wrappers when feature is enabled
97#[cfg(feature = "rkyv")]
98pub use intern::{AsDecimal, AsInternedStr, AsNaiveDate};