Added setup file for finalizing the input_file_path and settings.

This commit is contained in:
scoobybejesus 2019-10-06 21:40:18 -04:00
parent a1e8b1adc6
commit f95b68b69b
5 changed files with 155 additions and 172 deletions

View File

@ -54,6 +54,7 @@ pub struct ImportProcessParameters {
pub lk_cutoff_date_string: String, pub lk_cutoff_date_string: String,
pub date_separator: String, pub date_separator: String,
pub iso_date_style: bool, pub iso_date_style: bool,
pub should_export: bool,
} }
pub(crate) fn import_and_process_final( pub(crate) fn import_and_process_final(

View File

@ -12,7 +12,6 @@ use std::ffi::OsString;
use structopt::StructOpt; use structopt::StructOpt;
use std::path::PathBuf; use std::path::PathBuf;
use std::error::Error; use std::error::Error;
use std::process;
mod account; mod account;
mod transaction; mod transaction;
@ -28,6 +27,7 @@ mod decimal_utils;
mod tests; mod tests;
mod wizard; mod wizard;
mod skip_wizard; mod skip_wizard;
mod setup;
#[derive(StructOpt, Debug)] #[derive(StructOpt, Debug)]
@ -99,20 +99,7 @@ pub(crate) struct Options {
output_dir_path: PathBuf, 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<OsString>,
pub output_dir_path: PathBuf,
pub suppress_reports: bool,
}
fn main() -> Result<(), Box<dyn Error>> { fn main() -> Result<(), Box<dyn Error>> {
@ -129,86 +116,17 @@ fn main() -> Result<(), Box<dyn Error>> {
Note: it is designed to import a full history. Gains and losses may be incorrect otherwise. Note: it is designed to import a full history. Gains and losses may be incorrect otherwise.
"); ");
let account_map; let (input_file_path, settings) = setup::run_setup(args)?;
let raw_acct_map;
let action_records_map;
let transactions_map;
let settings;
let like_kind_settings;
let should_export;
let input_file_path;
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 ( let (
account_map1, account_map,
raw_acct_map1, raw_acct_map,
action_records_map1, action_records_map,
transactions_map1, transactions_map,
like_kind_settings1, like_kind_settings
settings1, ) = core_functions::import_and_process_final(input_file_path, &settings)?;
should_export1
) = wizard::wizard(non_excl_args, excl_args)?;
account_map = account_map1; let should_export = settings.should_export;
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;
}
if should_export { if should_export {

114
src/setup.rs Normal file
View File

@ -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<OsString>,
pub output_dir_path: PathBuf,
pub suppress_reports: bool,
}
pub (crate) fn run_setup(args: super::Cli) -> Result<(PathBuf, ImportProcessParameters), Box<dyn Error>> {
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<dyn Error>> {
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))
}

View File

@ -1,25 +1,19 @@
// Copyright (c) 2017-2019, scoobybejesus // Copyright (c) 2017-2019, scoobybejesus
// Redistributions must include the license: https://github.com/scoobybejesus/cryptools/blob/master/LEGAL.txt // Redistributions must include the license: https://github.com/scoobybejesus/cryptools/blob/master/LEGAL.txt
use std::path::PathBuf;
use std::error::Error; use std::error::Error;
use std::collections::{HashMap};
use crate::cli_user_choices; use crate::cli_user_choices;
use crate::core_functions::{self, LikeKindSettings, ImportProcessParameters}; use crate::core_functions::{InventoryCostingMethod};
use crate::account::{Account, RawAccount}; use crate::setup::{WizardMaybeArgs};
use crate::transaction::{Transaction, ActionRecord};
pub(crate) fn skip_wizard( pub(crate) fn skip_wizard(excl_args: WizardMaybeArgs) -> Result<(
non_excl_args: super::NonExclusiveToImportWizardArgs, InventoryCostingMethod,
excl_args: super::ExclusiveToImportWizardArgs bool,
) -> Result<( String,
HashMap<u16, Account>, bool,
HashMap<u16, RawAccount>, PathBuf,
HashMap<u32, ActionRecord>,
HashMap<u32, Transaction>,
Option<LikeKindSettings>,
ImportProcessParameters,
bool
), Box<dyn Error>> { ), Box<dyn Error>> {
let costing_method_choice = cli_user_choices::inv_costing_from_cmd_arg(excl_args.inv_costing_method_arg)?; 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(); 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; 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))
} }

View File

@ -5,24 +5,17 @@ use std::error::Error;
use std::process; use std::process;
use std::io::{self, BufRead}; use std::io::{self, BufRead};
use std::path::PathBuf; use std::path::PathBuf;
use std::collections::{HashMap};
use crate::cli_user_choices; use crate::cli_user_choices;
use crate::core_functions::{self, LikeKindSettings, ImportProcessParameters}; use crate::core_functions::{InventoryCostingMethod};
use crate::account::{Account, RawAccount}; use crate::setup::{WizardMaybeArgs};
use crate::transaction::{Transaction, ActionRecord};
pub(crate) fn wizard( pub(crate) fn wizard(excl_args: WizardMaybeArgs) -> Result<(
non_excl_args: super::NonExclusiveToImportWizardArgs, InventoryCostingMethod,
excl_args: super::ExclusiveToImportWizardArgs bool,
) -> Result<( String,
HashMap<u16, Account>, bool,
HashMap<u16, RawAccount>, PathBuf,
HashMap<u32, ActionRecord>,
HashMap<u32, Transaction>,
Option<LikeKindSettings>,
ImportProcessParameters,
bool
), Box<dyn Error>> { ), Box<dyn Error>> {
shall_we_proceed()?; 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 (like_kind_election, like_kind_cutoff_date_string) = cli_user_choices::elect_like_kind_treatment(&lk_cutoff_date_opt_string)?;
let mut settings = ImportProcessParameters { let (should_export, output_dir_path) = export_reports_to_output_dir(excl_args.output_dir_path)?;
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 ( Ok((costing_method_choice, like_kind_election, like_kind_cutoff_date_string, should_export, output_dir_path.to_path_buf()))
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))
} }
fn shall_we_proceed() -> Result<(), Box<dyn Error>> { fn shall_we_proceed() -> Result<(), Box<dyn Error>> {
@ -85,19 +60,19 @@ fn shall_we_proceed() -> Result<(), Box<dyn Error>> {
Ok(()) Ok(())
} }
fn export_reports_to_output_dir(settings: &mut ImportProcessParameters) -> Result<(bool), Box<dyn Error>> { fn export_reports_to_output_dir(output_dir_path: PathBuf) -> Result<(bool, PathBuf), Box<dyn Error>> {
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!(" (A 'dot' denotes the default value: current working directory.)");
} }
println!("\nExport reports to selected directory? [Y/n/c] ('c' to 'change') "); 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<dyn Error>> { fn _export(output_dir_path: PathBuf) -> Result<(bool, PathBuf), Box<dyn Error>> {
let mut input = String::new(); let mut input = String::new();
let stdin = io::stdin(); 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() { match input.trim().to_ascii_lowercase().as_str() {
"y" | "ye" | "yes" | "" => { Ok(true) }, "y" | "ye" | "yes" | "" => { Ok((true, output_dir_path)) },
"n" | "no" => { println!("Okay, no reports will be created."); Ok(false) }, "n" | "no" => { println!("Okay, no reports will be created."); Ok((false, output_dir_path)) },
"c" | "change" => { "c" | "change" => {
let new_dir = cli_user_choices::choose_export_dir()?; let new_dir = cli_user_choices::choose_export_dir()?;
settings.export_path = PathBuf::from(new_dir); Ok((true, new_dir))
Ok(true)
}, },
_ => { println!("Please respond with 'y', 'n', or 'c' (or 'yes' or 'no' or 'change')."); _ => { 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))
} }