From 0ada885cedea21fb517d6a1f0b93111b63fa6640 Mon Sep 17 00:00:00 2001 From: figsoda Date: Mon, 17 Oct 2022 09:20:13 -0400 Subject: [PATCH] simplify logic --- src/commands/mod.rs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 32ff508..a82f4d8 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -89,28 +89,24 @@ pub fn run( // Formats from path extension, like "file.tar.gz.xz" -> vec![Tar, Gzip, Lzma] let formats = extension::extensions_from_path(&output_path); - if formats.is_empty() { - let error = FinalError::with_title(format!("Cannot compress to '{}'.", to_utf(&output_path))) + let first_format = formats.first().ok_or_else(|| { + let output_path = to_utf(&output_path); + FinalError::with_title(format!("Cannot compress to '{output_path}'.")) .detail("You shall supply the compression format") .hint("Try adding supported extensions (see --help):") - .hint(format!(" ouch compress ... {}.tar.gz", to_utf(&output_path))) - .hint(format!(" ouch compress ... {}.zip", to_utf(&output_path))) + .hint(format!(" ouch compress ... {output_path}.tar.gz")) + .hint(format!(" ouch compress ... {output_path}.zip")) .hint("") .hint("Alternatively, you can overwrite this option by using the '--format' flag:") - .hint(format!( - " ouch compress ... {} --format tar.gz", - to_utf(&output_path) - )); - - return Err(error.into()); - } + .hint(format!(" ouch compress ... {output_path} --format tar.gz")) + })?; let is_some_input_a_folder = files.iter().any(|path| path.is_dir()); let is_multiple_inputs = files.len() > 1; // If first format is not archive, can't compress folder, or multiple files // Index safety: empty formats should be checked above. - if !formats[0].is_archive() && (is_some_input_a_folder || is_multiple_inputs) { + if !first_format.is_archive() && (is_some_input_a_folder || is_multiple_inputs) { // This piece of code creates a suggestion for compressing multiple files // It says: // Change from file.bz.xz