Merge pull request #227 from Crypto-Spartan/question-continue

remove redundant user_wants_to_continue function
This commit is contained in:
Crypto-Spartan 2021-12-09 16:21:32 -05:00 committed by GitHub
parent f40f40cda0
commit add6a595bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 27 deletions

View File

@ -24,9 +24,9 @@ use crate::{
progress::Progress, progress::Progress,
utils::{ utils::{
self, concatenate_os_str_list, dir_is_empty, nice_directory_display, to_utf, try_infer_extension, 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 // 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 // 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(()); return Ok(());
} }
@ -523,7 +523,7 @@ fn decompress_file(
); );
// give user the option to continue decompressing after warning is shown // 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(()); return Ok(());
} }
@ -618,7 +618,7 @@ fn list_archive_contents(
); );
// give user the option to continue decompressing after warning is shown // 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(()); return Ok(());
} }
@ -708,7 +708,7 @@ fn check_mime_type(
// Infering the file extension can have unpredicted consequences (e.g. the user just // Infering the file extension can have unpredicted consequences (e.g. the user just
// mistyped, ...) which we should always inform the user about. // mistyped, ...) which we should always inform the user about.
info!(accessible, "Detected file: `{}` extension as `{}`", path.display(), detected_format); 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); format.push(detected_format);
} else { } else {
return Ok(ControlFlow::Break(())); return Ok(ControlFlow::Break(()));
@ -724,7 +724,7 @@ fn check_mime_type(
outer_ext, outer_ext,
detected_format 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(())); return Ok(ControlFlow::Break(()));
} }
} }

View File

@ -23,7 +23,7 @@ pub mod opts;
pub use error::{Error, Result}; pub use error::{Error, Result};
pub use opts::{Opts, Subcommand}; pub use opts::{Opts, Subcommand};
pub use utils::QuestionPolicy; pub use utils::{QuestionAction, QuestionPolicy};
/// The status code returned from `ouch` on error /// The status code returned from `ouch` on error
pub const EXIT_FAILURE: i32 = libc::EXIT_FAILURE; pub const EXIT_FAILURE: i32 = libc::EXIT_FAILURE;

View File

@ -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, cd_into_same_dir_as, clear_path, create_dir_if_non_existent, dir_is_empty, is_symlink, try_infer_extension,
}; };
pub use question::{ pub use question::{
create_or_ask_overwrite, user_wants_to_continue_compressing, user_wants_to_continue_decompressing, create_or_ask_overwrite, user_wants_to_continue, user_wants_to_overwrite, QuestionAction, QuestionPolicy,
user_wants_to_overwrite, QuestionPolicy,
}; };
pub use utf8::{get_invalid_utf8_paths, is_invalid_utf8}; pub use utf8::{get_invalid_utf8_paths, is_invalid_utf8};

View File

@ -28,6 +28,15 @@ pub enum QuestionPolicy {
AlwaysNo, 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. /// 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<bool> { pub fn user_wants_to_overwrite(path: &Path, question_policy: QuestionPolicy) -> crate::Result<bool> {
match question_policy { 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. /// Check if QuestionPolicy flags were set, otherwise, ask the user if they want to continue.
pub fn user_wants_to_continue_compressing(path: &Path, question_policy: QuestionPolicy) -> crate::Result<bool> { pub fn user_wants_to_continue(
path: &Path,
question_policy: QuestionPolicy,
question_action: QuestionAction,
) -> crate::Result<bool> {
match question_policy { match question_policy {
QuestionPolicy::AlwaysYes => Ok(true), QuestionPolicy::AlwaysYes => Ok(true),
QuestionPolicy::AlwaysNo => Ok(false), QuestionPolicy::AlwaysNo => Ok(false),
QuestionPolicy::Ask => { QuestionPolicy::Ask => {
let action = match question_action {
QuestionAction::Compression => "compressing",
QuestionAction::Decompression => "decompressing",
};
let path = to_utf(strip_cur_dir(path)); let path = to_utf(strip_cur_dir(path));
let path = Some(path.as_str()); let path = Some(path.as_str());
let placeholder = Some("FILE"); let placeholder = Some("FILE");
Confirmation::new("Do you want to continue compressing 'FILE'?", placeholder).ask(path) Confirmation::new(&format!("Do you want to continue {} 'FILE'?", action), 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<bool> {
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 decompressing 'FILE'?", placeholder).ask(path)
} }
} }
} }