Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove redundant user_wants_to_continue function #227

Merged
merged 2 commits into from
Dec 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
remove redundant user_wants_to_continue function
  • Loading branch information
Crypto-Spartan committed Dec 8, 2021
commit fe05fe067cc7c456dec7fafabe3194efc20c6da2
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, QuestionPolicy, QuestionAction, 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::{QuestionPolicy, QuestionAction};

/// The status code returned from `ouch` on error
pub const EXIT_FAILURE: i32 = libc::EXIT_FAILURE;
4 changes: 2 additions & 2 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ mod question;
pub use formatting::{concatenate_os_str_list, nice_directory_display, strip_cur_dir, to_utf, Bytes};
pub use fs::{cd_into_same_dir_as, clear_path, create_dir_if_non_existent, dir_is_empty, 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,
QuestionPolicy, QuestionAction,
};
pub use utf8::{get_invalid_utf8_paths, is_invalid_utf8};

Expand Down
34 changes: 17 additions & 17 deletions src/utils/question.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ 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 +73,20 @@ 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