Change clear_path to return Result<bool>

So `Ok(true)` means the path is clear while `Ok(false)` means the user doesn't want to overwrite
This commit is contained in:
Spyros Roum 2021-11-11 15:58:51 +02:00
parent d1d781dded
commit c33d896743
4 changed files with 12 additions and 12 deletions

View File

@ -31,7 +31,7 @@ pub fn unpack_archive(
let mut file = file?; let mut file = file?;
let file_path = output_folder.join(file.path()?); 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 // User doesn't want to overwrite
continue; continue;
} }

View File

@ -39,7 +39,7 @@ where
}; };
let file_path = into.join(file_path); 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 // User doesn't want to overwrite
continue; continue;
} }

View File

@ -342,7 +342,7 @@ fn decompress_file(
// //
// Any other Zip decompression done can take up the whole RAM and freeze ouch. // Any other Zip decompression done can take up the whole RAM and freeze ouch.
if formats.len() == 1 && *formats[0].compression_formats == [Zip] { 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 // User doesn't want to overwrite
return Ok(()); return Ok(());
} }
@ -374,7 +374,7 @@ fn decompress_file(
reader = chain_reader_decoder(format, reader)?; 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 // User doesn't want to overwrite
return Ok(()); return Ok(());
} }

View File

@ -19,14 +19,14 @@ pub fn dir_is_empty(dir_path: &Path) -> bool {
dir_path.read_dir().map(is_empty).unwrap_or_default() dir_path.read_dir().map(is_empty).unwrap_or_default()
} }
/// Remove `path` asking the user to overwrite if necessary /// 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 /// * `Ok(true)` means the path is clear,
/// `Err(_)` is an error /// * `Ok(false)` means the user doesn't want to overwrite
// ToDo: Actual type to translate the above might be clearer? /// * `Err(_)` is an error
pub fn clear_path(path: &Path, question_policy: QuestionPolicy) -> crate::Result<Option<()>> { pub fn clear_path(path: &Path, question_policy: QuestionPolicy) -> crate::Result<bool> {
if path.exists() && !user_wants_to_overwrite(path, question_policy)? { if path.exists() && !user_wants_to_overwrite(path, question_policy)? {
return Ok(None); return Ok(false);
} }
if path.is_dir() { if path.is_dir() {
@ -35,7 +35,7 @@ pub fn clear_path(path: &Path, question_policy: QuestionPolicy) -> crate::Result
fs::remove_file(path)?; fs::remove_file(path)?;
} }
Ok(Some(())) Ok(true)
} }
/// Creates a directory at the path, if there is nothing there. /// Creates a directory at the path, if there is nothing there.