From 93daa7b929e2a819b56141290e973e72ef16954f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Fri, 3 Feb 2023 01:54:06 -0300 Subject: [PATCH] separate function `check_first_format_when_compressing` --- src/check.rs | 16 ++++++++++++++++ src/commands/mod.rs | 12 +----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/check.rs b/src/check.rs index e84650c..dd5b6a0 100644 --- a/src/check.rs +++ b/src/check.rs @@ -150,3 +150,19 @@ pub fn check_missing_formats_when_decompressing(files: &[PathBuf], formats: &[Ve } Ok(()) } + +/// Check if there is a first format when compressing, and returns it. +pub fn check_first_format_when_compressing<'a>(formats: &'a [Extension], output_path: &Path) -> Result<&'a Extension> { + formats.first().ok_or_else(|| { + let output_path = EscapedPathDisplay::new(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 ... {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 ... {output_path} --format tar.gz")) + .into() + }) +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 9543d44..2c0cdaf 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -59,17 +59,7 @@ pub fn run( None => (None, extension::extensions_from_path(&output_path)), }; - let first_format = formats.first().ok_or_else(|| { - let output_path = EscapedPathDisplay::new(&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 ... {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 ... {output_path} --format tar.gz")) - })?; + let first_format = check::check_first_format_when_compressing(&formats, &output_path)?; let is_some_input_a_folder = files.iter().any(|path| path.is_dir()); let is_multiple_inputs = files.len() > 1;