Added remaining error handling (result types) throughout core_functions. Made another tertiary change or two related to error handling. Resolves #27.

This commit is contained in:
scoobybejesus 2019-08-27 19:08:03 -04:00
parent 067baa26e6
commit b73552057d
4 changed files with 79 additions and 21 deletions

View File

@ -134,7 +134,7 @@ pub fn import_and_process_final(
None None
}; };
transactions_map = create_lots_mvmts::create_lots_and_movements( transactions_map = match create_lots_mvmts::create_lots_and_movements(
transactions_map, transactions_map,
&settings, &settings,
&likekind_settings, &likekind_settings,
@ -142,26 +142,50 @@ pub fn import_and_process_final(
&mut raw_account_map, &mut raw_account_map,
&mut account_map, &mut account_map,
&mut lot_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."); 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, &settings,
&action_records_map, &action_records_map,
&raw_account_map, &raw_account_map,
&account_map, &account_map,
&transactions_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."); 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, &action_records_map,
&raw_account_map, &raw_account_map,
&account_map, &account_map,
&transactions_map &transactions_map
); ) {
Ok(()) => {}
Err(err) => {
println!("\nFailed to add proceeds to movements.");
println!("{}", err);
return Err(err)
}
};
println!(" Successfully added proceeds to movements."); 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; let cutoff_date = lk_settings.like_kind_cutoff_date;
println!(" Applying like-kind treatment for cut-off date: {}.", 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, cutoff_date,
&settings, &settings,
&action_records_map, &action_records_map,
&raw_account_map, &raw_account_map,
&account_map, &account_map,
&transactions_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."); println!(" Successfully applied like-kind treatment.");
}; };

View File

@ -4,6 +4,7 @@
use std::rc::{Rc}; use std::rc::{Rc};
use std::cell::{RefCell, Ref, Cell}; use std::cell::{RefCell, Ref, Cell};
use std::collections::{HashMap}; use std::collections::{HashMap};
use std::error::Error;
use decimal::d128; use decimal::d128;
use chrono::NaiveDate; use chrono::NaiveDate;
@ -21,7 +22,7 @@ pub fn create_lots_and_movements(
raw_acct_map: &HashMap<u16, RawAccount>, raw_acct_map: &HashMap<u16, RawAccount>,
acct_map: &HashMap<u16, Account>, acct_map: &HashMap<u16, Account>,
lot_map: &HashMap<(RawAccount, u32), Lot>, lot_map: &HashMap<(RawAccount, u32), Lot>,
) -> HashMap<u32,Transaction> { ) -> Result<HashMap<u32,Transaction>, Box<Error>> {
// Set values to be referred to repeatedly, potentially, in Incoming Exchange transactions // Set values to be referred to repeatedly, potentially, in Incoming Exchange transactions
let multiple_incoming_mvmts_per_ar = match &likekind_settings { 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 for ar in txn.actionrecords
} // end of tx does not have marginness of TwoARs } // end of tx does not have marginness of TwoARs
} // end for txn in transactions } // end for txn in transactions
txns_map Ok(txns_map)
} }
fn get_base_and_quote_acct_for_dual_actionrecord_flow_tx( fn get_base_and_quote_acct_for_dual_actionrecord_flow_tx(

View File

@ -2,6 +2,7 @@
// Redistributions must include the license: https://github.com/scoobybejesus/cryptools-rs/blob/master/LEGAL.txt // Redistributions must include the license: https://github.com/scoobybejesus/cryptools-rs/blob/master/LEGAL.txt
use std::collections::{HashMap}; use std::collections::{HashMap};
use std::error::Error;
use chrono::NaiveDate; use chrono::NaiveDate;
use decimal::d128; use decimal::d128;
@ -17,7 +18,7 @@ pub fn add_cost_basis_to_movements(
raw_acct_map: &HashMap<u16, RawAccount>, raw_acct_map: &HashMap<u16, RawAccount>,
acct_map: &HashMap<u16, Account>, acct_map: &HashMap<u16, Account>,
txns_map: &HashMap<u32, Transaction>, txns_map: &HashMap<u32, Transaction>,
) { ) -> Result<(), Box<Error>> {
let length = txns_map.len(); let length = txns_map.len();
@ -142,6 +143,8 @@ pub fn add_cost_basis_to_movements(
basis basis
}; };
Ok(())
} }
pub fn add_proceeds_to_movements( pub fn add_proceeds_to_movements(
@ -149,7 +152,7 @@ pub fn add_proceeds_to_movements(
raw_acct_map: &HashMap<u16, RawAccount>, raw_acct_map: &HashMap<u16, RawAccount>,
acct_map: &HashMap<u16, Account>, acct_map: &HashMap<u16, Account>,
txns_map: &HashMap<u32, Transaction>, txns_map: &HashMap<u32, Transaction>,
) { ) -> Result<(), Box<Error>> {
let length = txns_map.len(); let length = txns_map.len();
@ -195,6 +198,8 @@ pub fn add_proceeds_to_movements(
} }
} }
} }
Ok(())
} }
pub fn apply_like_kind_treatment( pub fn apply_like_kind_treatment(
@ -204,7 +209,7 @@ pub fn apply_like_kind_treatment(
raw_acct_map: &HashMap<u16, RawAccount>, raw_acct_map: &HashMap<u16, RawAccount>,
acct_map: &HashMap<u16, Account>, acct_map: &HashMap<u16, Account>,
txns_map: &HashMap<u32, Transaction>, txns_map: &HashMap<u32, Transaction>,
) { ) -> Result<(), Box<Error>> {
let length = txns_map.len(); let length = txns_map.len();
for txn_num in 1..=length { 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_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, &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 { 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( fn update_current_txn_for_prior_likekind_treatment(
@ -227,7 +250,7 @@ fn update_current_txn_for_prior_likekind_treatment(
raw_acct_map: &HashMap<u16, RawAccount>, raw_acct_map: &HashMap<u16, RawAccount>,
acct_map: &HashMap<u16, Account>, acct_map: &HashMap<u16, Account>,
txns_map: &HashMap<u32, Transaction>, txns_map: &HashMap<u32, Transaction>,
) { ) -> Result<(), Box<Error>> {
let mut sum_of_outgoing_cost_basis_in_ar = d128!(0); let mut sum_of_outgoing_cost_basis_in_ar = d128!(0);
let txn = txns_map.get(&txn_num).unwrap(); 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( fn perform_likekind_treatment_on_txn(
@ -291,7 +316,7 @@ fn perform_likekind_treatment_on_txn(
raw_acct_map: &HashMap<u16, RawAccount>, raw_acct_map: &HashMap<u16, RawAccount>,
acct_map: &HashMap<u16, Account>, acct_map: &HashMap<u16, Account>,
txns_map: &HashMap<u32, Transaction>, txns_map: &HashMap<u32, Transaction>,
) { ) -> Result<(), Box<Error>> {
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);
@ -366,8 +391,8 @@ fn perform_likekind_treatment_on_txn(
} }
} }
} }
TxType::ToSelf => { TxType::ToSelf => {}
return
}
} }
Ok(())
} }

View File

@ -70,7 +70,7 @@ struct Cli {
} }
fn main() -> Result<(), Box<dyn std::error::Error>> { fn main() -> Result<(), Box<dyn Error>> {
let args = Cli::from_args(); let args = Cli::from_args();