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;