Created structs in main for splitting Cli args between wizard-specific args and wizard-independent args.

This commit is contained in:
scoobybejesus 2019-10-06 19:28:56 -04:00
parent 3e4ff26e22
commit a1e8b1adc6
3 changed files with 73 additions and 61 deletions

View File

@ -12,6 +12,7 @@ use std::ffi::OsString;
use structopt::StructOpt;
use std::path::PathBuf;
use std::error::Error;
use std::process;
mod account;
mod transaction;
@ -98,6 +99,20 @@ 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<OsString>,
pub output_dir_path: PathBuf,
pub suppress_reports: bool,
}
fn main() -> Result<(), Box<dyn Error>> {
@ -122,6 +137,37 @@ fn main() -> Result<(), Box<dyn Error>> {
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 (
@ -132,7 +178,7 @@ fn main() -> Result<(), Box<dyn Error>> {
like_kind_settings1,
settings1,
should_export1
) = wizard::wizard(args)?;
) = wizard::wizard(non_excl_args, excl_args)?;
account_map = account_map1;
raw_acct_map = raw_acct_map1;
@ -152,7 +198,7 @@ fn main() -> Result<(), Box<dyn Error>> {
like_kind_settings1,
settings1,
should_export1
) = skip_wizard::skip_wizard(args)?;
) = skip_wizard::skip_wizard(non_excl_args, excl_args)?;
account_map = account_map1;
raw_acct_map = raw_acct_map1;

View File

@ -3,14 +3,16 @@
use std::error::Error;
use std::collections::{HashMap};
use std::process;
use crate::cli_user_choices;
use crate::core_functions::{self, LikeKindSettings, ImportProcessParameters};
use crate::account::{Account, RawAccount};
use crate::transaction::{Transaction, ActionRecord};
pub(crate) fn skip_wizard(args: super::Cli) -> Result<(
pub(crate) fn skip_wizard(
non_excl_args: super::NonExclusiveToImportWizardArgs,
excl_args: super::ExclusiveToImportWizardArgs
) -> Result<(
HashMap<u16, Account>,
HashMap<u16, RawAccount>,
HashMap<u32, ActionRecord>,
@ -20,32 +22,12 @@ pub(crate) fn skip_wizard(args: super::Cli) -> Result<(
bool
), 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;
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 output_dir_path = args.opts.output_dir_path;
let costing_method_choice = cli_user_choices::inv_costing_from_cmd_arg(args.opts.inv_costing_method)?;
let home_currency_choice = args.opts.home_currency.into_string().unwrap().to_uppercase();
let costing_method_choice = cli_user_choices::inv_costing_from_cmd_arg(excl_args.inv_costing_method_arg)?;
let like_kind_election;
let like_kind_cutoff_date_string: String;
if let Some(date) = args.opts.lk_cutoff_date {
if let Some(date) = excl_args.lk_cutoff_date_arg {
like_kind_election = true;
like_kind_cutoff_date_string = date.into_string().unwrap();
} else {
@ -54,13 +36,13 @@ pub(crate) fn skip_wizard(args: super::Cli) -> Result<(
};
let settings = ImportProcessParameters {
export_path: output_dir_path,
home_currency: home_currency_choice,
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: date_separator.to_string(),
iso_date_style: args.flags.iso_date
date_separator: non_excl_args.date_separator,
iso_date_style: non_excl_args.iso_date
};
let (
@ -69,9 +51,9 @@ pub(crate) fn skip_wizard(args: super::Cli) -> Result<(
action_records_map1,
transactions_map1,
like_kind_settings1
) = core_functions::import_and_process_final(input_file_path, &settings)?;
) = core_functions::import_and_process_final(non_excl_args.file_to_import, &settings)?;
let should_export = !args.flags.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))
}

View File

@ -12,7 +12,10 @@ use crate::core_functions::{self, LikeKindSettings, ImportProcessParameters};
use crate::account::{Account, RawAccount};
use crate::transaction::{Transaction, ActionRecord};
pub(crate) fn wizard(args: super::Cli) -> Result<(
pub(crate) fn wizard(
non_excl_args: super::NonExclusiveToImportWizardArgs,
excl_args: super::ExclusiveToImportWizardArgs
) -> Result<(
HashMap<u16, Account>,
HashMap<u16, RawAccount>,
HashMap<u32, ActionRecord>,
@ -24,45 +27,26 @@ pub(crate) fn wizard(args: super::Cli) -> Result<(
shall_we_proceed()?;
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;
if let Some(file) = args.file_to_import {
input_file_path = file
} else {
input_file_path = cli_user_choices::choose_file_for_import()?;
}
let output_dir_path = args.opts.output_dir_path;
let costing_method_choice = cli_user_choices::choose_inventory_costing_method(args.opts.inv_costing_method)?;
let home_currency_choice = args.opts.home_currency.into_string().unwrap().to_uppercase();
let costing_method_choice = cli_user_choices::choose_inventory_costing_method(excl_args.inv_costing_method_arg)?;
let lk_cutoff_date_opt_string;
if let Some(lk_cutoff) = args.opts.lk_cutoff_date {
if let Some(lk_cutoff) = excl_args.lk_cutoff_date_arg {
lk_cutoff_date_opt_string = Some(lk_cutoff.into_string().unwrap())
} else {
lk_cutoff_date_opt_string = None
};
let (like_kind_election, like_kind_cutoff_date) = 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 {
export_path: output_dir_path,
home_currency: home_currency_choice,
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,
date_separator: date_separator.to_string(),
iso_date_style: args.flags.iso_date
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 (
@ -71,7 +55,7 @@ pub(crate) fn wizard(args: super::Cli) -> Result<(
action_records_map1,
transactions_map1,
like_kind_settings1
) = core_functions::import_and_process_final(input_file_path, &settings)?;
) = core_functions::import_and_process_final(non_excl_args.file_to_import, &settings)?;
let should_export = export_reports_to_output_dir(&mut settings)?;