pub struct Inventory { /* private fields */ }Expand description
An inventory is a collection of positions.
It tracks all positions for an account and supports booking operations for adding and reducing positions.
§Examples
use rustledger_core::{Inventory, Position, Amount, Cost, BookingMethod};
use rust_decimal_macros::dec;
let mut inv = Inventory::new();
// Add a simple position
inv.add(Position::simple(Amount::new(dec!(100), "USD")));
assert_eq!(inv.units("USD"), dec!(100));
// Add a position with cost
let cost = Cost::new(dec!(150.00), "USD");
inv.add(Position::with_cost(Amount::new(dec!(10), "AAPL"), cost));
assert_eq!(inv.units("AAPL"), dec!(10));Implementations§
Source§impl Inventory
impl Inventory
Sourcepub fn positions_mut(&mut self) -> &mut Vec<Position>
pub fn positions_mut(&mut self) -> &mut Vec<Position>
Get mutable access to all positions.
Sourcepub fn units(&self, currency: &str) -> Decimal
pub fn units(&self, currency: &str) -> Decimal
Get total units of a currency (ignoring cost lots).
This sums all positions of the given currency regardless of cost basis.
Sourcepub fn currencies(&self) -> Vec<&str>
pub fn currencies(&self) -> Vec<&str>
Get all currencies in this inventory.
Sourcepub fn book_value(&self, units_currency: &str) -> HashMap<InternedStr, Decimal>
pub fn book_value(&self, units_currency: &str) -> HashMap<InternedStr, Decimal>
Get the total book value (cost basis) for a currency.
Returns the sum of all cost bases for positions of the given currency.
Sourcepub fn add(&mut self, position: Position)
pub fn add(&mut self, position: Position)
Add a position to the inventory.
For positions with cost, this creates a new lot. For positions without cost, this merges with existing positions of the same currency.
Sourcepub fn reduce(
&mut self,
units: &Amount,
cost_spec: Option<&CostSpec>,
method: BookingMethod,
) -> Result<BookingResult, BookingError>
pub fn reduce( &mut self, units: &Amount, cost_spec: Option<&CostSpec>, method: BookingMethod, ) -> Result<BookingResult, BookingError>
Reduce positions from the inventory using the specified booking method.
§Arguments
units- The units to reduce (negative for selling)cost_spec- Optional cost specification for matching lotsmethod- The booking method to use
§Returns
Returns a BookingResult with the matched positions and cost basis,
or a BookingError if the reduction cannot be performed.
Trait Implementations§
Source§impl Archive for Inventory
impl Archive for Inventory
Source§const COPY_OPTIMIZATION: CopyOptimization<Self>
const COPY_OPTIMIZATION: CopyOptimization<Self>
serialize. Read moreSource§type Archived = ArchivedInventory
type Archived = ArchivedInventory
Source§type Resolver = InventoryResolver
type Resolver = InventoryResolver
Source§impl<'de> Deserialize<'de> for Inventory
impl<'de> Deserialize<'de> for Inventory
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 Inventory
impl StructuralPartialEq for Inventory
Auto Trait Implementations§
impl Freeze for Inventory
impl RefUnwindSafe for Inventory
impl Send for Inventory
impl Sync for Inventory
impl Unpin for Inventory
impl UnwindSafe for Inventory
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.