1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- use itertools::Itertools;
- use super::{AccountName, Decimal, Spanned, UnitName};
- 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<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 annotations: Vec<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()),
- ))
- }
- pub fn is_mono_unit(&self) -> bool {
- self.balances.iter().unique_by(|b| *b.unit).count() == 1
- }
- pub fn mono_unit(&self) -> Option<UnitName> {
- let mut it = self.balances.iter().unique_by(|b| *b.unit);
- let uniq = it.next()?;
- it.next().is_none().then_some(*uniq.unit)
- }
- }
|