Skip to content

Commit

Permalink
Merge pull request #227 from Crypto-Spartan/question-continue
Browse files Browse the repository at this point in the history
remove redundant user_wants_to_continue function
  • Loading branch information
Crypto-Spartan authored Dec 9, 2021
1 parent f40f40c commit add6a59
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 27 deletions.
14 changes: 7 additions & 7 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(());
}

Expand Down Expand Up @@ -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(());
}

Expand Down Expand Up @@ -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(());
}

Expand Down Expand Up @@ -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(()));
Expand All @@ -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(()));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
3 changes: 1 addition & 2 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
37 changes: 20 additions & 17 deletions src/utils/question.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> {
match question_policy {
Expand Down Expand Up @@ -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<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 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<bool> {
/// 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<bool> {
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)
}
}
}
Expand Down

0 comments on commit add6a59

Please sign in to comment.