From b73552057d3bf0355d60e48f359a52641a9e9711 Mon Sep 17 00:00:00 2001 From: scoobybejesus Date: Tue, 27 Aug 2019 19:08:03 -0400 Subject: [PATCH] Added remaining error handling (result types) throughout core_functions. Made another tertiary change or two related to error handling. Resolves #27. --- src/core_functions.rs | 48 +++++++++++++++++++++++++++------ src/create_lots_mvmts.rs | 5 ++-- src/import_cost_proceeds_etc.rs | 45 ++++++++++++++++++++++++------- src/main.rs | 2 +- 4 files changed, 79 insertions(+), 21 deletions(-) diff --git a/src/core_functions.rs b/src/core_functions.rs index 4a6a6ae..bacc0aa 100644 --- a/src/core_functions.rs +++ b/src/core_functions.rs @@ -134,7 +134,7 @@ pub fn import_and_process_final( None }; - transactions_map = create_lots_mvmts::create_lots_and_movements( + transactions_map = match create_lots_mvmts::create_lots_and_movements( transactions_map, &settings, &likekind_settings, @@ -142,26 +142,50 @@ pub fn import_and_process_final( &mut raw_account_map, &mut account_map, &mut lot_map, - ); + ) { + Ok(txns_map) => {txns_map} + Err(err) => { + println!("\nFailed to add lots and movements to transactions hashmap."); + println!("{}", err); + + return Err(err) + } + }; println!(" Successfully created lots and movements."); - import_cost_proceeds_etc::add_cost_basis_to_movements( + match import_cost_proceeds_etc::add_cost_basis_to_movements( &settings, &action_records_map, &raw_account_map, &account_map, &transactions_map - ); + ) { + Ok(()) => {} + Err(err) => { + println!("\nFailed to add cost basis to movements."); + println!("{}", err); + + return Err(err) + } + }; println!(" Successfully added cost basis to movements."); - import_cost_proceeds_etc::add_proceeds_to_movements( + match import_cost_proceeds_etc::add_proceeds_to_movements( &action_records_map, &raw_account_map, &account_map, &transactions_map - ); + ) { + Ok(()) => {} + Err(err) => { + println!("\nFailed to add proceeds to movements."); + println!("{}", err); + + return Err(err) + } + }; println!(" Successfully added proceeds to movements."); @@ -171,14 +195,22 @@ pub fn import_and_process_final( let cutoff_date = lk_settings.like_kind_cutoff_date; println!(" Applying like-kind treatment for cut-off date: {}.", cutoff_date); - import_cost_proceeds_etc::apply_like_kind_treatment( + match import_cost_proceeds_etc::apply_like_kind_treatment( cutoff_date, &settings, &action_records_map, &raw_account_map, &account_map, &transactions_map - ); + ) { + Ok(()) => {} + Err(err) => { + println!("\nFailed to apply like-kind treatment to movements."); + println!("{}", err); + + return Err(err) + } + }; println!(" Successfully applied like-kind treatment."); }; diff --git a/src/create_lots_mvmts.rs b/src/create_lots_mvmts.rs index 107d3ae..52e5dcf 100644 --- a/src/create_lots_mvmts.rs +++ b/src/create_lots_mvmts.rs @@ -4,6 +4,7 @@ use std::rc::{Rc}; use std::cell::{RefCell, Ref, Cell}; use std::collections::{HashMap}; +use std::error::Error; use decimal::d128; use chrono::NaiveDate; @@ -21,7 +22,7 @@ pub fn create_lots_and_movements( raw_acct_map: &HashMap, acct_map: &HashMap, lot_map: &HashMap<(RawAccount, u32), Lot>, -) -> HashMap { +) -> Result, Box> { // Set values to be referred to repeatedly, potentially, in Incoming Exchange transactions let multiple_incoming_mvmts_per_ar = match &likekind_settings { @@ -569,7 +570,7 @@ pub fn create_lots_and_movements( } // end for ar in txn.actionrecords } // end of tx does not have marginness of TwoARs } // end for txn in transactions - txns_map + Ok(txns_map) } fn get_base_and_quote_acct_for_dual_actionrecord_flow_tx( diff --git a/src/import_cost_proceeds_etc.rs b/src/import_cost_proceeds_etc.rs index 53df3b7..5b0078c 100644 --- a/src/import_cost_proceeds_etc.rs +++ b/src/import_cost_proceeds_etc.rs @@ -2,6 +2,7 @@ // Redistributions must include the license: https://github.com/scoobybejesus/cryptools-rs/blob/master/LEGAL.txt use std::collections::{HashMap}; +use std::error::Error; use chrono::NaiveDate; use decimal::d128; @@ -17,7 +18,7 @@ pub fn add_cost_basis_to_movements( raw_acct_map: &HashMap, acct_map: &HashMap, txns_map: &HashMap, -) { +) -> Result<(), Box> { let length = txns_map.len(); @@ -142,6 +143,8 @@ pub fn add_cost_basis_to_movements( basis }; + + Ok(()) } pub fn add_proceeds_to_movements( @@ -149,7 +152,7 @@ pub fn add_proceeds_to_movements( raw_acct_map: &HashMap, acct_map: &HashMap, txns_map: &HashMap, -) { +) -> Result<(), Box> { let length = txns_map.len(); @@ -195,6 +198,8 @@ pub fn add_proceeds_to_movements( } } } + + Ok(()) } pub fn apply_like_kind_treatment( @@ -204,7 +209,7 @@ pub fn apply_like_kind_treatment( raw_acct_map: &HashMap, acct_map: &HashMap, txns_map: &HashMap, -) { +) -> Result<(), Box> { let length = txns_map.len(); for txn_num in 1..=length { @@ -212,12 +217,30 @@ pub 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); + match update_current_txn_for_prior_likekind_treatment(txn_num, &settings, &ars, &raw_acct_map, &acct_map, &txns_map) { + Ok(()) => {} + Err(err) => { + println!("\nFailed to update current transaction for prior like-kind treatment."); + println!("{}", err); + + return Err(err) + } + }; if txn.date <= cutoff_date { - perform_likekind_treatment_on_txn(txn_num, &settings, &ars, &raw_acct_map, &acct_map, &txns_map); + match perform_likekind_treatment_on_txn(txn_num, &settings, &ars, &raw_acct_map, &acct_map, &txns_map) { + Ok(()) => {} + Err(err) => { + println!("\nFailed to perform like-kind treatment on transaction."); + println!("{}", err); + + return Err(err) + } + }; } } + + Ok(()) } fn update_current_txn_for_prior_likekind_treatment( @@ -227,7 +250,7 @@ fn update_current_txn_for_prior_likekind_treatment( raw_acct_map: &HashMap, acct_map: &HashMap, txns_map: &HashMap, -) { +) -> Result<(), Box> { let mut sum_of_outgoing_cost_basis_in_ar = d128!(0); let txn = txns_map.get(&txn_num).unwrap(); @@ -282,6 +305,8 @@ fn update_current_txn_for_prior_likekind_treatment( } } } + + Ok(()) } fn perform_likekind_treatment_on_txn( @@ -291,7 +316,7 @@ fn perform_likekind_treatment_on_txn( raw_acct_map: &HashMap, acct_map: &HashMap, txns_map: &HashMap, -) { +) -> Result<(), Box> { let txn = txns_map.get(&txn_num).unwrap(); let tx_type = txn.transaction_type(ars, raw_acct_map, acct_map); @@ -366,8 +391,8 @@ fn perform_likekind_treatment_on_txn( } } } - TxType::ToSelf => { - return - } + TxType::ToSelf => {} } + + Ok(()) } diff --git a/src/main.rs b/src/main.rs index 199f8fc..9180287 100644 --- a/src/main.rs +++ b/src/main.rs @@ -70,7 +70,7 @@ struct Cli { } -fn main() -> Result<(), Box> { +fn main() -> Result<(), Box> { let args = Cli::from_args();