separate function check_first_format_when_compressing

This commit is contained in:
João M. Bezerra 2023-02-03 01:54:06 -03:00
parent f33c9c0f39
commit 93daa7b929
2 changed files with 17 additions and 11 deletions

View File

@ -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 <FILES>... {output_path}.tar.gz"))
.hint(format!(" ouch compress <FILES>... {output_path}.zip"))
.hint("")
.hint("Alternatively, you can overwrite this option by using the '--format' flag:")
.hint(format!(" ouch compress <FILES>... {output_path} --format tar.gz"))
.into()
})
}

View File

@ -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 <FILES>... {output_path}.tar.gz"))
.hint(format!(" ouch compress <FILES>... {output_path}.zip"))
.hint("")
.hint("Alternatively, you can overwrite this option by using the '--format' flag:")
.hint(format!(" ouch compress <FILES>... {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;