1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- use super::{AccountName, UnitName, Spanned};
- mod parse;
- pub use parse::parse_ledger;
- mod print;
- pub use print::print_ledger;
- #[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Eq)]
- pub struct Balance {
- pub account: Spanned<AccountName>,
- pub amount: Spanned<rust_decimal::Decimal>,
- pub unit: Spanned<UnitName>,
- }
- #[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord)]
- pub struct LedgerEntry {
- pub datestamp: (u16, u8, u8),
- pub title: Option<String>,
- pub balances: Vec<Spanned<Balance>>,
- }
- impl LedgerEntry {
- pub fn modifies(&self, account: AccountName) -> bool {
- self.balances.iter().any(|b| b.account.as_ref() == &account)
- }
- pub fn balance_for(&self, account: AccountName) -> Option<&Spanned<Balance>> {
- self.balances.iter().find(|b| b.account.as_ref() == &account)
- }
- pub fn split_balances(
- &self,
- account: AccountName,
- ) -> Option<(&Spanned<Balance>, impl Iterator<Item = &Spanned<Balance>>)> {
- let index = self.balances.iter().position(|b| b.account.as_ref() == &account)?;
- Some((
- &self.balances[index],
- self.balances[0..index]
- .iter()
- .chain(self.balances[index + 1..].iter()),
- ))
- }
- }
|