Minor refactoring and renaming to clean things up.

This commit is contained in:
scoobybejesus 2019-12-07 13:27:15 -05:00
parent c15420643a
commit 0af550cee6
13 changed files with 109 additions and 116 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "cryptools"
version = "0.8.4"
version = "0.8.5"
authors = ["scoobybejesus <scoobybejesus@users.noreply.github.com>"]
edition = "2018"
description = "Command-line utility for processing cryptocurrency transactions into 'lots' and 'movements'."

View File

@ -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<u16, Account>,
HashMap<u16, RawAccount>,
HashMap<u16, Account>,
HashMap<u32, ActionRecord>,
HashMap<u32, Transaction>,
), Box<dyn Error>> {
@ -51,63 +51,23 @@ pub fn import_and_process_final(
let mut account_map: HashMap<u16, Account> = 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<u32, Transaction>,
action_records: &mut HashMap<u32, ActionRecord>,
raw_acct_map: &mut HashMap<u16, RawAccount>,
acct_map: &mut HashMap<u16, Account>,
date_separator: &str,
iso_date_style: bool,
) -> Result<(), Box<dyn Error>> {
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))
}

View File

@ -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<u32, Transaction>,
ar_map: &HashMap<u32, ActionRecord>,
settings: &ImportProcessParameters,
raw_acct_map: &HashMap<u16, RawAccount>,
acct_map: &HashMap<u16, Account>,
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<u32, ActionRecord>,
txns_map: HashMap<u32, Transaction>,
// lot_map: &HashMap<(RawAccount, u32), Lot>,
) -> Result<HashMap<u32,Transaction>, Box<dyn Error>> {
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.

View File

@ -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<u16, RawAccount>,
acct_map: &mut HashMap<u16, Account>,
action_records: &mut HashMap<u32, ActionRecord>,
transactions_map: &mut HashMap<u32, Transaction>,
) -> Result<(), Box<dyn Error>> {
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<File>,
raw_acct_map: &mut HashMap<u16, RawAccount>,
acct_map: &mut HashMap<u16, Account>,
@ -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<File>,
txns_map: &mut HashMap<u32, Transaction>,
settings: &ImportProcessParameters,
action_records: &mut HashMap<u32, ActionRecord>,
sep: &str,
iso: bool,
txns_map: &mut HashMap<u32, Transaction>,
) -> Result<(), Box<dyn Error>> {
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)

View File

@ -13,9 +13,9 @@ use crate::crptls_lib::core_functions::{ImportProcessParameters};
pub(crate) fn add_cost_basis_to_movements(
settings: &ImportProcessParameters,
ars: &HashMap<u32, ActionRecord>,
raw_acct_map: &HashMap<u16, RawAccount>,
acct_map: &HashMap<u16, Account>,
ars: &HashMap<u32, ActionRecord>,
txns_map: &HashMap<u32, Transaction>,
) -> Result<(), Box<dyn Error>> {
@ -183,9 +183,9 @@ pub(crate) fn add_cost_basis_to_movements(
}
pub(crate) fn add_proceeds_to_movements(
ars: &HashMap<u32, ActionRecord>,
raw_acct_map: &HashMap<u16, RawAccount>,
acct_map: &HashMap<u16, Account>,
ars: &HashMap<u32, ActionRecord>,
txns_map: &HashMap<u32, Transaction>,
) -> Result<(), Box<dyn Error>> {
@ -266,9 +266,9 @@ pub(crate) fn add_proceeds_to_movements(
pub(crate) fn apply_like_kind_treatment(
settings: &ImportProcessParameters,
ars: &HashMap<u32, ActionRecord>,
raw_acct_map: &HashMap<u16, RawAccount>,
acct_map: &HashMap<u16, Account>,
ars: &HashMap<u32, ActionRecord>,
txns_map: &HashMap<u32, Transaction>,
) -> Result<(), Box<dyn Error>> {
@ -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<u32, ActionRecord>,
raw_acct_map: &HashMap<u16, RawAccount>,
acct_map: &HashMap<u16, Account>,
ars: &HashMap<u32, ActionRecord>,
txns_map: &HashMap<u32, Transaction>,
) -> Result<(), Box<dyn Error>> {
@ -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<u32, ActionRecord>,
raw_acct_map: &HashMap<u16, RawAccount>,
acct_map: &HashMap<u16, Account>,
ars: &HashMap<u32, ActionRecord>,
txns_map: &HashMap<u32, Transaction>,
) -> Result<(), Box<dyn Error>> {

View File

@ -14,9 +14,9 @@ use crate::export_je;
pub fn export(
settings: &ImportProcessParameters,
action_records_map: &HashMap<u32, ActionRecord>,
raw_acct_map: &HashMap<u16, RawAccount>,
account_map: &HashMap<u16, Account>,
action_records_map: &HashMap<u32, ActionRecord>,
transactions_map: &HashMap<u32, Transaction>,
) -> Result<(), Box<dyn Error>> {
@ -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,
)?;
}

View File

@ -87,9 +87,9 @@ pub fn _1_account_sums_to_csv(
}
pub fn _2_account_sums_nonzero_to_csv(
acct_map: &HashMap<u16, Account>,
settings: &ImportProcessParameters,
raw_acct_map: &HashMap<u16, RawAccount>
raw_acct_map: &HashMap<u16, RawAccount>,
acct_map: &HashMap<u16, Account>,
) {
let mut rows: Vec<Vec<String>> = 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<u32, ActionRecord>,
raw_acct_map: &HashMap<u16, RawAccount>,
acct_map: &HashMap<u16, Account>,
ars: &HashMap<u32, ActionRecord>,
txns_map: &HashMap<u32, Transaction>,
) -> Result<(), Box<dyn Error>> {
@ -363,9 +363,9 @@ pub fn _4_transaction_mvmt_detail_to_csv(
pub fn _5_transaction_mvmt_summaries_to_csv(
settings: &ImportProcessParameters,
ars: &HashMap<u32, ActionRecord>,
raw_acct_map: &HashMap<u16, RawAccount>,
acct_map: &HashMap<u16, Account>,
ars: &HashMap<u32, ActionRecord>,
txns_map: &HashMap<u32, Transaction>,
) -> Result<(), Box<dyn Error>> {
@ -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<u32, ActionRecord>,
raw_acct_map: &HashMap<u16, RawAccount>,
acct_map: &HashMap<u16, Account>,
ars: &HashMap<u32, ActionRecord>,
txns_map: &HashMap<u32, Transaction>,
) -> Result<(), Box<dyn Error>> {

View File

@ -16,9 +16,9 @@ use crptls::core_functions::{ImportProcessParameters};
pub fn prepare_non_lk_journal_entries(
settings: &ImportProcessParameters,
ars: &HashMap<u32, ActionRecord>,
raw_acct_map: &HashMap<u16, RawAccount>,
acct_map: &HashMap<u16, Account>,
ars: &HashMap<u32, ActionRecord>,
txns_map: &HashMap<u32, Transaction>,
) -> Result<(), Box<dyn Error>> {

View File

@ -18,8 +18,8 @@ pub fn _1_account_lot_detail_to_txt(
settings: &ImportProcessParameters,
raw_acct_map: &HashMap<u16, RawAccount>,
acct_map: &HashMap<u16, Account>,
txns_map: &HashMap<u32, Transaction>,
ars: &HashMap<u32, ActionRecord>,
txns_map: &HashMap<u32, Transaction>,
) -> Result<(), Box<dyn Error>> {
// =====================================

View File

@ -126,8 +126,8 @@ fn main() -> Result<(), Box<dyn Error>> {
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<dyn Error>> {
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<dyn Error>> {
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<dyn Error>> {
mytui::print_menu_tui::print_menu_tui(
&settings,
&action_records_map,
&raw_acct_map,
&account_map,
&action_records_map,
&transactions_map
)?;
}

View File

@ -129,9 +129,9 @@ impl<'a> PrintWindow<'a> {
pub fn export(
app: &PrintWindow,
settings: &ImportProcessParameters,
action_records_map: &HashMap<u32, ActionRecord>,
raw_acct_map: &HashMap<u16, RawAccount>,
account_map: &HashMap<u16, Account>,
action_records_map: &HashMap<u32, ActionRecord>,
transactions_map: &HashMap<u32, Transaction>,
) -> Result<(), Box<dyn Error>> {
@ -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 {

View File

@ -24,9 +24,9 @@ use crate::mytui::app as app;
pub (crate) fn print_menu_tui(
settings: &ImportProcessParameters,
action_records_map: &HashMap<u32, ActionRecord>,
raw_acct_map: &HashMap<u16, RawAccount>,
account_map: &HashMap<u16, Account>,
action_records_map: &HashMap<u32, ActionRecord>,
transactions_map: &HashMap<u32, Transaction>,
) -> Result<(), Box<dyn Error>> {
@ -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
)?;

View File

@ -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,