From a9673a3ebac9d91752949907f0be3ba1830c4f97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Rodrigues=20Miguel?= Date: Sun, 25 Jul 2021 01:23:58 -0300 Subject: [PATCH] Reuse Confirmation struct when checking for overwrite permission --- src/cli.rs | 43 ++++++++++++++++------------------------ src/commands.rs | 7 ++----- src/decompressors/tar.rs | 7 ++----- src/decompressors/zip.rs | 7 ++----- src/dialogs.rs | 2 +- src/lib.rs | 3 +++ src/utils.rs | 10 +++------- 7 files changed, 30 insertions(+), 49 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 0b38cab..66ab1d3 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -37,8 +37,8 @@ pub fn parse_args() -> crate::Result { match &mut parsed_args.command { Command::Compress { files, .. } | Command::Decompress { files, .. } => { *files = canonicalize_files(&files)?; - } - _ => {} + }, + _ => {}, } Ok(parsed_args) } @@ -72,7 +72,7 @@ fn canonicalize(path: impl AsRef) -> crate::Result { } else { Err(io_err.into()) } - } + }, } } @@ -107,7 +107,7 @@ pub fn parse_args_from(mut args: Vec) -> crate::Result { let command = Command::Compress { files, compressed_output_path }; ParsedArgs { command, flags } - } + }, // Defaults to decompression when there is no subcommand None => { flags_info.push(arg_flag!('o', "output")); @@ -130,7 +130,7 @@ pub fn parse_args_from(mut args: Vec) -> crate::Result { let command = Command::Decompress { files, output_folder }; ParsedArgs { command, flags } - } + }, _ => unreachable!("You should match each subcommand passed."), }; @@ -157,20 +157,14 @@ mod tests { assert_eq!(test_cli("--help").unwrap().command, Command::ShowHelp); assert_eq!(test_cli("--version").unwrap().command, Command::ShowVersion); assert_eq!(test_cli("--version").unwrap().flags, oof::Flags::default()); - assert_eq!( - test_cli("foo.zip bar.zip").unwrap().command, - Command::Decompress { - files: vec!["foo.zip".into(), "bar.zip".into()], - output_folder: None - } - ); - assert_eq!( - test_cli("compress foo bar baz.zip").unwrap().command, - Command::Compress { - files: vec!["foo".into(), "bar".into()], - compressed_output_path: "baz.zip".into() - } - ); + assert_eq!(test_cli("foo.zip bar.zip").unwrap().command, Command::Decompress { + files: vec!["foo.zip".into(), "bar.zip".into()], + output_folder: None + }); + assert_eq!(test_cli("compress foo bar baz.zip").unwrap().command, Command::Compress { + files: vec!["foo".into(), "bar".into()], + compressed_output_path: "baz.zip".into() + }); assert_eq!(test_cli("compress").unwrap_err(), crate::Error::MissingArgumentsForCompression); } @@ -180,12 +174,9 @@ mod tests { assert_eq!(test_cli("--help").unwrap().flags, oof::Flags::default()); assert_eq!(test_cli("--version").unwrap().flags, oof::Flags::default()); - assert_eq!( - test_cli("foo --yes bar --output folder").unwrap().flags, - oof::Flags { - boolean_flags: vec!["yes"].into_iter().collect(), - argument_flags: vec![("output", OsString::from("folder"))].into_iter().collect(), - } - ); + assert_eq!(test_cli("foo --yes bar --output folder").unwrap().flags, oof::Flags { + boolean_flags: vec!["yes"].into_iter().collect(), + argument_flags: vec![("output", OsString::from("folder"))].into_iter().collect(), + }); } } diff --git a/src/commands.rs b/src/commands.rs index b39ac7c..7c7bbaa 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -16,7 +16,6 @@ use crate::{ BzipDecompressor, DecompressionResult, Decompressor, GzipDecompressor, LzmaDecompressor, TarDecompressor, ZipDecompressor, }, - dialogs::Confirmation, extension::{CompressionFormat, Extension}, file::File, oof, utils, @@ -140,8 +139,7 @@ fn decompress_file_in_memory( println!("{}[INFO]{} saving to {:?}.", colors::yellow(), colors::reset(), file_name); if file_name.exists() { - let confirm = Confirmation::new("Do you want to overwrite 'FILE'?", Some("FILE")); - if !utils::permission_for_overwriting(&file_name, flags, &confirm)? { + if !utils::permission_for_overwriting(&file_name, flags)? { return Ok(()); } } @@ -169,10 +167,9 @@ fn compress_files( ) -> crate::Result<()> { let mut output = File::from(output_path)?; - let confirm = Confirmation::new("Do you want to overwrite 'FILE'?", Some("FILE")); let (first_compressor, second_compressor) = get_compressor(&output)?; - if output_path.exists() && !utils::permission_for_overwriting(&output_path, flags, &confirm)? { + if output_path.exists() && !utils::permission_for_overwriting(&output_path, flags)? { // The user does not want to overwrite the file return Ok(()); } diff --git a/src/decompressors/tar.rs b/src/decompressors/tar.rs index 5298f82..7a9e01f 100644 --- a/src/decompressors/tar.rs +++ b/src/decompressors/tar.rs @@ -8,7 +8,7 @@ use tar::{self, Archive}; use utils::colors; use super::decompressor::{DecompressionResult, Decompressor}; -use crate::{dialogs::Confirmation, file::File, oof, utils}; +use crate::{file::File, oof, utils}; #[derive(Debug)] pub struct TarDecompressor; @@ -22,7 +22,6 @@ impl TarDecompressor { &from.path ); let mut files_unpacked = vec![]; - let confirm = Confirmation::new("Do you want to overwrite 'FILE'?", Some("FILE")); let mut archive: Archive> = match from.contents_in_memory { Some(bytes) => tar::Archive::new(Box::new(Cursor::new(bytes))), @@ -36,9 +35,7 @@ impl TarDecompressor { let mut file = file?; let file_path = PathBuf::from(into).join(file.path()?); - if file_path.exists() - && !utils::permission_for_overwriting(&file_path, flags, &confirm)? - { + if file_path.exists() && !utils::permission_for_overwriting(&file_path, flags)? { // The user does not want to overwrite the file continue; } diff --git a/src/decompressors/zip.rs b/src/decompressors/zip.rs index d9db14c..80a148f 100644 --- a/src/decompressors/zip.rs +++ b/src/decompressors/zip.rs @@ -8,7 +8,7 @@ use utils::colors; use zip::{self, read::ZipFile, ZipArchive}; use super::decompressor::{DecompressionResult, Decompressor}; -use crate::{dialogs::Confirmation, file::File, oof, utils}; +use crate::{file::File, oof, utils}; #[cfg(unix)] fn __unix_set_permissions(file_path: &Path, file: &ZipFile) { @@ -43,7 +43,6 @@ impl ZipDecompressor { where R: Read + Seek, { - let confirm = Confirmation::new("Do you want to overwrite 'FILE'?", Some("FILE")); let mut unpacked_files = vec![]; for idx in 0..archive.len() { let mut file = archive.by_index(idx)?; @@ -53,9 +52,7 @@ impl ZipDecompressor { }; let file_path = into.join(file_path); - if file_path.exists() - && !utils::permission_for_overwriting(&file_path, flags, &confirm)? - { + if file_path.exists() && !utils::permission_for_overwriting(&file_path, flags)? { // The user does not want to overwrite the file continue; } diff --git a/src/dialogs.rs b/src/dialogs.rs index 5d5e44e..4cc728e 100644 --- a/src/dialogs.rs +++ b/src/dialogs.rs @@ -11,7 +11,7 @@ pub struct Confirmation<'a> { pub struct Error; impl<'a> Confirmation<'a> { - pub fn new(prompt: &'a str, pattern: Option<&'a str>) -> Self { + pub const fn new(prompt: &'a str, pattern: Option<&'a str>) -> Self { Self { prompt, placeholder: pattern } } diff --git a/src/lib.rs b/src/lib.rs index bd0f46a..86814e4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,9 +12,12 @@ mod extension; mod file; mod utils; +use dialogs::Confirmation; pub use error::{Error, Result}; const VERSION: &str = "0.1.5"; +const OVERWRITE_CONFIRMATION: Confirmation = + Confirmation::new("Do you want to overwrite 'FILE'?", Some("FILE")); fn help_command() { use utils::colors::*; diff --git a/src/utils.rs b/src/utils.rs index ddc6745..819f2d5 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -5,7 +5,7 @@ use std::{ path::{Path, PathBuf}, }; -use crate::{dialogs::Confirmation, extension::CompressionFormat, file::File, oof}; +use crate::{extension::CompressionFormat, file::File, oof, OVERWRITE_CONFIRMATION}; #[macro_export] #[cfg(debug_assertions)] @@ -95,11 +95,7 @@ pub fn change_dir_and_return_parent(filename: &Path) -> crate::Result { Ok(previous_location) } -pub fn permission_for_overwriting( - path: &Path, - flags: &oof::Flags, - confirm: &Confirmation, -) -> crate::Result { +pub fn permission_for_overwriting(path: &Path, flags: &oof::Flags) -> crate::Result { match (flags.is_present("yes"), flags.is_present("no")) { (true, true) => { unreachable!( @@ -112,7 +108,7 @@ pub fn permission_for_overwriting( } let file_path_str = to_utf(path); - confirm.ask(Some(&file_path_str)) + OVERWRITE_CONFIRMATION.ask(Some(&file_path_str)) } pub fn to_utf(os_str: impl AsRef) -> String {