From fadc41289635369f7eaae4f1e312abc3bf83ddc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Tue, 2 Nov 2021 06:13:24 -0300 Subject: [PATCH] Improve error message when no extensions given to decompress Fixes #137 --- src/commands.rs | 26 +++++++++++++++----------- src/utils.rs | 15 +++++++++++++++ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index c6a89ca..8883ceb 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -19,7 +19,7 @@ use crate::{ Extension, }, info, - utils::{self, dir_is_empty, nice_directory_display, to_utf}, + utils::{self, concatenate_list_of_os_str, dir_is_empty, nice_directory_display, to_utf}, Opts, QuestionPolicy, Subcommand, }; @@ -36,8 +36,7 @@ fn represents_several_files(files: &[PathBuf]) -> bool { files.iter().any(is_non_empty_dir) || files.len() > 1 } -/// Entrypoint of ouch, receives cli options and matches Subcommand -/// to decide what to do +/// Entrypoint of ouch, receives cli options and matches Subcommand to decide what to do pub fn run(args: Opts, question_policy: QuestionPolicy) -> crate::Result<()> { match args.cmd { Subcommand::Compress { files, output: output_path } => { @@ -176,15 +175,20 @@ pub fn run(args: Opts, question_policy: QuestionPolicy) -> crate::Result<()> { .map(|(input_path, _)| PathBuf::from(input_path)) .collect(); - // Error if !files_missing_format.is_empty() { - eprintln!("Some file you asked ouch to decompress lacks a supported extension."); - eprintln!("Could not decompress {}.", to_utf(&files_missing_format[0])); - todo!( - "Dev note: add this error variant and pass the Vec to it, all the files \ - lacking extension shall be shown: {:#?}.", - files_missing_format - ); + let error = FinalError::with_title("Cannot decompress files without extensions") + .detail(format!( + "Files without supported extensions: {}", + concatenate_list_of_os_str(&files_missing_format) + )) + .detail("Decompression formats are detected automatically by the file extension") + .hint("Provide a file with a supported extension:") + .hint(" ouch decompress example.tar.gz") + .hint("") + .hint("Or overwrite this option with the '--format' flag:") + .hint(format!(" ouch decompress {} --format tar.gz", to_utf(&files_missing_format[0]))); + + return Err(error.into()); } // From Option to Option<&Path> diff --git a/src/utils.rs b/src/utils.rs index 7d19964..334186e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -72,6 +72,21 @@ pub fn to_utf(os_str: impl AsRef) -> String { text.trim_matches('"').to_string() } +/// Converts a slice of AsRef to comma separated String +/// +/// Panics if the slice is empty. +pub fn concatenate_list_of_os_str(os_strs: &[impl AsRef]) -> String { + let mut iter = os_strs.iter().map(AsRef::as_ref); + + let mut string = to_utf(iter.next().unwrap()); // May panic + + for os_str in iter { + string += ", "; + string += &to_utf(os_str); + } + string +} + /// Display the directory name, but change to "current directory" when necessary. pub fn nice_directory_display(os_str: impl AsRef) -> String { let text = to_utf(os_str);