From 6440f0e47fbdd99300c75598e7275da883e28e4f Mon Sep 17 00:00:00 2001 From: scoobybejesus Date: Sun, 22 Sep 2019 11:20:16 -0400 Subject: [PATCH] Inv_costing_method arg now passed through wizard. Related refactoring. --- src/cli_user_choices.rs | 28 ++++++++++++++++------------ src/skip_wizard.rs | 8 +------- src/wizard.rs | 2 +- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/cli_user_choices.rs b/src/cli_user_choices.rs index f88f993..5d04cc1 100644 --- a/src/cli_user_choices.rs +++ b/src/cli_user_choices.rs @@ -5,6 +5,7 @@ use std::error::Error; use std::io::{self, BufRead}; use std::process; use std::path::PathBuf; +use std::ffi::OsString; use chrono::NaiveDate; use rustyline::completion::{Completer, FilenameCompleter, Pair}; @@ -128,44 +129,47 @@ fn _get_path() -> Result<(String, bool), Box> { // } // } -pub fn choose_inventory_costing_method() -> Result> { +pub fn choose_inventory_costing_method(cmd_line_arg: OsString) -> Result> { - println!("Choose the lot inventory costing method. [Default: 1]"); + println!("Choose the lot inventory costing method. [Default/Chosen: {:?}]", cmd_line_arg); println!("1. LIFO according to the order the lot was created."); println!("2. LIFO according to the basis date of the lot."); println!("3. FIFO according to the order the lot was created."); println!("4. FIFO according to the basis date of the lot."); - let method = _costing_method()?; + let method = _costing_method(cmd_line_arg)?; - fn _costing_method() -> Result> { + fn _costing_method(cmd_line_arg: OsString) -> Result> { let mut input = String::new(); let stdin = io::stdin(); stdin.lock().read_line(&mut input).expect("Failed to read stdin"); match input.trim() { // Without .trim(), there's a hidden \n or something preventing the match - "1" | "" => Ok(InventoryCostingMethod::LIFObyLotCreationDate), + "" => Ok(inv_costing_from_cmd_arg(cmd_line_arg)?), + "1" => Ok(InventoryCostingMethod::LIFObyLotCreationDate), "2" => Ok(InventoryCostingMethod::LIFObyLotBasisDate), "3" => Ok(InventoryCostingMethod::FIFObyLotCreationDate), "4" => Ok(InventoryCostingMethod::FIFObyLotBasisDate), - _ => { println!("Invalid choice. Please enter a valid number."); _costing_method() } + _ => { println!("Invalid choice. Please enter a valid choice."); _costing_method(cmd_line_arg) } } } Ok(method) } -pub fn inv_costing_from_cmd_arg(arg: String) -> Result { +pub fn inv_costing_from_cmd_arg(arg: OsString) -> Result { - match arg.trim() { // Without .trim(), there's a hidden \n or something preventing the match + let inv_costing_arg = match arg.into_string().expect("Could not convert OsString to String.").trim() { + "1" => {"1"} "2" => {"2"} "3" => {"3"} "4" => {"4"} + _ => { println!("WARN: Invalid command line arg passed for 'inv_costing_method'. Using default."); "1" } + }; + + match inv_costing_arg { "1" => Ok(InventoryCostingMethod::LIFObyLotCreationDate), "2" => Ok(InventoryCostingMethod::LIFObyLotBasisDate), "3" => Ok(InventoryCostingMethod::FIFObyLotCreationDate), "4" => Ok(InventoryCostingMethod::FIFObyLotBasisDate), - _ => { - println!("Invalid choice. Please enter a valid number."); - return Err("Invalid choice"); - } + _ => { Err("Invalid input parameter") } // Impossible code path } } diff --git a/src/skip_wizard.rs b/src/skip_wizard.rs index a0f2c6e..31d639f 100644 --- a/src/skip_wizard.rs +++ b/src/skip_wizard.rs @@ -44,13 +44,7 @@ pub(crate) fn skip_wizard(args: super::Cli) -> Result<( like_kind_cutoff_date_string = "1-1-1".to_string(); }; - let clean_inv_costing_arg = match args.inv_costing_method.clone().into_string().expect("Invalid choice on costing method. Aborting.").trim() { - "1" => {"1"} "2" => {"2"} "3" => {"3"} "4" => {"4"} - _ => { println!("WARN: Invalid command line arg passed for 'inv_costing_method'. Using default."); "1" } - }; - let clean_inv_costing_arg_string = clean_inv_costing_arg.to_owned(); - - let costing_method_choice = cli_user_choices::inv_costing_from_cmd_arg(clean_inv_costing_arg_string)?; + let costing_method_choice = cli_user_choices::inv_costing_from_cmd_arg(args.inv_costing_method)?; let settings = ImportProcessParameters { export_path: output_dir_path, diff --git a/src/wizard.rs b/src/wizard.rs index e507911..bb73a8a 100644 --- a/src/wizard.rs +++ b/src/wizard.rs @@ -34,7 +34,7 @@ pub(crate) fn wizard(args: super::Cli) -> Result<( let output_dir_path = args.output_dir_path; - let costing_method_choice = cli_user_choices::choose_inventory_costing_method()?; + let costing_method_choice = cli_user_choices::choose_inventory_costing_method(args.inv_costing_method)?; let home_currency_choice = args.home_currency.into_string().unwrap().to_uppercase();