Skip to content

Commit

Permalink
Merge pull request #51 from frosklis/release/0.16
Browse files Browse the repository at this point in the history
Release/0.16
  • Loading branch information
frosklis authored Mar 4, 2021
2 parents a92bd7e + 038cd8f commit 17ff6fc
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 120 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
# Changelog
Changelog file for dinero-rs project, a command line application for managing finances.

## [0.16.0] - 2021-03-04
### Added
- Virtual postings show correctly like this ```(account)```
### Fixed
- Now you can add tags [through automated transactions](https://github.com/frosklis/dinero-rs/issues/49)
## [0.15.0] - 2021-02-28
### Fixed
- Correct caonversion of currencies. There were [certain cases that did not work properly](https://github.com/frosklis/dinero-rs/issues/37)
- Correct conversion of currencies. There were [certain cases that did not work properly](https://github.com/frosklis/dinero-rs/issues/37)
### Added
- complete transaction grammar
## [0.14.0] - 2021-02-27
Expand Down
2 changes: 1 addition & 1 deletion src/commands/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn execute(options: &CommonOpts, flat: bool, show_total: bool) -> Result<(),
};

for t in ledger.transactions.iter() {
for p in t.postings_iter() {
for p in t.postings.borrow().iter() {
if !filter::filter(&options, &node, t, p, &mut ledger.commodities)? {
continue;
}
Expand Down
26 changes: 19 additions & 7 deletions src/commands/register.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::models::PostingType;
use crate::models::{Balance, Money};
use crate::parser::value_expr::build_root_node_from_expression;
use crate::parser::Tokenizer;
Expand All @@ -6,7 +7,6 @@ use crate::{filter, CommonOpts};
use colored::Colorize;
use std::collections::HashMap;
use terminal_size::{terminal_size, Width};

/// Register report
pub fn execute(options: &CommonOpts) -> Result<(), Error> {
// Get options from options
Expand Down Expand Up @@ -50,7 +50,7 @@ pub fn execute(options: &CommonOpts) -> Result<(), Error> {

for t in ledger.transactions.iter() {
let mut counter = 0;
for p in t.postings_iter() {
for p in t.postings.borrow().iter() {
if !filter::filter(&options, &node, t, p, &mut ledger.commodities)? {
continue;
}
Expand All @@ -74,11 +74,23 @@ pub fn execute(options: &CommonOpts) -> Result<(), Error> {
if balance.is_zero() {
balance = Balance::from(Money::Zero);
}
print!(
"{:width$}",
format!("{}", p.account).blue(),
width = w_account
);
match p.kind {
PostingType::Real => print!(
"{:width$}",
format!("{}", p.account).blue(),
width = w_account
),
PostingType::Virtual => print!(
"{:width$}",
format!("({})", p.account).blue(),
width = w_account
),
PostingType::VirtualMustBalance => print!(
"{:width$}",
format!("[{}]", p.account).blue(),
width = w_account
),
}

match p.amount.as_ref().unwrap().is_negative() {
false => print!(
Expand Down
1 change: 1 addition & 0 deletions src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use colored::Colorize;
use regex::Regex;
use std::collections::HashMap;

/// Filters a posting based on the options
pub fn filter(
options: &CommonOpts,
node: &Option<Node>,
Expand Down
65 changes: 24 additions & 41 deletions src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use payee::Payee;
pub use price::conversion;
pub use price::{Price, PriceType};
pub use transaction::{
Cleared, Posting, PostingType, Transaction, TransactionStatus, TransactionType,
Cleared, Posting, PostingOrigin, PostingType, Transaction, TransactionStatus, TransactionType,
};

use crate::filter::filter_expression;
Expand All @@ -21,6 +21,7 @@ use crate::parser::ParsedLedger;
use crate::parser::{tokenizers, value_expr};
use crate::{Error, List};
use num::BigInt;
use std::cell::RefCell;
use std::rc::Rc;

mod account;
Expand Down Expand Up @@ -61,7 +62,7 @@ impl ParsedLedger {
// 1. Populate the directive lists

for transaction in self.transactions.iter() {
for p in transaction.postings.iter() {
for p in transaction.postings.borrow().iter() {
account_strs.insert(p.account.clone());
if let Some(payee) = p.payee.clone() {
payee_strs.insert(payee);
Expand Down Expand Up @@ -212,17 +213,11 @@ impl ParsedLedger {
for t in transactions.iter_mut() {
for automated in automated_transactions.iter_mut() {
let mut extra_postings = vec![];
let mut extra_virtual_postings = vec![];
let mut extra_virtual_postings_balance = vec![];
let mut matched = false;

for comment in automated.comments.iter() {
t.tags.append(&mut comment.get_tags());
for p in t.postings.iter_mut() {
p.tags.append(&mut comment.get_tags());
for p in t.postings.borrow().iter() {
if p.origin != PostingOrigin::FromTransaction {
continue;
}
}
for p in t.postings_iter() {
let node = root_nodes.get(automated.get_filter_query().as_str());
if filter_expression(
node.unwrap(), // automated.get_filter_query().as_str(),
Expand All @@ -231,9 +226,11 @@ impl ParsedLedger {
&mut self.commodities,
&mut regexes,
)? {
matched = true;
for comment in automated.comments.iter() {
p.tags.borrow_mut().append(&mut comment.get_tags());
}

for auto_posting in automated.postings_iter() {
for auto_posting in automated.postings.borrow().iter() {
let account_alias = auto_posting.account.clone();
match self.accounts.get(&account_alias) {
Ok(_) => {} // do nothing
Expand Down Expand Up @@ -290,27 +287,20 @@ impl ParsedLedger {
cost: None,
kind: auto_posting.kind,
comments: vec![],
tags: vec![],
tags: RefCell::new(vec![]),
payee,
transaction: RefCell::new(Rc::downgrade(&Rc::new(t.clone()))),
origin: PostingOrigin::Automated,
};

match auto_posting.kind {
PostingType::Real => extra_postings.push(posting),
PostingType::Virtual => extra_virtual_postings.push(posting),
PostingType::VirtualMustBalance => {
extra_virtual_postings_balance.push(posting)
}
}
extra_postings.push(posting);
}
}
}
t.postings.append(&mut extra_postings);
t.virtual_postings.append(&mut extra_virtual_postings);
t.virtual_postings_balance
.append(&mut extra_virtual_postings_balance);
if matched {
break;
}
t.postings.borrow_mut().append(&mut extra_postings);
// if matched {
// break;
// }
}
}
// Populate balances
Expand Down Expand Up @@ -379,7 +369,7 @@ impl ParsedLedger {
transaction.tags.append(&mut comment.get_tags());
}
// Go posting by posting
for p in parsed.postings.iter() {
for p in parsed.postings.borrow().iter() {
let payee = match &p.payee {
None => transaction.get_payee_inmutable(&self.payees),
Some(x) => self.payees.get(x).unwrap().clone(),
Expand All @@ -401,10 +391,11 @@ impl ParsedLedger {
} else {
self.accounts.get(&p.account)?.clone()
};
let mut posting: Posting = Posting::new(&account, p.kind, &payee);
posting.tags = transaction.tags.clone();
let mut posting: Posting =
Posting::new(&account, p.kind, &payee, PostingOrigin::FromTransaction);
posting.tags = RefCell::new(transaction.tags.clone());
for comment in p.comments.iter() {
posting.tags.append(&mut comment.get_tags());
posting.tags.borrow_mut().append(&mut comment.get_tags());
}

// Modify posting with amounts
Expand Down Expand Up @@ -452,15 +443,7 @@ impl ParsedLedger {
p.balance_amount.clone().unwrap(),
)));
}
match posting.kind {
PostingType::Real => transaction.postings.push(posting.to_owned()),
PostingType::Virtual => {
transaction.virtual_postings.push(posting.to_owned())
}
PostingType::VirtualMustBalance => transaction
.virtual_postings_balance
.push(posting.to_owned()),
}
transaction.postings.borrow_mut().push(posting.to_owned());
}
match transaction.clone().is_balanced() {
true => {
Expand Down
2 changes: 1 addition & 1 deletion src/models/price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ mod tests {
let mut balances: HashMap<Rc<Account>, Balance> = HashMap::new();

for t in ledger.transactions.iter() {
for p in t.postings_iter() {
for p in t.postings.borrow().iter() {
let mut cur_bal = balances
.get(p.account.deref())
.unwrap_or(&Balance::new())
Expand Down
Loading

0 comments on commit 17ff6fc

Please sign in to comment.