From f95b68b69b9fe367d843f34c4e63258d182390bd Mon Sep 17 00:00:00 2001 From: scoobybejesus Date: Sun, 6 Oct 2019 21:40:18 -0400 Subject: [PATCH] Added setup file for finalizing the input_file_path and settings. --- src/core_functions.rs | 1 + src/main.rs | 102 ++++--------------------------------- src/setup.rs | 114 ++++++++++++++++++++++++++++++++++++++++++ src/skip_wizard.rs | 44 ++++------------ src/wizard.rs | 66 ++++++++---------------- 5 files changed, 155 insertions(+), 172 deletions(-) create mode 100644 src/setup.rs diff --git a/src/core_functions.rs b/src/core_functions.rs index 8ee1cbf..2f47acc 100644 --- a/src/core_functions.rs +++ b/src/core_functions.rs @@ -54,6 +54,7 @@ pub struct ImportProcessParameters { pub lk_cutoff_date_string: String, pub date_separator: String, pub iso_date_style: bool, + pub should_export: bool, } pub(crate) fn import_and_process_final( diff --git a/src/main.rs b/src/main.rs index 5b16676..e14218c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,6 @@ use std::ffi::OsString; use structopt::StructOpt; use std::path::PathBuf; use std::error::Error; -use std::process; mod account; mod transaction; @@ -28,6 +27,7 @@ mod decimal_utils; mod tests; mod wizard; mod skip_wizard; +mod setup; #[derive(StructOpt, Debug)] @@ -99,20 +99,7 @@ pub(crate) struct Options { output_dir_path: PathBuf, } -pub struct NonExclusiveToImportWizardArgs { - file_to_import: PathBuf, - accept_args: bool, - iso_date: bool, - date_separator: String, - home_currency: String, -} -pub struct ExclusiveToImportWizardArgs { - pub inv_costing_method_arg: OsString, - pub lk_cutoff_date_arg: Option, - pub output_dir_path: PathBuf, - pub suppress_reports: bool, -} fn main() -> Result<(), Box> { @@ -129,86 +116,17 @@ fn main() -> Result<(), Box> { Note: it is designed to import a full history. Gains and losses may be incorrect otherwise. "); - let account_map; - let raw_acct_map; - let action_records_map; - let transactions_map; - let settings; - let like_kind_settings; - let should_export; + let (input_file_path, settings) = setup::run_setup(args)?; - let input_file_path; + let ( + account_map, + raw_acct_map, + action_records_map, + transactions_map, + like_kind_settings + ) = core_functions::import_and_process_final(input_file_path, &settings)?; - if let Some(file) = args.file_to_import { - input_file_path = file - } else { - // println!("WARN: Flag to 'accept args' was set, but 'file' is missing. It is a required field.\n"); - input_file_path = cli_user_choices::choose_file_for_import()?; - } - - let date_separator = match args.opts.date_separator.into_string().unwrap().as_str() { - "h" => {"-"} - "s" => {"/"} - "p" => {"."} - _ => { println!("\nFATAL: The date-separator arg requires either an 'h', an 's', or a 'p'.\n"); process::exit(1) } - }; - - let non_excl_args = NonExclusiveToImportWizardArgs { - file_to_import: input_file_path, - accept_args: args.flags.accept_args, - iso_date: args.flags.iso_date, - date_separator: date_separator.to_string(), - home_currency: args.opts.home_currency.into_string().unwrap().to_uppercase(), - }; - - let excl_args = ExclusiveToImportWizardArgs { - inv_costing_method_arg: args.opts.inv_costing_method, - lk_cutoff_date_arg: args.opts.lk_cutoff_date, - output_dir_path: args.opts.output_dir_path, - suppress_reports: args.flags.suppress_reports, - }; - - if !args.flags.accept_args { - - let ( - account_map1, - raw_acct_map1, - action_records_map1, - transactions_map1, - like_kind_settings1, - settings1, - should_export1 - ) = wizard::wizard(non_excl_args, excl_args)?; - - account_map = account_map1; - raw_acct_map = raw_acct_map1; - action_records_map = action_records_map1; - transactions_map = transactions_map1; - settings = settings1; - like_kind_settings = like_kind_settings1; - should_export = should_export1; - - } else { - - let ( - account_map1, - raw_acct_map1, - action_records_map1, - transactions_map1, - like_kind_settings1, - settings1, - should_export1 - ) = skip_wizard::skip_wizard(non_excl_args, excl_args)?; - - account_map = account_map1; - raw_acct_map = raw_acct_map1; - action_records_map = action_records_map1; - transactions_map = transactions_map1; - settings = settings1; - like_kind_settings = like_kind_settings1; - should_export = should_export1; - - } + let should_export = settings.should_export; if should_export { diff --git a/src/setup.rs b/src/setup.rs new file mode 100644 index 0000000..92526dc --- /dev/null +++ b/src/setup.rs @@ -0,0 +1,114 @@ +// Copyright (c) 2017-2019, scoobybejesus +// Redistributions must include the license: https://github.com/scoobybejesus/cryptools/blob/master/LEGAL.txt + +use std::ffi::OsString; +use std::path::PathBuf; +use std::error::Error; +use std::process; + +use crate::cli_user_choices; +use crate::core_functions::{ImportProcessParameters, InventoryCostingMethod}; +use crate::skip_wizard; +use crate::wizard; + + +pub struct WizardMaybeArgs { + pub inv_costing_method_arg: OsString, + pub lk_cutoff_date_arg: Option, + pub output_dir_path: PathBuf, + pub suppress_reports: bool, +} + +pub (crate) fn run_setup(args: super::Cli) -> Result<(PathBuf, ImportProcessParameters), Box> { + + let date_separator = match args.opts.date_separator.into_string().unwrap().as_str() { + "h" => { "-" } + "s" => { "/" } + "p" => { "." } + _ => { println!("\nFATAL: The date-separator arg requires either an 'h', an 's', or a 'p'.\n"); process::exit(1) } + }; + + let input_file_path = match args.file_to_import { + Some(file) => file, + None => cli_user_choices::choose_file_for_import()? + }; + + let wizard_or_not_args = WizardMaybeArgs { + inv_costing_method_arg: args.opts.inv_costing_method, + lk_cutoff_date_arg: args.opts.lk_cutoff_date, + output_dir_path: args.opts.output_dir_path, + suppress_reports: args.flags.suppress_reports, + }; + + let( + costing_method_choice, + like_kind_election, + like_kind_cutoff_date_string, + should_export, + output_dir_path, + ) = wizard_or_not(args.flags.accept_args, wizard_or_not_args)?; + + let settings = ImportProcessParameters { + export_path: output_dir_path, + home_currency: args.opts.home_currency.into_string().unwrap().to_uppercase(), + costing_method: costing_method_choice, + enable_like_kind_treatment: like_kind_election, + lk_cutoff_date_string: like_kind_cutoff_date_string, + date_separator: date_separator.to_string(), + iso_date_style: args.flags.iso_date, + should_export: should_export, + }; + + Ok((input_file_path, settings)) +} + +fn wizard_or_not(accept_args: bool, excl_args: WizardMaybeArgs) -> Result<( + InventoryCostingMethod, + bool, + String, + bool, + PathBuf, + ), Box> { + + let costing_method_choice; + let like_kind_election; + let like_kind_cutoff_date_string; + let should_export; + let output_dir_path; + + if !accept_args { + + let ( + costing_method_choice1, + like_kind_election1, + like_kind_cutoff_date_string1, + should_export1, + output_dir_path1, + ) = wizard::wizard(excl_args)?; + + costing_method_choice = costing_method_choice1; + like_kind_election = like_kind_election1; + like_kind_cutoff_date_string = like_kind_cutoff_date_string1; + should_export = should_export1; + output_dir_path = output_dir_path1; + + } else { + + let ( + costing_method_choice1, + like_kind_election1, + like_kind_cutoff_date_string1, + should_export1, + output_dir_path1, + ) = skip_wizard::skip_wizard(excl_args)?; + + costing_method_choice = costing_method_choice1; + like_kind_election = like_kind_election1; + like_kind_cutoff_date_string = like_kind_cutoff_date_string1; + should_export = should_export1; + output_dir_path = output_dir_path1; + + } + + Ok((costing_method_choice, like_kind_election, like_kind_cutoff_date_string, should_export, output_dir_path)) + } \ No newline at end of file diff --git a/src/skip_wizard.rs b/src/skip_wizard.rs index 183c140..1a8aefa 100644 --- a/src/skip_wizard.rs +++ b/src/skip_wizard.rs @@ -1,25 +1,19 @@ // Copyright (c) 2017-2019, scoobybejesus // Redistributions must include the license: https://github.com/scoobybejesus/cryptools/blob/master/LEGAL.txt +use std::path::PathBuf; use std::error::Error; -use std::collections::{HashMap}; use crate::cli_user_choices; -use crate::core_functions::{self, LikeKindSettings, ImportProcessParameters}; -use crate::account::{Account, RawAccount}; -use crate::transaction::{Transaction, ActionRecord}; +use crate::core_functions::{InventoryCostingMethod}; +use crate::setup::{WizardMaybeArgs}; -pub(crate) fn skip_wizard( - non_excl_args: super::NonExclusiveToImportWizardArgs, - excl_args: super::ExclusiveToImportWizardArgs -) -> Result<( - HashMap, - HashMap, - HashMap, - HashMap, - Option, - ImportProcessParameters, - bool +pub(crate) fn skip_wizard(excl_args: WizardMaybeArgs) -> Result<( + InventoryCostingMethod, + bool, + String, + bool, + PathBuf, ), Box> { let costing_method_choice = cli_user_choices::inv_costing_from_cmd_arg(excl_args.inv_costing_method_arg)?; @@ -35,25 +29,7 @@ pub(crate) fn skip_wizard( like_kind_cutoff_date_string = "1-1-1".to_string(); }; - let settings = ImportProcessParameters { - export_path: excl_args.output_dir_path, - home_currency: non_excl_args.home_currency, - costing_method: costing_method_choice, - enable_like_kind_treatment: like_kind_election, - lk_cutoff_date_string: like_kind_cutoff_date_string, - date_separator: non_excl_args.date_separator, - iso_date_style: non_excl_args.iso_date - }; - - let ( - account_map1, - raw_acct_map1, - action_records_map1, - transactions_map1, - like_kind_settings1 - ) = core_functions::import_and_process_final(non_excl_args.file_to_import, &settings)?; - let should_export = !excl_args.suppress_reports; - Ok((account_map1, raw_acct_map1, action_records_map1, transactions_map1, like_kind_settings1, settings, should_export)) + Ok((costing_method_choice, like_kind_election, like_kind_cutoff_date_string, should_export, excl_args.output_dir_path)) } \ No newline at end of file diff --git a/src/wizard.rs b/src/wizard.rs index e17c6e9..de75930 100644 --- a/src/wizard.rs +++ b/src/wizard.rs @@ -5,24 +5,17 @@ use std::error::Error; use std::process; use std::io::{self, BufRead}; use std::path::PathBuf; -use std::collections::{HashMap}; use crate::cli_user_choices; -use crate::core_functions::{self, LikeKindSettings, ImportProcessParameters}; -use crate::account::{Account, RawAccount}; -use crate::transaction::{Transaction, ActionRecord}; +use crate::core_functions::{InventoryCostingMethod}; +use crate::setup::{WizardMaybeArgs}; -pub(crate) fn wizard( - non_excl_args: super::NonExclusiveToImportWizardArgs, - excl_args: super::ExclusiveToImportWizardArgs -) -> Result<( - HashMap, - HashMap, - HashMap, - HashMap, - Option, - ImportProcessParameters, - bool +pub(crate) fn wizard(excl_args: WizardMaybeArgs) -> Result<( + InventoryCostingMethod, + bool, + String, + bool, + PathBuf, ), Box> { shall_we_proceed()?; @@ -39,27 +32,9 @@ pub(crate) fn wizard( let (like_kind_election, like_kind_cutoff_date_string) = cli_user_choices::elect_like_kind_treatment(&lk_cutoff_date_opt_string)?; - let mut settings = ImportProcessParameters { - export_path: excl_args.output_dir_path, - home_currency: non_excl_args.home_currency, - costing_method: costing_method_choice, - enable_like_kind_treatment: like_kind_election, - lk_cutoff_date_string: like_kind_cutoff_date_string, - date_separator: non_excl_args.date_separator, - iso_date_style: non_excl_args.iso_date - }; + let (should_export, output_dir_path) = export_reports_to_output_dir(excl_args.output_dir_path)?; - let ( - account_map1, - raw_acct_map1, - action_records_map1, - transactions_map1, - like_kind_settings1 - ) = core_functions::import_and_process_final(non_excl_args.file_to_import, &settings)?; - - let should_export = export_reports_to_output_dir(&mut settings)?; - - Ok((account_map1, raw_acct_map1, action_records_map1, transactions_map1, like_kind_settings1, settings, should_export)) + Ok((costing_method_choice, like_kind_election, like_kind_cutoff_date_string, should_export, output_dir_path.to_path_buf())) } fn shall_we_proceed() -> Result<(), Box> { @@ -85,19 +60,19 @@ fn shall_we_proceed() -> Result<(), Box> { Ok(()) } -fn export_reports_to_output_dir(settings: &mut ImportProcessParameters) -> Result<(bool), Box> { +fn export_reports_to_output_dir(output_dir_path: PathBuf) -> Result<(bool, PathBuf), Box> { - println!("\nThe directory currently selected for exporting reports is: {}", settings.export_path.to_str().unwrap()); + println!("\nThe directory currently selected for exporting reports is: {}", output_dir_path.to_str().unwrap()); - if settings.export_path.to_str().unwrap() == "." { + if output_dir_path.to_str().unwrap() == "." { println!(" (A 'dot' denotes the default value: current working directory.)"); } println!("\nExport reports to selected directory? [Y/n/c] ('c' to 'change') "); - let choice = _export(settings)?; + let (choice, path) = _export(output_dir_path)?; - fn _export(settings: &mut ImportProcessParameters) -> Result<(bool), Box> { + fn _export(output_dir_path: PathBuf) -> Result<(bool, PathBuf), Box> { let mut input = String::new(); let stdin = io::stdin(); @@ -105,18 +80,17 @@ fn export_reports_to_output_dir(settings: &mut ImportProcessParameters) -> Resul match input.trim().to_ascii_lowercase().as_str() { - "y" | "ye" | "yes" | "" => { Ok(true) }, - "n" | "no" => { println!("Okay, no reports will be created."); Ok(false) }, + "y" | "ye" | "yes" | "" => { Ok((true, output_dir_path)) }, + "n" | "no" => { println!("Okay, no reports will be created."); Ok((false, output_dir_path)) }, "c" | "change" => { let new_dir = cli_user_choices::choose_export_dir()?; - settings.export_path = PathBuf::from(new_dir); - Ok(true) + Ok((true, new_dir)) }, _ => { println!("Please respond with 'y', 'n', or 'c' (or 'yes' or 'no' or 'change')."); - _export(settings) + _export(output_dir_path) } } } - Ok(choice) + Ok((choice, path)) } \ No newline at end of file