diff --git a/Cargo.toml b/Cargo.toml index 9a2cce4..0c4e748 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cryptools" -version = "0.8.4" +version = "0.8.5" authors = ["scoobybejesus "] edition = "2018" description = "Command-line utility for processing cryptocurrency transactions into 'lots' and 'movements'." diff --git a/src/crptls_lib/core_functions.rs b/src/crptls_lib/core_functions.rs index 3f74575..b994906 100644 --- a/src/crptls_lib/core_functions.rs +++ b/src/crptls_lib/core_functions.rs @@ -3,7 +3,7 @@ use std::path::PathBuf; use std::error::Error; -use std::fs::File; + use std::collections::{HashMap}; use chrono::NaiveDate; @@ -21,16 +21,16 @@ use crate::crptls_lib::costing_method::InventoryCostingMethod; /// `Account`s and `Transaction`s, creation of `Lot`s and `Movement`s, addition of cost basis and proceeds /// to `Movement`s, and application of like-kind treatment, in a specific and automated fashion. pub struct ImportProcessParameters { - pub export_path: PathBuf, + pub input_file_date_separator: String, + pub input_file_uses_iso_date_style: bool, pub home_currency: String, + pub costing_method: InventoryCostingMethod, pub lk_treatment_enabled: bool, /// NaiveDate either from "1-1-1" (default and not to be used) or the actual date chosen (or passed in via Cli option) pub lk_cutoff_date: NaiveDate, pub lk_basis_date_preserved: bool, - pub costing_method: InventoryCostingMethod, - pub input_file_date_separator: String, - pub input_file_has_iso_date_style: bool, pub should_export: bool, + pub export_path: PathBuf, pub print_menu: bool, pub journal_entry_export: bool, } @@ -39,8 +39,8 @@ pub fn import_and_process_final( input_file_path: PathBuf, settings: &ImportProcessParameters, ) -> Result<( - HashMap, HashMap, + HashMap, HashMap, HashMap, ), Box> { @@ -51,63 +51,23 @@ pub fn import_and_process_final( let mut account_map: HashMap = HashMap::new(); let mut _lot_map: HashMap<(RawAccount, u32), Lot> = HashMap::new(); - match import_from_csv( + csv_import_accts_txns::import_from_csv( input_file_path, - &mut transactions_map, - &mut action_records_map, + settings, &mut raw_account_map, &mut account_map, - &settings.input_file_date_separator, - settings.input_file_has_iso_date_style, - ) { - Ok(()) => { println!("Successfully imported csv file."); } - Err(err) => { - println!("\nFailed to import accounts and transactions from CSV."); - println!("{}", err); + &mut action_records_map, + &mut transactions_map, + )?; - return Err(err) - } - }; - - pub(crate) fn import_from_csv( - import_file_path: PathBuf, - transactions_map: &mut HashMap, - action_records: &mut HashMap, - raw_acct_map: &mut HashMap, - acct_map: &mut HashMap, - date_separator: &str, - iso_date_style: bool, - ) -> Result<(), Box> { - - let file = File::open(import_file_path)?; println!("CSV ledger file opened successfully.\n"); - - let mut rdr = csv::ReaderBuilder::new() - .has_headers(true) - .from_reader(file); - - csv_import_accts_txns::import_accounts(&mut rdr, raw_acct_map, acct_map)?; - - csv_import_accts_txns::import_transactions( - &mut rdr, - transactions_map, - action_records, - date_separator, - iso_date_style, - )?; - - Ok(()) - } + println!("Successfully imported csv file."); transactions_map = create_lots_mvmts::create_lots_and_movements( - transactions_map, - &action_records_map, + &settings, &raw_account_map, &account_map, - &settings.home_currency.as_str(), - &settings.costing_method, - settings.lk_treatment_enabled, - settings.lk_cutoff_date, - settings.lk_basis_date_preserved, + &action_records_map, + transactions_map, // &mut lot_map, )?; @@ -115,18 +75,18 @@ pub fn import_and_process_final( import_cost_proceeds_etc::add_cost_basis_to_movements( &settings, - &action_records_map, &raw_account_map, &account_map, + &action_records_map, &transactions_map )?; println!(" Successfully added cost basis to movements."); import_cost_proceeds_etc::add_proceeds_to_movements( - &action_records_map, &raw_account_map, &account_map, + &action_records_map, &transactions_map )?; @@ -139,14 +99,14 @@ pub fn import_and_process_final( import_cost_proceeds_etc::apply_like_kind_treatment( &settings, - &action_records_map, &raw_account_map, &account_map, + &action_records_map, &transactions_map )?; println!(" Successfully applied like-kind treatment."); } - Ok((account_map, raw_account_map, action_records_map, transactions_map)) + Ok((raw_account_map, account_map, action_records_map, transactions_map)) } diff --git a/src/crptls_lib/create_lots_mvmts.rs b/src/crptls_lib/create_lots_mvmts.rs index 2a1b568..3724fc4 100644 --- a/src/crptls_lib/create_lots_mvmts.rs +++ b/src/crptls_lib/create_lots_mvmts.rs @@ -7,26 +7,28 @@ use std::collections::{HashMap}; use std::error::Error; use decimal::d128; -use chrono::NaiveDate; +use crate::crptls_lib::core_functions::{ImportProcessParameters}; use crate::crptls_lib::transaction::{Transaction, ActionRecord, TxType, Polarity, TxHasMargin}; use crate::crptls_lib::account::{Account, RawAccount, Lot, Movement}; use crate::crptls_lib::costing_method::{InventoryCostingMethod}; use crate::crptls_lib::decimal_utils::{round_d128_1e8}; pub(crate) fn create_lots_and_movements( - txns_map: HashMap, - ar_map: &HashMap, + settings: &ImportProcessParameters, raw_acct_map: &HashMap, acct_map: &HashMap, - chosen_home_currency: &str, - chosen_costing_method: &InventoryCostingMethod, - enable_lk_treatment: bool, - like_kind_cutoff_date: NaiveDate, - lk_basis_date_preserved: bool, + ar_map: &HashMap, + txns_map: HashMap, // lot_map: &HashMap<(RawAccount, u32), Lot>, ) -> Result, Box> { + let chosen_home_currency = &settings.home_currency; + let chosen_costing_method = &settings.costing_method; + let enable_lk_treatment = settings.lk_treatment_enabled; + let like_kind_cutoff_date = settings.lk_cutoff_date; + let lk_basis_date_preserved = settings.lk_basis_date_preserved; + let multiple_incoming_mvmts_per_ar = lk_basis_date_preserved; // On with the creating of lots and movements. diff --git a/src/crptls_lib/csv_import_accts_txns.rs b/src/crptls_lib/csv_import_accts_txns.rs index 49dae13..165095a 100644 --- a/src/crptls_lib/csv_import_accts_txns.rs +++ b/src/crptls_lib/csv_import_accts_txns.rs @@ -6,16 +6,45 @@ use std::process; use std::fs::File; use std::cell::{RefCell}; use std::collections::{HashMap}; +use std::path::PathBuf; use chrono::NaiveDate; use decimal::d128; +use crate::crptls_lib::core_functions::{ImportProcessParameters}; use crate::crptls_lib::transaction::{Transaction, ActionRecord}; use crate::crptls_lib::account::{Account, RawAccount}; use crate::crptls_lib::decimal_utils::{round_d128_1e8}; -pub(crate) fn import_accounts( +pub(crate) fn import_from_csv( + import_file_path: PathBuf, + settings: &ImportProcessParameters, + raw_acct_map: &mut HashMap, + acct_map: &mut HashMap, + action_records: &mut HashMap, + transactions_map: &mut HashMap, +) -> Result<(), Box> { + + let file = File::open(import_file_path)?; println!("CSV ledger file opened successfully.\n"); + + let mut rdr = csv::ReaderBuilder::new() + .has_headers(true) + .from_reader(file); + + import_accounts(&mut rdr, raw_acct_map, acct_map)?; + + import_transactions( + &mut rdr, + settings, + action_records, + transactions_map, + )?; + + Ok(()) +} + +fn import_accounts( rdr: &mut csv::Reader, raw_acct_map: &mut HashMap, acct_map: &mut HashMap, @@ -107,12 +136,11 @@ The next column's value should be 2, then 3, etc, until the final account)."; Ok(()) } -pub(crate) fn import_transactions( +fn import_transactions( rdr: &mut csv::Reader, - txns_map: &mut HashMap, + settings: &ImportProcessParameters, action_records: &mut HashMap, - sep: &str, - iso: bool, + txns_map: &mut HashMap, ) -> Result<(), Box> { let mut this_tx_number = 0; @@ -198,12 +226,15 @@ pub(crate) fn import_transactions( let format_yy: String; let format_yyyy: String; - if iso { - format_yyyy = "%Y".to_owned() + sep + "%d" + sep + "%m"; - format_yy = "%y".to_owned() + sep + "%d" + sep + "%m"; + 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 + "%d" + separator + "%m"; + format_yy = "%y".to_owned() + separator + "%d" + separator + "%m"; } else { - format_yyyy = "%m".to_owned() + sep + "%d" + sep + "%Y"; - format_yy = "%m".to_owned() + sep + "%d" + sep + "%y"; + format_yyyy = "%m".to_owned() + separator + "%d" + separator + "%Y"; + format_yy = "%m".to_owned() + separator + "%d" + separator + "%y"; } let tx_date = NaiveDate::parse_from_str(this_tx_date, &format_yy) diff --git a/src/crptls_lib/import_cost_proceeds_etc.rs b/src/crptls_lib/import_cost_proceeds_etc.rs index f0fa05a..3f9338f 100644 --- a/src/crptls_lib/import_cost_proceeds_etc.rs +++ b/src/crptls_lib/import_cost_proceeds_etc.rs @@ -13,9 +13,9 @@ use crate::crptls_lib::core_functions::{ImportProcessParameters}; pub(crate) fn add_cost_basis_to_movements( settings: &ImportProcessParameters, - ars: &HashMap, raw_acct_map: &HashMap, acct_map: &HashMap, + ars: &HashMap, txns_map: &HashMap, ) -> Result<(), Box> { @@ -183,9 +183,9 @@ pub(crate) fn add_cost_basis_to_movements( } pub(crate) fn add_proceeds_to_movements( - ars: &HashMap, raw_acct_map: &HashMap, acct_map: &HashMap, + ars: &HashMap, txns_map: &HashMap, ) -> Result<(), Box> { @@ -266,9 +266,9 @@ pub(crate) fn add_proceeds_to_movements( pub(crate) fn apply_like_kind_treatment( settings: &ImportProcessParameters, - ars: &HashMap, raw_acct_map: &HashMap, acct_map: &HashMap, + ars: &HashMap, txns_map: &HashMap, ) -> Result<(), Box> { @@ -279,10 +279,10 @@ pub(crate) fn apply_like_kind_treatment( 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, &ars, &raw_acct_map, &acct_map, &txns_map)?; + update_current_txn_for_prior_likekind_treatment(txn_num, &settings, &raw_acct_map, &acct_map, &ars, &txns_map)?; if txn.date <= cutoff_date { - perform_likekind_treatment_on_txn(txn_num, &settings, &ars, &raw_acct_map, &acct_map, &txns_map)?; + perform_likekind_treatment_on_txn(txn_num, &settings, &raw_acct_map, &acct_map, &ars, &txns_map)?; } } @@ -292,9 +292,9 @@ pub(crate) fn apply_like_kind_treatment( fn update_current_txn_for_prior_likekind_treatment( txn_num: u32, settings: &ImportProcessParameters, - ars: &HashMap, raw_acct_map: &HashMap, acct_map: &HashMap, + ars: &HashMap, txns_map: &HashMap, ) -> Result<(), Box> { @@ -382,9 +382,9 @@ fn update_current_txn_for_prior_likekind_treatment( fn perform_likekind_treatment_on_txn( txn_num: u32, settings: &ImportProcessParameters, - ars: &HashMap, raw_acct_map: &HashMap, acct_map: &HashMap, + ars: &HashMap, txns_map: &HashMap, ) -> Result<(), Box> { diff --git a/src/export_all.rs b/src/export_all.rs index 449190d..ee5f231 100644 --- a/src/export_all.rs +++ b/src/export_all.rs @@ -14,9 +14,9 @@ use crate::export_je; pub fn export( settings: &ImportProcessParameters, - action_records_map: &HashMap, raw_acct_map: &HashMap, account_map: &HashMap, + action_records_map: &HashMap, transactions_map: &HashMap, ) -> Result<(), Box> { @@ -29,9 +29,9 @@ pub fn export( ); export_csv::_2_account_sums_nonzero_to_csv( - &account_map, &settings, - &raw_acct_map + &raw_acct_map, + &account_map, ); if settings.lk_treatment_enabled { @@ -44,25 +44,25 @@ pub fn export( export_csv::_4_transaction_mvmt_detail_to_csv( &settings, - &action_records_map, &raw_acct_map, &account_map, + &action_records_map, &transactions_map )?; export_csv::_5_transaction_mvmt_summaries_to_csv( &settings, - &action_records_map, &raw_acct_map, &account_map, + &action_records_map, &transactions_map )?; export_csv::_6_transaction_mvmt_detail_to_csv_w_orig( &settings, - &action_records_map, &raw_acct_map, &account_map, + &action_records_map, &transactions_map )?; @@ -70,8 +70,8 @@ pub fn export( &settings, &raw_acct_map, &account_map, + &action_records_map, &transactions_map, - &action_records_map )?; export_txt::_2_account_lot_summary_to_txt( @@ -89,9 +89,9 @@ pub fn export( if !settings.lk_treatment_enabled { export_je::prepare_non_lk_journal_entries( &settings, - &action_records_map, &raw_acct_map, &account_map, + &action_records_map, &transactions_map, )?; } diff --git a/src/export_csv.rs b/src/export_csv.rs index 2473301..cb686ea 100644 --- a/src/export_csv.rs +++ b/src/export_csv.rs @@ -87,9 +87,9 @@ pub fn _1_account_sums_to_csv( } pub fn _2_account_sums_nonzero_to_csv( - acct_map: &HashMap, settings: &ImportProcessParameters, - raw_acct_map: &HashMap + raw_acct_map: &HashMap, + acct_map: &HashMap, ) { let mut rows: Vec> = Vec::with_capacity(acct_map.len()); // more than needed... @@ -257,9 +257,9 @@ pub fn _3_account_sums_to_csv_with_orig_basis( pub fn _4_transaction_mvmt_detail_to_csv( settings: &ImportProcessParameters, - ars: &HashMap, raw_acct_map: &HashMap, acct_map: &HashMap, + ars: &HashMap, txns_map: &HashMap, ) -> Result<(), Box> { @@ -363,9 +363,9 @@ pub fn _4_transaction_mvmt_detail_to_csv( pub fn _5_transaction_mvmt_summaries_to_csv( settings: &ImportProcessParameters, - ars: &HashMap, raw_acct_map: &HashMap, acct_map: &HashMap, + ars: &HashMap, txns_map: &HashMap, ) -> Result<(), Box> { @@ -546,9 +546,9 @@ pub fn _5_transaction_mvmt_summaries_to_csv( pub fn _6_transaction_mvmt_detail_to_csv_w_orig( settings: &ImportProcessParameters, - ars: &HashMap, raw_acct_map: &HashMap, acct_map: &HashMap, + ars: &HashMap, txns_map: &HashMap, ) -> Result<(), Box> { diff --git a/src/export_je.rs b/src/export_je.rs index fa200df..0eea778 100644 --- a/src/export_je.rs +++ b/src/export_je.rs @@ -16,9 +16,9 @@ use crptls::core_functions::{ImportProcessParameters}; pub fn prepare_non_lk_journal_entries( settings: &ImportProcessParameters, - ars: &HashMap, raw_acct_map: &HashMap, acct_map: &HashMap, + ars: &HashMap, txns_map: &HashMap, ) -> Result<(), Box> { diff --git a/src/export_txt.rs b/src/export_txt.rs index d9d2ae8..b9d660d 100644 --- a/src/export_txt.rs +++ b/src/export_txt.rs @@ -18,8 +18,8 @@ pub fn _1_account_lot_detail_to_txt( settings: &ImportProcessParameters, raw_acct_map: &HashMap, acct_map: &HashMap, - txns_map: &HashMap, ars: &HashMap, + txns_map: &HashMap, ) -> Result<(), Box> { // ===================================== diff --git a/src/main.rs b/src/main.rs index e8e10ad..cd7476b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -126,8 +126,8 @@ fn main() -> Result<(), Box> { let (input_file_path, settings) = setup::run_setup(args)?; let ( - account_map, raw_acct_map, + account_map, action_records_map, transactions_map, ) = crptls::core_functions::import_and_process_final(input_file_path, &settings)?; @@ -143,9 +143,9 @@ fn main() -> Result<(), Box> { export_all::export( &settings, - &action_records_map, &raw_acct_map, &account_map, + &action_records_map, &transactions_map )?; } @@ -154,9 +154,9 @@ fn main() -> Result<(), Box> { export_je::prepare_non_lk_journal_entries( &settings, - &action_records_map, &raw_acct_map, &account_map, + &action_records_map, &transactions_map, )?; } @@ -165,9 +165,9 @@ fn main() -> Result<(), Box> { mytui::print_menu_tui::print_menu_tui( &settings, - &action_records_map, &raw_acct_map, &account_map, + &action_records_map, &transactions_map )?; } diff --git a/src/mytui/app.rs b/src/mytui/app.rs index cde934b..f70c1a6 100644 --- a/src/mytui/app.rs +++ b/src/mytui/app.rs @@ -129,9 +129,9 @@ impl<'a> PrintWindow<'a> { pub fn export( app: &PrintWindow, settings: &ImportProcessParameters, - action_records_map: &HashMap, raw_acct_map: &HashMap, account_map: &HashMap, + action_records_map: &HashMap, transactions_map: &HashMap, ) -> Result<(), Box> { @@ -154,9 +154,9 @@ pub fn export( } 2 => { export_csv::_2_account_sums_nonzero_to_csv( - &account_map, &settings, - &raw_acct_map + &raw_acct_map, + &account_map, ); } 3 => { @@ -169,27 +169,27 @@ pub fn export( 4 => { export_csv::_4_transaction_mvmt_detail_to_csv( &settings, - &action_records_map, &raw_acct_map, &account_map, + &action_records_map, &transactions_map )?; } 5 => { export_csv::_5_transaction_mvmt_summaries_to_csv( &settings, - &action_records_map, &raw_acct_map, &account_map, + &action_records_map, &transactions_map )?; } 6 => { export_csv::_6_transaction_mvmt_detail_to_csv_w_orig( &settings, - &action_records_map, &raw_acct_map, &account_map, + &action_records_map, &transactions_map )?; } @@ -198,8 +198,8 @@ pub fn export( &settings, &raw_acct_map, &account_map, + &action_records_map, &transactions_map, - &action_records_map )?; } 8 => { @@ -220,9 +220,9 @@ pub fn export( if !settings.lk_treatment_enabled { export_je::prepare_non_lk_journal_entries( &settings, - &action_records_map, &raw_acct_map, &account_map, + &action_records_map, &transactions_map, )?; } else { diff --git a/src/mytui/print_menu_tui.rs b/src/mytui/print_menu_tui.rs index 2016b42..973f8f9 100644 --- a/src/mytui/print_menu_tui.rs +++ b/src/mytui/print_menu_tui.rs @@ -24,9 +24,9 @@ use crate::mytui::app as app; pub (crate) fn print_menu_tui( settings: &ImportProcessParameters, - action_records_map: &HashMap, raw_acct_map: &HashMap, account_map: &HashMap, + action_records_map: &HashMap, transactions_map: &HashMap, ) -> Result<(), Box> { @@ -77,9 +77,9 @@ pub (crate) fn print_menu_tui( app::export( &app, &settings, - &action_records_map, &raw_acct_map, &account_map, + &action_records_map, &transactions_map )?; diff --git a/src/setup.rs b/src/setup.rs index a3e4b6c..8133e74 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -59,9 +59,9 @@ pub (crate) fn run_setup(args: super::Cli) -> Result<(PathBuf, ImportProcessPara } else { NaiveDate::parse_from_str(&"1-1-1", "%y-%m-%d").unwrap() }; let settings = ImportProcessParameters { - home_currency: args.opts.home_currency.into_string().unwrap().to_uppercase(), - input_file_has_iso_date_style: args.flags.iso_date, + input_file_uses_iso_date_style: args.flags.iso_date, input_file_date_separator: date_separator.to_string(), + home_currency: args.opts.home_currency.into_string().unwrap().to_uppercase(), costing_method: costing_method_choice, lk_treatment_enabled: like_kind_election, lk_cutoff_date: like_kind_cutoff_date,