Made function params more concise.

This commit is contained in:
scoobybejesus 2020-12-06 22:47:34 -05:00
parent 9891d14820
commit 8e7a903669
3 changed files with 27 additions and 25 deletions

View File

@ -53,7 +53,8 @@ pub fn import_and_process_final(
csv_import_accts_txns::import_from_csv( csv_import_accts_txns::import_from_csv(
input_file_path, input_file_path,
settings, settings.input_file_uses_iso_date_style,
&settings.input_file_date_separator,
&mut raw_account_map, &mut raw_account_map,
&mut account_map, &mut account_map,
&mut action_records_map, &mut action_records_map,
@ -75,7 +76,7 @@ pub fn import_and_process_final(
println!(" Created lots and movements."); println!(" Created lots and movements.");
import_cost_proceeds_etc::add_cost_basis_to_movements( import_cost_proceeds_etc::add_cost_basis_to_movements(
&settings, &settings.home_currency,
&raw_account_map, &raw_account_map,
&account_map, &account_map,
&action_records_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); println!(" Applying like-kind treatment through cut-off date: {}.", settings.lk_cutoff_date);
import_cost_proceeds_etc::apply_like_kind_treatment( import_cost_proceeds_etc::apply_like_kind_treatment(
&settings, &settings.home_currency,
settings.lk_cutoff_date,
&raw_account_map, &raw_account_map,
&account_map, &account_map,
&action_records_map, &action_records_map,

View File

@ -11,7 +11,6 @@ use std::path::PathBuf;
use chrono::NaiveDate; use chrono::NaiveDate;
use decimal::d128; use decimal::d128;
use crate::core_functions::{ImportProcessParameters};
use crate::transaction::{Transaction, ActionRecord}; use crate::transaction::{Transaction, ActionRecord};
use crate::account::{Account, RawAccount}; use crate::account::{Account, RawAccount};
use crate::decimal_utils::{round_d128_1e8}; use crate::decimal_utils::{round_d128_1e8};
@ -19,7 +18,8 @@ use crate::decimal_utils::{round_d128_1e8};
pub fn import_from_csv( pub fn import_from_csv(
import_file_path: PathBuf, import_file_path: PathBuf,
settings: &ImportProcessParameters, iso_date_style: bool,
separator: &String,
raw_acct_map: &mut HashMap<u16, RawAccount>, raw_acct_map: &mut HashMap<u16, RawAccount>,
acct_map: &mut HashMap<u16, Account>, acct_map: &mut HashMap<u16, Account>,
action_records: &mut HashMap<u32, ActionRecord>, action_records: &mut HashMap<u32, ActionRecord>,
@ -46,7 +46,8 @@ pub fn import_from_csv(
import_transactions( import_transactions(
&mut rdr, &mut rdr,
settings, iso_date_style,
&separator,
action_records, action_records,
transactions_map, transactions_map,
)?; )?;
@ -141,7 +142,8 @@ The next column's value should be 2, then 3, etc, until the final account).";
fn import_transactions( fn import_transactions(
rdr: &mut csv::Reader<File>, rdr: &mut csv::Reader<File>,
settings: &ImportProcessParameters, iso_date_style: bool,
separator: &String,
action_records: &mut HashMap<u32, ActionRecord>, action_records: &mut HashMap<u32, ActionRecord>,
txns_map: &mut HashMap<u32, Transaction>, txns_map: &mut HashMap<u32, Transaction>,
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
@ -230,9 +232,6 @@ fn import_transactions(
let format_yy: String; let format_yy: String;
let format_yyyy: 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 { if iso_date_style {
format_yyyy = "%Y".to_owned() + separator + "%m" + separator + "%d"; format_yyyy = "%Y".to_owned() + separator + "%m" + separator + "%d";
format_yy = "%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) let tx_date = NaiveDate::parse_from_str(this_tx_date, &format_yy)
.unwrap_or_else(|_| NaiveDate::parse_from_str(this_tx_date, &format_yyyy) .unwrap_or_else(|_| NaiveDate::parse_from_str(this_tx_date, &format_yyyy)
.expect(" .expect("
Failed to parse date in input file. Check date the separator character, which is expected to be a hyphen \ 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 environment variable or .env file. See `.env.example.`\n") 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 { 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 { 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(()) Ok(())

View File

@ -4,15 +4,15 @@
use std::collections::{HashMap}; use std::collections::{HashMap};
use std::error::Error; use std::error::Error;
use chrono::NaiveDate;
use decimal::d128; use decimal::d128;
use crate::transaction::{Transaction, TxType, ActionRecord, Polarity}; use crate::transaction::{Transaction, TxType, ActionRecord, Polarity};
use crate::account::{Account, RawAccount}; use crate::account::{Account, RawAccount};
use crate::decimal_utils::{round_d128_1e2}; use crate::decimal_utils::{round_d128_1e2};
use crate::core_functions::{ImportProcessParameters};
pub(crate) fn add_cost_basis_to_movements( pub(crate) fn add_cost_basis_to_movements(
settings: &ImportProcessParameters, home_currency: &String,
raw_acct_map: &HashMap<u16, RawAccount>, raw_acct_map: &HashMap<u16, RawAccount>,
acct_map: &HashMap<u16, Account>, acct_map: &HashMap<u16, Account>,
ars: &HashMap<u32, ActionRecord>, ars: &HashMap<u32, ActionRecord>,
@ -37,7 +37,7 @@ pub(crate) fn add_cost_basis_to_movements(
let polarity = ar.direction(); let polarity = ar.direction();
let tx_type = txn.transaction_type(ars, raw_acct_map, acct_map)?; 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 mvmt_copy = mvmt.clone();
let borrowed_mvmt = mvmt_copy.clone(); let borrowed_mvmt = mvmt_copy.clone();
// println!("Txn: {} on {} of type: {:?}", // 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 other_acct = acct_map.get(&other_ar.account_key).unwrap();
let raw_other_acct = raw_acct_map.get(&other_acct.raw_key).unwrap(); let raw_other_acct = raw_acct_map.get(&other_acct.raw_key).unwrap();
assert_eq!(other_ar.direction(), Polarity::Outgoing); 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 { if other_ar_is_home_curr {
mvmt.cost_basis.set(-(other_ar.amount)); 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( pub(crate) fn apply_like_kind_treatment(
settings: &ImportProcessParameters, home_currency: &String,
cutoff_date: NaiveDate,
raw_acct_map: &HashMap<u16, RawAccount>, raw_acct_map: &HashMap<u16, RawAccount>,
acct_map: &HashMap<u16, Account>, acct_map: &HashMap<u16, Account>,
ars: &HashMap<u32, ActionRecord>, ars: &HashMap<u32, ActionRecord>,
@ -273,16 +274,16 @@ pub(crate) fn apply_like_kind_treatment(
) -> Result<(), Box<dyn Error>> { ) -> Result<(), Box<dyn Error>> {
let length = txns_map.len(); let length = txns_map.len();
let cutoff_date = settings.lk_cutoff_date;
for txn_num in 1..=length { for txn_num in 1..=length {
let txn_num = txn_num as u32; let txn_num = txn_num as u32;
let txn = txns_map.get(&(txn_num)).unwrap(); 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 { 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( fn update_current_txn_for_prior_likekind_treatment(
txn_num: u32, txn_num: u32,
settings: &ImportProcessParameters, home_currency: &String,
raw_acct_map: &HashMap<u16, RawAccount>, raw_acct_map: &HashMap<u16, RawAccount>,
acct_map: &HashMap<u16, Account>, acct_map: &HashMap<u16, Account>,
ars: &HashMap<u32, ActionRecord>, ars: &HashMap<u32, ActionRecord>,
@ -312,7 +313,7 @@ fn update_current_txn_for_prior_likekind_treatment(
let polarity = ar.direction(); let polarity = ar.direction();
let tx_type = txn.transaction_type(ars, raw_acct_map, acct_map)?; 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 mvmt_copy = mvmt.clone();
let borrowed_mvmt = mvmt_copy.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( fn perform_likekind_treatment_on_txn(
txn_num: u32, txn_num: u32,
settings: &ImportProcessParameters, home_currency: &String,
raw_acct_map: &HashMap<u16, RawAccount>, raw_acct_map: &HashMap<u16, RawAccount>,
acct_map: &HashMap<u16, Account>, acct_map: &HashMap<u16, Account>,
ars: &HashMap<u32, ActionRecord>, ars: &HashMap<u32, ActionRecord>,
@ -390,7 +391,6 @@ fn perform_likekind_treatment_on_txn(
let txn = txns_map.get(&txn_num).unwrap(); let txn = txns_map.get(&txn_num).unwrap();
let tx_type = txn.transaction_type(ars, raw_acct_map, acct_map)?; let tx_type = txn.transaction_type(ars, raw_acct_map, acct_map)?;
let home_currency = &settings.home_currency;
match tx_type { match tx_type {