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)
Trait Implementations§
Source§impl Archive for Position
impl Archive for Position
Source§const COPY_OPTIMIZATION: CopyOptimization<Self>
const COPY_OPTIMIZATION: CopyOptimization<Self>
serialize. Read moreSource§type Archived = ArchivedPosition
type Archived = ArchivedPosition
Source§type Resolver = PositionResolver
type Resolver = PositionResolver
Source§impl<'de> Deserialize<'de> for Position
impl<'de> Deserialize<'de> for Position
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl FromIterator<Position> for Inventory
impl FromIterator<Position> for Inventory
impl Eq for Position
impl StructuralPartialEq for Position
Auto Trait Implementations§
impl Freeze for Position
impl RefUnwindSafe for Position
impl Send for Position
impl Sync for Position
impl Unpin for Position
impl UnwindSafe for Position
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> ArchiveUnsized for Twhere
T: Archive,
impl<T> ArchiveUnsized for Twhere
T: Archive,
Source§type Archived = <T as Archive>::Archived
type Archived = <T as Archive>::Archived
Archive, it may be
unsized. Read moreSource§fn archived_metadata(
&self,
) -> <<T as ArchiveUnsized>::Archived as ArchivePointee>::ArchivedMetadata
fn archived_metadata( &self, ) -> <<T as ArchiveUnsized>::Archived as ArchivePointee>::ArchivedMetadata
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.