From c33d896743137df3186b5a9096e7ee947db1087a Mon Sep 17 00:00:00 2001 From: Spyros Roum Date: Thu, 11 Nov 2021 15:58:51 +0200 Subject: [PATCH] Change `clear_path` to return `Result` So `Ok(true)` means the path is clear while `Ok(false)` means the user doesn't want to overwrite --- src/archive/tar.rs | 2 +- src/archive/zip.rs | 2 +- src/commands.rs | 4 ++-- src/utils/fs.rs | 16 ++++++++-------- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/archive/tar.rs b/src/archive/tar.rs index 97159fc..a42108c 100644 --- a/src/archive/tar.rs +++ b/src/archive/tar.rs @@ -31,7 +31,7 @@ pub fn unpack_archive( let mut file = file?; let file_path = output_folder.join(file.path()?); - if utils::clear_path(&file_path, question_policy)?.is_none() { + if !utils::clear_path(&file_path, question_policy)? { // User doesn't want to overwrite continue; } diff --git a/src/archive/zip.rs b/src/archive/zip.rs index 5d383b6..c71be89 100644 --- a/src/archive/zip.rs +++ b/src/archive/zip.rs @@ -39,7 +39,7 @@ where }; let file_path = into.join(file_path); - if clear_path(&file_path, question_policy)?.is_none() { + if !clear_path(&file_path, question_policy)? { // User doesn't want to overwrite continue; } diff --git a/src/commands.rs b/src/commands.rs index 235792c..5a2dcee 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -342,7 +342,7 @@ fn decompress_file( // // Any other Zip decompression done can take up the whole RAM and freeze ouch. if formats.len() == 1 && *formats[0].compression_formats == [Zip] { - if utils::clear_path(output_dir, question_policy)?.is_none() { + if !utils::clear_path(output_dir, question_policy)? { // User doesn't want to overwrite return Ok(()); } @@ -374,7 +374,7 @@ fn decompress_file( reader = chain_reader_decoder(format, reader)?; } - if utils::clear_path(&output_path, question_policy)?.is_none() { + if !utils::clear_path(&output_path, question_policy)? { // User doesn't want to overwrite return Ok(()); } diff --git a/src/utils/fs.rs b/src/utils/fs.rs index 9d47ca9..034c3aa 100644 --- a/src/utils/fs.rs +++ b/src/utils/fs.rs @@ -19,14 +19,14 @@ pub fn dir_is_empty(dir_path: &Path) -> bool { dir_path.read_dir().map(is_empty).unwrap_or_default() } -/// Remove `path` asking the user to overwrite if necessary -/// `Ok(Some(())` means the path is clear, -/// `Ok(None)` means the user doesn't want to overwrite -/// `Err(_)` is an error -// ToDo: Actual type to translate the above might be clearer? -pub fn clear_path(path: &Path, question_policy: QuestionPolicy) -> crate::Result> { +/// Remove `path` asking the user to overwrite if necessary. +/// +/// * `Ok(true)` means the path is clear, +/// * `Ok(false)` means the user doesn't want to overwrite +/// * `Err(_)` is an error +pub fn clear_path(path: &Path, question_policy: QuestionPolicy) -> crate::Result { if path.exists() && !user_wants_to_overwrite(path, question_policy)? { - return Ok(None); + return Ok(false); } if path.is_dir() { @@ -35,7 +35,7 @@ pub fn clear_path(path: &Path, question_policy: QuestionPolicy) -> crate::Result fs::remove_file(path)?; } - Ok(Some(())) + Ok(true) } /// Creates a directory at the path, if there is nothing there.