From 99d9c09fb78f59c9df1d6bc459d48f4e862904c4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Fri, 3 Feb 2023 02:13:00 -0300 Subject: [PATCH] refac: `check_invalid_compression_with_non_archive_format` --- src/check.rs | 84 +++++++++++++++++++++++++++++++----------------------------- 1 file changed, 44 insertions(+), 40 deletions(-) diff --git a/src/check.rs b/src/check.rs index f7d0237..558e1ec 100644 --- a/src/check.rs +++ b/src/check.rs @@ -169,6 +169,8 @@ pub fn check_first_format_when_compressing<'a>(formats: &'a [Extension], output_ } /// Check if compression is invalid because an archive format is necessary. +/// +/// Non-archive formats don't support multiple file compression or folder compression. pub fn check_invalid_compression_with_non_archive_format( formats: &[Extension], output_path: &Path, @@ -180,46 +182,48 @@ pub fn check_invalid_compression_with_non_archive_format( 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 - if !first_format.is_archive() && (is_some_input_a_folder || is_multiple_inputs) { - let first_detail_message = if is_multiple_inputs { - "You are trying to compress multiple files." - } else { - "You are trying to compress a folder." - }; - - let (from_hint, to_hint) = if let Some(formats) = formats_from_flag { - let formats = formats.to_string_lossy(); - ( - format!("From: --format {formats}"), - format!("To: --format tar.{formats}"), - ) - } else { - // This piece of code creates a suggestion for compressing multiple files - // It says: - // Change from file.bz.xz - // To file.tar.bz.xz - let suggested_output_path = build_archive_file_suggestion(output_path, ".tar") - .expect("output path should contain a compression format"); - - ( - format!("From: {}", EscapedPathDisplay::new(output_path)), - format!("To: {suggested_output_path}"), - ) - }; - let output_path = EscapedPathDisplay::new(output_path); + // If format is archive, nothing to check + // If there's no folder or multiple inputs, non-archive formats can handle it + if first_format.is_archive() || !is_some_input_a_folder && !is_multiple_inputs { + return Ok(()); + } - let error = FinalError::with_title(format!("Cannot compress to '{output_path}'.")) - .detail(first_detail_message) - .detail(format!( - "The compression format '{first_format}' does not accept multiple files.", - )) - .detail("Formats that bundle files into an archive are tar and zip.") - .hint(format!("Try inserting 'tar.' or 'zip.' before '{first_format}'.")) - .hint(from_hint) - .hint(to_hint); + let first_detail_message = if is_multiple_inputs { + "You are trying to compress multiple files." + } else { + "You are trying to compress a folder." + }; + + let (from_hint, to_hint) = if let Some(formats) = formats_from_flag { + let formats = formats.to_string_lossy(); + ( + format!("From: --format {formats}"), + format!("To: --format tar.{formats}"), + ) + } else { + // This piece of code creates a suggestion for compressing multiple files + // It says: + // Change from file.bz.xz + // To file.tar.bz.xz + let suggested_output_path = build_archive_file_suggestion(output_path, ".tar") + .expect("output path should contain a compression format"); + + ( + format!("From: {}", EscapedPathDisplay::new(output_path)), + format!("To: {suggested_output_path}"), + ) + }; + let output_path = EscapedPathDisplay::new(output_path); + + let error = FinalError::with_title(format!("Cannot compress to '{output_path}'.")) + .detail(first_detail_message) + .detail(format!( + "The compression format '{first_format}' does not accept multiple files.", + )) + .detail("Formats that bundle files into an archive are tar and zip.") + .hint(format!("Try inserting 'tar.' or 'zip.' before '{first_format}'.")) + .hint(from_hint) + .hint(to_hint); - return Err(error.into()); - } - Ok(()) + Err(error.into()) } -- 2.11.4.GIT