pub struct Position {
pub units: Amount,
pub cost: Option<Cost>,
}Expand description
A position is units of a currency held at an optional cost.
For simple currencies (cash), positions typically have no cost. For investments (stocks, crypto), positions track the cost basis for capital gains calculations.
§Examples
use rustledger_core::{Amount, Cost, Position};
use rust_decimal_macros::dec;
use chrono::NaiveDate;
// Simple position (no cost)
let cash = Position::simple(Amount::new(dec!(1000.00), "USD"));
assert!(cash.cost.is_none());
// Position with cost (lot)
let cost = Cost::new(dec!(150.00), "USD")
.with_date(NaiveDate::from_ymd_opt(2024, 1, 15).unwrap());
let stock = Position::with_cost(
Amount::new(dec!(10), "AAPL"),
cost
);
assert!(stock.cost.is_some());Fields§
§units: AmountThe units held (number + currency/commodity)
cost: Option<Cost>The cost basis (if tracked)
Implementations§
Source§impl Position
impl Position
Sourcepub const fn simple(units: Amount) -> Self
pub const fn simple(units: Amount) -> Self
Create a new position without cost tracking.
Use this for simple currency positions like cash.
Sourcepub const fn with_cost(units: Amount, cost: Cost) -> Self
pub const fn with_cost(units: Amount, cost: Cost) -> Self
Create a new position with cost tracking.
Use this for investment positions (stocks, crypto, etc.) where cost basis matters.
Sourcepub fn cost_currency(&self) -> Option<&str>
pub fn cost_currency(&self) -> Option<&str>
Get the cost currency, if this position has a cost.
Sourcepub fn book_value(&self) -> Option<Amount>
pub fn book_value(&self) -> Option<Amount>
Calculate the book value (total cost) of this position.
Returns None if there is no cost.
Sourcepub fn matches_cost_spec(&self, spec: &CostSpec) -> bool
pub fn matches_cost_spec(&self, spec: &CostSpec) -> bool
Check if this position matches a cost specification.
Returns true if:
- Both have no cost, or
- The position’s cost matches the spec
Sourcepub fn can_reduce(&self, reduction: &Amount) -> bool
pub fn can_reduce(&self, reduction: &Amount) -> bool
Check if this position can be reduced by another amount.
A position can be reduced if:
- The currencies match
- The reduction is in the opposite direction (selling what you have)