From 8e7a9036692e53348789b046f92288bf39b34fcc Mon Sep 17 00:00:00 2001 From: scoobybejesus Date: Sun, 6 Dec 2020 22:47:34 -0500 Subject: [PATCH] Made function params more concise. --- crptls/src/core_functions.rs | 8 +++++--- crptls/src/csv_import_accts_txns.rs | 20 ++++++++++---------- crptls/src/import_cost_proceeds_etc.rs | 24 ++++++++++++------------ 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/crptls/src/core_functions.rs b/crptls/src/core_functions.rs index 8ab71e5..7e5c0cc 100644 --- a/crptls/src/core_functions.rs +++ b/crptls/src/core_functions.rs @@ -53,7 +53,8 @@ pub fn import_and_process_final( csv_import_accts_txns::import_from_csv( input_file_path, - settings, + settings.input_file_uses_iso_date_style, + &settings.input_file_date_separator, &mut raw_account_map, &mut account_map, &mut action_records_map, @@ -75,7 +76,7 @@ pub fn import_and_process_final( println!(" Created lots and movements."); import_cost_proceeds_etc::add_cost_basis_to_movements( - &settings, + &settings.home_currency, &raw_account_map, &account_map, &action_records_map, @@ -98,7 +99,8 @@ pub fn import_and_process_final( println!(" Applying like-kind treatment through cut-off date: {}.", settings.lk_cutoff_date); import_cost_proceeds_etc::apply_like_kind_treatment( - &settings, + &settings.home_currency, + settings.lk_cutoff_date, &raw_account_map, &account_map, &action_records_map, diff --git a/crptls/src/csv_import_accts_txns.rs b/crptls/src/csv_import_accts_txns.rs index 56c5c02..db225a7 100644 --- a/crptls/src/csv_import_accts_txns.rs +++ b/crptls/src/csv_import_accts_txns.rs @@ -11,7 +11,6 @@ use std::path::PathBuf; use chrono::NaiveDate; use decimal::d128; -use crate::core_functions::{ImportProcessParameters}; use crate::transaction::{Transaction, ActionRecord}; use crate::account::{Account, RawAccount}; use crate::decimal_utils::{round_d128_1e8}; @@ -19,7 +18,8 @@ use crate::decimal_utils::{round_d128_1e8}; pub fn import_from_csv( import_file_path: PathBuf, - settings: &ImportProcessParameters, + iso_date_style: bool, + separator: &String, raw_acct_map: &mut HashMap, acct_map: &mut HashMap, action_records: &mut HashMap, @@ -46,7 +46,8 @@ pub fn import_from_csv( import_transactions( &mut rdr, - settings, + iso_date_style, + &separator, action_records, transactions_map, )?; @@ -141,7 +142,8 @@ The next column's value should be 2, then 3, etc, until the final account)."; fn import_transactions( rdr: &mut csv::Reader, - settings: &ImportProcessParameters, + iso_date_style: bool, + separator: &String, action_records: &mut HashMap, txns_map: &mut HashMap, ) -> Result<(), Box> { @@ -230,9 +232,6 @@ fn import_transactions( let format_yy: String; let format_yyyy: String; - let iso_date_style = settings.input_file_uses_iso_date_style; - let separator = &settings.input_file_date_separator; - if iso_date_style { format_yyyy = "%Y".to_owned() + separator + "%m" + separator + "%d"; format_yy = "%y".to_owned() + separator + "%m" + separator + "%d"; @@ -244,8 +243,9 @@ fn import_transactions( let tx_date = NaiveDate::parse_from_str(this_tx_date, &format_yy) .unwrap_or_else(|_| NaiveDate::parse_from_str(this_tx_date, &format_yyyy) .expect(" -Failed to parse date in input file. Check date the separator character, which is expected to be a hyphen \ - unless otherwise set via environment variable or .env file. See `.env.example.`\n") +Failed to parse date in input file. Confirm your choice of the separator character, which is expected to be a hyphen \ + unless otherwise set via command line flag, environment variable or .env file. Also confirm your choice of dating format \ + whether it be American (%m-%d-%y) or ISO (%y-%m-%d). Run with `--help` or see `.env.example.`\n") ); let transaction = Transaction { @@ -261,7 +261,7 @@ Failed to parse date in input file. Check date the separator character, which is }; if changed_action_records > 0 { - println!(" Changed actionrecord amounts: {}. Changed txn numbers: {:?}.", changed_action_records, changed_txn_num); + println!(" Changed actionrecord amounts due to rounding precision: {}. Changed txn numbers: {:?}.", changed_action_records, changed_txn_num); } Ok(()) diff --git a/crptls/src/import_cost_proceeds_etc.rs b/crptls/src/import_cost_proceeds_etc.rs index 28dbcdf..453f188 100644 --- a/crptls/src/import_cost_proceeds_etc.rs +++ b/crptls/src/import_cost_proceeds_etc.rs @@ -4,15 +4,15 @@ use std::collections::{HashMap}; use std::error::Error; +use chrono::NaiveDate; use decimal::d128; use crate::transaction::{Transaction, TxType, ActionRecord, Polarity}; use crate::account::{Account, RawAccount}; use crate::decimal_utils::{round_d128_1e2}; -use crate::core_functions::{ImportProcessParameters}; pub(crate) fn add_cost_basis_to_movements( - settings: &ImportProcessParameters, + home_currency: &String, raw_acct_map: &HashMap, acct_map: &HashMap, ars: &HashMap, @@ -37,7 +37,7 @@ pub(crate) fn add_cost_basis_to_movements( let polarity = ar.direction(); let tx_type = txn.transaction_type(ars, raw_acct_map, acct_map)?; - let is_home_curr = raw_acct.is_home_currency(&settings.home_currency); + let is_home_curr = raw_acct.is_home_currency(home_currency); let mvmt_copy = mvmt.clone(); let borrowed_mvmt = mvmt_copy.clone(); // println!("Txn: {} on {} of type: {:?}", @@ -90,7 +90,7 @@ pub(crate) fn add_cost_basis_to_movements( let other_acct = acct_map.get(&other_ar.account_key).unwrap(); let raw_other_acct = raw_acct_map.get(&other_acct.raw_key).unwrap(); assert_eq!(other_ar.direction(), Polarity::Outgoing); - let other_ar_is_home_curr = raw_other_acct.is_home_currency(&settings.home_currency); + let other_ar_is_home_curr = raw_other_acct.is_home_currency(home_currency); if other_ar_is_home_curr { mvmt.cost_basis.set(-(other_ar.amount)); @@ -265,7 +265,8 @@ pub(crate) fn add_proceeds_to_movements( } pub(crate) fn apply_like_kind_treatment( - settings: &ImportProcessParameters, + home_currency: &String, + cutoff_date: NaiveDate, raw_acct_map: &HashMap, acct_map: &HashMap, ars: &HashMap, @@ -273,16 +274,16 @@ pub(crate) fn apply_like_kind_treatment( ) -> Result<(), Box> { let length = txns_map.len(); - let cutoff_date = settings.lk_cutoff_date; + for txn_num in 1..=length { let txn_num = txn_num as u32; let txn = txns_map.get(&(txn_num)).unwrap(); - update_current_txn_for_prior_likekind_treatment(txn_num, &settings, &raw_acct_map, &acct_map, &ars, &txns_map)?; + update_current_txn_for_prior_likekind_treatment(txn_num, home_currency, &raw_acct_map, &acct_map, &ars, &txns_map)?; if txn.date <= cutoff_date { - perform_likekind_treatment_on_txn(txn_num, &settings, &raw_acct_map, &acct_map, &ars, &txns_map)?; + perform_likekind_treatment_on_txn(txn_num, home_currency, &raw_acct_map, &acct_map, &ars, &txns_map)?; } } @@ -291,7 +292,7 @@ pub(crate) fn apply_like_kind_treatment( fn update_current_txn_for_prior_likekind_treatment( txn_num: u32, - settings: &ImportProcessParameters, + home_currency: &String, raw_acct_map: &HashMap, acct_map: &HashMap, ars: &HashMap, @@ -312,7 +313,7 @@ fn update_current_txn_for_prior_likekind_treatment( let polarity = ar.direction(); let tx_type = txn.transaction_type(ars, raw_acct_map, acct_map)?; - let is_home_curr = raw_acct.is_home_currency(&settings.home_currency); + let is_home_curr = raw_acct.is_home_currency(home_currency); let mvmt_copy = mvmt.clone(); let borrowed_mvmt = mvmt_copy.clone(); @@ -381,7 +382,7 @@ fn update_current_txn_for_prior_likekind_treatment( fn perform_likekind_treatment_on_txn( txn_num: u32, - settings: &ImportProcessParameters, + home_currency: &String, raw_acct_map: &HashMap, acct_map: &HashMap, ars: &HashMap, @@ -390,7 +391,6 @@ fn perform_likekind_treatment_on_txn( let txn = txns_map.get(&txn_num).unwrap(); let tx_type = txn.transaction_type(ars, raw_acct_map, acct_map)?; - let home_currency = &settings.home_currency; match tx_type {