From add6a595bd2c8ce24a67609dfb8ae8bccddb627f Mon Sep 17 00:00:00 2001 From: Crypto-Spartan <29098151+Crypto-Spartan@users.noreply.github.com> Date: Thu, 9 Dec 2021 16:21:32 -0500 Subject: [PATCH] Merge pull request #227 from Crypto-Spartan/question-continue remove redundant user_wants_to_continue function --- src/commands.rs | 14 +++++++------- src/lib.rs | 2 +- src/utils/mod.rs | 3 +-- src/utils/question.rs | 37 ++++++++++++++++++++----------------- 4 files changed, 29 insertions(+), 27 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index 5de074308..ecc140779 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -24,9 +24,9 @@ use crate::{ progress::Progress, utils::{ self, concatenate_os_str_list, dir_is_empty, nice_directory_display, to_utf, try_infer_extension, - user_wants_to_continue_compressing, user_wants_to_continue_decompressing, + user_wants_to_continue, }, - warning, Opts, QuestionPolicy, Subcommand, + warning, Opts, QuestionAction, QuestionPolicy, Subcommand, }; // Used in BufReader and BufWriter to perform less syscalls @@ -361,7 +361,7 @@ fn compress_files( ); // give user the option to continue compressing after warning is shown - if !user_wants_to_continue_compressing(output_dir, question_policy)? { + if !user_wants_to_continue(output_dir, question_policy, QuestionAction::Compression)? { return Ok(()); } @@ -523,7 +523,7 @@ fn decompress_file( ); // give user the option to continue decompressing after warning is shown - if !user_wants_to_continue_decompressing(input_file_path, question_policy)? { + if !user_wants_to_continue(input_file_path, question_policy, QuestionAction::Decompression)? { return Ok(()); } @@ -618,7 +618,7 @@ fn list_archive_contents( ); // give user the option to continue decompressing after warning is shown - if !user_wants_to_continue_decompressing(archive_path, question_policy)? { + if !user_wants_to_continue(archive_path, question_policy, QuestionAction::Decompression)? { return Ok(()); } @@ -708,7 +708,7 @@ fn check_mime_type( // Infering the file extension can have unpredicted consequences (e.g. the user just // mistyped, ...) which we should always inform the user about. info!(accessible, "Detected file: `{}` extension as `{}`", path.display(), detected_format); - if user_wants_to_continue_decompressing(path, question_policy)? { + if user_wants_to_continue(path, question_policy, QuestionAction::Decompression)? { format.push(detected_format); } else { return Ok(ControlFlow::Break(())); @@ -724,7 +724,7 @@ fn check_mime_type( outer_ext, detected_format ); - if !user_wants_to_continue_decompressing(path, question_policy)? { + if !user_wants_to_continue(path, question_policy, QuestionAction::Decompression)? { return Ok(ControlFlow::Break(())); } } diff --git a/src/lib.rs b/src/lib.rs index e33706177..76a84603b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,7 @@ pub mod opts; pub use error::{Error, Result}; pub use opts::{Opts, Subcommand}; -pub use utils::QuestionPolicy; +pub use utils::{QuestionAction, QuestionPolicy}; /// The status code returned from `ouch` on error pub const EXIT_FAILURE: i32 = libc::EXIT_FAILURE; diff --git a/src/utils/mod.rs b/src/utils/mod.rs index b86318617..e3dde097b 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -13,8 +13,7 @@ pub use fs::{ cd_into_same_dir_as, clear_path, create_dir_if_non_existent, dir_is_empty, is_symlink, try_infer_extension, }; pub use question::{ - create_or_ask_overwrite, user_wants_to_continue_compressing, user_wants_to_continue_decompressing, - user_wants_to_overwrite, QuestionPolicy, + create_or_ask_overwrite, user_wants_to_continue, user_wants_to_overwrite, QuestionAction, QuestionPolicy, }; pub use utf8::{get_invalid_utf8_paths, is_invalid_utf8}; diff --git a/src/utils/question.rs b/src/utils/question.rs index e0d2cd51a..65039440e 100644 --- a/src/utils/question.rs +++ b/src/utils/question.rs @@ -28,6 +28,15 @@ pub enum QuestionPolicy { AlwaysNo, } +#[derive(Debug, PartialEq, Clone, Copy)] +/// Determines which action is being questioned +pub enum QuestionAction { + /// question called from a compression function + Compression, + /// question called from a decompression function + Decompression, +} + /// Check if QuestionPolicy flags were set, otherwise, ask user if they want to overwrite. pub fn user_wants_to_overwrite(path: &Path, question_policy: QuestionPolicy) -> crate::Result { match question_policy { @@ -63,30 +72,24 @@ pub fn create_or_ask_overwrite(path: &Path, question_policy: QuestionPolicy) -> } } -/// Check if QuestionPolicy flags were set, otherwise, ask the user if they want to continue compressing. -pub fn user_wants_to_continue_compressing(path: &Path, question_policy: QuestionPolicy) -> crate::Result { - match question_policy { - QuestionPolicy::AlwaysYes => Ok(true), - QuestionPolicy::AlwaysNo => Ok(false), - QuestionPolicy::Ask => { - let path = to_utf(strip_cur_dir(path)); - let path = Some(path.as_str()); - let placeholder = Some("FILE"); - Confirmation::new("Do you want to continue compressing 'FILE'?", placeholder).ask(path) - } - } -} - -/// Check if QuestionPolicy flags were set, otherwise, ask the user if they want to continue decompressing. -pub fn user_wants_to_continue_decompressing(path: &Path, question_policy: QuestionPolicy) -> crate::Result { +/// Check if QuestionPolicy flags were set, otherwise, ask the user if they want to continue. +pub fn user_wants_to_continue( + path: &Path, + question_policy: QuestionPolicy, + question_action: QuestionAction, +) -> crate::Result { match question_policy { QuestionPolicy::AlwaysYes => Ok(true), QuestionPolicy::AlwaysNo => Ok(false), QuestionPolicy::Ask => { + let action = match question_action { + QuestionAction::Compression => "compressing", + QuestionAction::Decompression => "decompressing", + }; let path = to_utf(strip_cur_dir(path)); let path = Some(path.as_str()); let placeholder = Some("FILE"); - Confirmation::new("Do you want to continue decompressing 'FILE'?", placeholder).ask(path) + Confirmation::new(&format!("Do you want to continue {} 'FILE'?", action), placeholder).ask(path) } } }