From 5b99f434c3628ad934e43b08bfcb809b626d4a8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Thu, 2 Feb 2023 20:11:05 -0300 Subject: [PATCH 01/14] rename `Opts` to `CliArgs` --- build.rs | 4 ++-- src/{opts.rs => cli/args.rs} | 2 +- src/{cli.rs => cli/mod.rs} | 27 +++++++++++++++------------ src/commands/mod.rs | 5 +++-- src/main.rs | 7 ++----- 5 files changed, 23 insertions(+), 22 deletions(-) rename src/{opts.rs => cli/args.rs} (99%) rename src/{cli.rs => cli/mod.rs} (65%) diff --git a/build.rs b/build.rs index 6983dc4..c7a1f91 100644 --- a/build.rs +++ b/build.rs @@ -27,7 +27,7 @@ use clap::{CommandFactory, ValueEnum}; use clap_complete::{generate_to, Shell}; use clap_mangen::Man; -include!("src/opts.rs"); +include!("src/cli/args.rs"); fn main() { println!("cargo:rerun-if-env-changed=OUCH_ARTIFACTS_FOLDER"); @@ -35,7 +35,7 @@ fn main() { if let Some(dir) = env::var_os("OUCH_ARTIFACTS_FOLDER") { let out = &Path::new(&dir); create_dir_all(out).unwrap(); - let cmd = &mut Opts::command(); + let cmd = &mut CliArgs::command(); Man::new(cmd.clone()) .render(&mut File::create(out.join("ouch.1")).unwrap()) diff --git a/src/opts.rs b/src/cli/args.rs similarity index 99% rename from src/opts.rs rename to src/cli/args.rs index 8139647..6f31969 100644 --- a/src/opts.rs +++ b/src/cli/args.rs @@ -12,7 +12,7 @@ use clap::{Parser, ValueHint}; #[command(about, version)] // Disable rustdoc::bare_urls because rustdoc parses URLs differently than Clap #[allow(rustdoc::bare_urls)] -pub struct Opts { +pub struct CliArgs { /// Skip [Y/n] questions positively #[arg(short, long, conflicts_with = "no", global = true)] pub yes: bool, diff --git a/src/cli.rs b/src/cli/mod.rs similarity index 65% rename from src/cli.rs rename to src/cli/mod.rs index ee25e41..1fd7f38 100644 --- a/src/cli.rs +++ b/src/cli/mod.rs @@ -1,4 +1,6 @@ -//! CLI related functions, uses the clap argparsing definitions from `opts.rs`. +//! CLI related functions, uses the clap argparsing definitions from `args.rs`. + +mod args; use std::{ io, @@ -9,25 +11,26 @@ use std::{ use clap::Parser; use fs_err as fs; -use crate::{accessible::set_accessible, utils::FileVisibilityPolicy, Opts, QuestionPolicy, Subcommand}; +pub use self::args::{CliArgs, Subcommand}; +use crate::{accessible::set_accessible, utils::FileVisibilityPolicy, QuestionPolicy}; -impl Opts { +impl CliArgs { /// A helper method that calls `clap::Parser::parse`. /// /// And: /// 1. Make paths absolute. /// 2. Checks the QuestionPolicy. pub fn parse_args() -> crate::Result<(Self, QuestionPolicy, FileVisibilityPolicy)> { - let mut opts = Self::parse(); + let mut args = Self::parse(); - set_accessible(opts.accessible); + set_accessible(args.accessible); let (Subcommand::Compress { files, .. } | Subcommand::Decompress { files, .. } - | Subcommand::List { archives: files, .. }) = &mut opts.cmd; + | Subcommand::List { archives: files, .. }) = &mut args.cmd; *files = canonicalize_files(files)?; - let skip_questions_positively = match (opts.yes, opts.no) { + let skip_questions_positively = match (args.yes, args.no) { (false, false) => QuestionPolicy::Ask, (true, false) => QuestionPolicy::AlwaysYes, (false, true) => QuestionPolicy::AlwaysNo, @@ -35,12 +38,12 @@ impl Opts { }; let file_visibility_policy = FileVisibilityPolicy::new() - .read_git_exclude(opts.gitignore) - .read_ignore(opts.gitignore) - .read_git_ignore(opts.gitignore) - .read_hidden(opts.hidden); + .read_git_exclude(args.gitignore) + .read_ignore(args.gitignore) + .read_git_ignore(args.gitignore) + .read_hidden(args.hidden); - Ok((opts, skip_questions_positively, file_visibility_policy)) + Ok((args, skip_questions_positively, file_visibility_policy)) } } diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 95f8cfc..f154b5b 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -13,6 +13,7 @@ use rayon::prelude::{IndexedParallelIterator, IntoParallelRefIterator, ParallelI use utils::colors; use crate::{ + cli::{CliArgs, Subcommand}, commands::{compress::compress_files, decompress::decompress_file, list::list_archive_contents}, error::{Error, FinalError}, extension::{self, flatten_compression_formats, parse_format, Extension, SUPPORTED_EXTENSIONS}, @@ -22,7 +23,7 @@ use crate::{ self, pretty_format_list_of_paths, to_utf, try_infer_extension, user_wants_to_continue, EscapedPathDisplay, FileVisibilityPolicy, }, - warning, Opts, QuestionAction, QuestionPolicy, Subcommand, + warning, QuestionAction, QuestionPolicy, }; /// Warn the user that (de)compressing this .zip archive might freeze their system. @@ -99,7 +100,7 @@ fn check_for_non_archive_formats(files: &[PathBuf], formats: &[Vec]) /// /// There are a lot of custom errors to give enough error description and explanation. pub fn run( - args: Opts, + args: CliArgs, question_policy: QuestionPolicy, file_visibility_policy: FileVisibilityPolicy, ) -> crate::Result<()> { diff --git a/src/main.rs b/src/main.rs index e10dc62..02119df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,14 +10,11 @@ pub mod extension; pub mod list; pub mod utils; -/// CLI argparsing definitions, using `clap`. -pub mod opts; - use std::{env, path::PathBuf}; +use cli::CliArgs; use error::{Error, Result}; use once_cell::sync::Lazy; -use opts::{Opts, Subcommand}; use utils::{QuestionAction, QuestionPolicy}; // Used in BufReader and BufWriter to perform less syscalls @@ -37,6 +34,6 @@ fn main() { } fn run() -> Result<()> { - let (args, skip_questions_positively, file_visibility_policy) = Opts::parse_args()?; + let (args, skip_questions_positively, file_visibility_policy) = CliArgs::parse_args()?; commands::run(args, skip_questions_positively, file_visibility_policy) } From d2db26a59dcf39aec900741630aa0f0c9f3e807f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Thu, 2 Feb 2023 20:14:10 -0300 Subject: [PATCH 02/14] create `check.rs` --- src/check.rs | 0 src/main.rs | 1 + 2 files changed, 1 insertion(+) create mode 100644 src/check.rs diff --git a/src/check.rs b/src/check.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/main.rs b/src/main.rs index 02119df..46619bf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ pub mod macros; pub mod accessible; pub mod archive; +pub mod check; pub mod cli; pub mod commands; pub mod error; From aad55e610216921c785320bfe75c613191d90a3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Thu, 2 Feb 2023 21:52:32 -0300 Subject: [PATCH 03/14] move `check_mime_type` to `check.rs` --- src/check.rs | 58 +++++++++++++++++++++++++++++++++++++++ src/commands/mod.rs | 66 +++++---------------------------------------- 2 files changed, 65 insertions(+), 59 deletions(-) diff --git a/src/check.rs b/src/check.rs index e69de29..ce56a89 100644 --- a/src/check.rs +++ b/src/check.rs @@ -0,0 +1,58 @@ +use std::{ops::ControlFlow, path::PathBuf}; + +use crate::{ + extension::Extension, + info, + utils::{try_infer_extension, user_wants_to_continue}, + warning, QuestionAction, QuestionPolicy, +}; + +pub fn check_mime_type( + files: &[PathBuf], + formats: &mut [Vec], + question_policy: QuestionPolicy, +) -> crate::Result> { + for (path, format) in files.iter().zip(formats.iter_mut()) { + if format.is_empty() { + // File with no extension + // Try to detect it automatically and prompt the user about it + if let Some(detected_format) = try_infer_extension(path) { + // Inferring the file extension can have unpredicted consequences (e.g. the user just + // mistyped, ...) which we should always inform the user about. + info!( + accessible, + "Detected file: `{}` extension as `{}`", + path.display(), + detected_format + ); + if user_wants_to_continue(path, question_policy, QuestionAction::Decompression)? { + format.push(detected_format); + } else { + return Ok(ControlFlow::Break(())); + } + } + } else if let Some(detected_format) = try_infer_extension(path) { + // File ending with extension + // Try to detect the extension and warn the user if it differs from the written one + let outer_ext = format.iter().next_back().unwrap(); + if !outer_ext + .compression_formats + .ends_with(detected_format.compression_formats) + { + warning!( + "The file extension: `{}` differ from the detected extension: `{}`", + outer_ext, + detected_format + ); + if !user_wants_to_continue(path, question_policy, QuestionAction::Decompression)? { + return Ok(ControlFlow::Break(())); + } + } + } else { + // NOTE: If this actually produces no false positives, we can upgrade it in the future + // to a warning and ask the user if he wants to continue decompressing. + info!(accessible, "Could not detect the extension of `{}`", path.display()); + } + } + Ok(ControlFlow::Continue(())) +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index f154b5b..ea07640 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -13,17 +13,15 @@ use rayon::prelude::{IndexedParallelIterator, IntoParallelRefIterator, ParallelI use utils::colors; use crate::{ - cli::{CliArgs, Subcommand}, + check::check_mime_type, + cli::Subcommand, commands::{compress::compress_files, decompress::decompress_file, list::list_archive_contents}, error::{Error, FinalError}, - extension::{self, flatten_compression_formats, parse_format, Extension, SUPPORTED_EXTENSIONS}, + extension::{self, parse_format, Extension}, info, list::ListOptions, - utils::{ - self, pretty_format_list_of_paths, to_utf, try_infer_extension, user_wants_to_continue, EscapedPathDisplay, - FileVisibilityPolicy, - }, - warning, QuestionAction, QuestionPolicy, + utils::{self, pretty_format_list_of_paths, to_utf, EscapedPathDisplay, FileVisibilityPolicy}, + warning, CliArgs, QuestionPolicy, }; /// Warn the user that (de)compressing this .zip archive might freeze their system. @@ -59,7 +57,7 @@ fn build_archive_file_suggestion(path: &Path, suggested_extension: &str) -> Opti // If the extension we got is a supported extension, generate the suggestion // at the position we found - if SUPPORTED_EXTENSIONS.contains(&maybe_extension) { + if extension::SUPPORTED_EXTENSIONS.contains(&maybe_extension) { let mut path = path.to_string(); path.insert_str(position_to_insert - 1, suggested_extension); @@ -349,7 +347,7 @@ pub fn run( if i > 0 { println!(); } - let formats = flatten_compression_formats(&formats); + let formats = extension::flatten_compression_formats(&formats); list_archive_contents(archive_path, formats, list_options, question_policy)?; } } @@ -357,56 +355,6 @@ pub fn run( Ok(()) } -fn check_mime_type( - files: &[PathBuf], - formats: &mut [Vec], - question_policy: QuestionPolicy, -) -> crate::Result> { - for (path, format) in files.iter().zip(formats.iter_mut()) { - if format.is_empty() { - // File with no extension - // Try to detect it automatically and prompt the user about it - if let Some(detected_format) = try_infer_extension(path) { - // Inferring the file extension can have unpredicted consequences (e.g. the user just - // mistyped, ...) which we should always inform the user about. - info!( - accessible, - "Detected file: `{}` extension as `{}`", - path.display(), - detected_format - ); - if user_wants_to_continue(path, question_policy, QuestionAction::Decompression)? { - format.push(detected_format); - } else { - return Ok(ControlFlow::Break(())); - } - } - } else if let Some(detected_format) = try_infer_extension(path) { - // File ending with extension - // Try to detect the extension and warn the user if it differs from the written one - let outer_ext = format.iter().next_back().unwrap(); - if !outer_ext - .compression_formats - .ends_with(detected_format.compression_formats) - { - warning!( - "The file extension: `{}` differ from the detected extension: `{}`", - outer_ext, - detected_format - ); - if !user_wants_to_continue(path, question_policy, QuestionAction::Decompression)? { - return Ok(ControlFlow::Break(())); - } - } - } else { - // NOTE: If this actually produces no false positives, we can upgrade it in the future - // to a warning and ask the user if he wants to continue decompressing. - info!(accessible, "Could not detect the extension of `{}`", path.display()); - } - } - Ok(ControlFlow::Continue(())) -} - #[cfg(test)] mod tests { use std::path::Path; From 7f763ff500532aa24f7e47571567543e1fbc92a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Thu, 2 Feb 2023 21:55:16 -0300 Subject: [PATCH 04/14] move module `tests` to end of file --- src/extension.rs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/extension.rs b/src/extension.rs index 46b27dd..7e57b8d 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -170,6 +170,21 @@ pub fn extensions_from_path(path: &Path) -> Vec { extensions } +// Panics if formats has an empty list of compression formats +pub fn split_first_compression_format(formats: &[Extension]) -> (CompressionFormat, Vec) { + let mut extensions: Vec = flatten_compression_formats(formats); + let first_extension = extensions.remove(0); + (first_extension, extensions) +} + +pub fn flatten_compression_formats(extensions: &[Extension]) -> Vec { + extensions + .iter() + .flat_map(|extension| extension.compression_formats.iter()) + .copied() + .collect() +} + #[cfg(test)] mod tests { use super::*; @@ -185,18 +200,3 @@ mod tests { assert_eq!(formats, vec![Tar, Gzip]); } } - -// Panics if formats has an empty list of compression formats -pub fn split_first_compression_format(formats: &[Extension]) -> (CompressionFormat, Vec) { - let mut extensions: Vec = flatten_compression_formats(formats); - let first_extension = extensions.remove(0); - (first_extension, extensions) -} - -pub fn flatten_compression_formats(extensions: &[Extension]) -> Vec { - extensions - .iter() - .flat_map(|extension| extension.compression_formats.iter()) - .copied() - .collect() -} From b938dc014ce4b2114a2ffde8c4cb5c8f67b61a81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Thu, 2 Feb 2023 21:56:50 -0300 Subject: [PATCH 05/14] move `build_archive_file_suggestion` to `extension.rs` --- src/commands/mod.rs | 69 ++------------------------------------------- src/extension.rs | 57 +++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 67 deletions(-) diff --git a/src/commands/mod.rs b/src/commands/mod.rs index ea07640..a97e0c0 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -4,10 +4,7 @@ mod compress; mod decompress; mod list; -use std::{ - ops::ControlFlow, - path::{Path, PathBuf}, -}; +use std::{ops::ControlFlow, path::PathBuf}; use rayon::prelude::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator}; use utils::colors; @@ -17,7 +14,7 @@ use crate::{ cli::Subcommand, commands::{compress::compress_files, decompress::decompress_file, list::list_archive_contents}, error::{Error, FinalError}, - extension::{self, parse_format, Extension}, + extension::{self, build_archive_file_suggestion, parse_format, Extension}, info, list::ListOptions, utils::{self, pretty_format_list_of_paths, to_utf, EscapedPathDisplay, FileVisibilityPolicy}, @@ -34,40 +31,6 @@ fn warn_user_about_loading_zip_in_memory() { warning!("{}", ZIP_IN_MEMORY_LIMITATION_WARNING); } -/// Builds a suggested output file in scenarios where the user tried to compress -/// a folder into a non-archive compression format, for error message purposes -/// -/// E.g.: `build_suggestion("file.bz.xz", ".tar")` results in `Some("file.tar.bz.xz")` -fn build_archive_file_suggestion(path: &Path, suggested_extension: &str) -> Option { - let path = path.to_string_lossy(); - let mut rest = &*path; - let mut position_to_insert = 0; - - // Walk through the path to find the first supported compression extension - while let Some(pos) = rest.find('.') { - // Use just the text located after the dot we found - rest = &rest[pos + 1..]; - position_to_insert += pos + 1; - - // If the string contains more chained extensions, clip to the immediate one - let maybe_extension = { - let idx = rest.find('.').unwrap_or(rest.len()); - &rest[..idx] - }; - - // If the extension we got is a supported extension, generate the suggestion - // at the position we found - if extension::SUPPORTED_EXTENSIONS.contains(&maybe_extension) { - let mut path = path.to_string(); - path.insert_str(position_to_insert - 1, suggested_extension); - - return Some(path); - } - } - - None -} - /// In the context of listing archives, this function checks if `ouch` was told to list /// the contents of a compressed file that is not an archive fn check_for_non_archive_formats(files: &[PathBuf], formats: &[Vec]) -> crate::Result<()> { @@ -354,31 +317,3 @@ pub fn run( } Ok(()) } - -#[cfg(test)] -mod tests { - use std::path::Path; - - use super::build_archive_file_suggestion; - - #[test] - fn builds_suggestion_correctly() { - assert_eq!(build_archive_file_suggestion(Path::new("linux.png"), ".tar"), None); - assert_eq!( - build_archive_file_suggestion(Path::new("linux.xz.gz.zst"), ".tar").unwrap(), - "linux.tar.xz.gz.zst" - ); - assert_eq!( - build_archive_file_suggestion(Path::new("linux.pkg.xz.gz.zst"), ".tar").unwrap(), - "linux.pkg.tar.xz.gz.zst" - ); - assert_eq!( - build_archive_file_suggestion(Path::new("linux.pkg.zst"), ".tar").unwrap(), - "linux.pkg.tar.zst" - ); - assert_eq!( - build_archive_file_suggestion(Path::new("linux.pkg.info.zst"), ".tar").unwrap(), - "linux.pkg.info.tar.zst" - ); - } -} diff --git a/src/extension.rs b/src/extension.rs index 7e57b8d..016da7d 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -185,8 +185,44 @@ pub fn flatten_compression_formats(extensions: &[Extension]) -> Vec Option { + let path = path.to_string_lossy(); + let mut rest = &*path; + let mut position_to_insert = 0; + + // Walk through the path to find the first supported compression extension + while let Some(pos) = rest.find('.') { + // Use just the text located after the dot we found + rest = &rest[pos + 1..]; + position_to_insert += pos + 1; + + // If the string contains more chained extensions, clip to the immediate one + let maybe_extension = { + let idx = rest.find('.').unwrap_or(rest.len()); + &rest[..idx] + }; + + // If the extension we got is a supported extension, generate the suggestion + // at the position we found + if SUPPORTED_EXTENSIONS.contains(&maybe_extension) { + let mut path = path.to_string(); + path.insert_str(position_to_insert - 1, suggested_extension); + + return Some(path); + } + } + + None +} + #[cfg(test)] mod tests { + use std::path::Path; + use super::*; #[test] @@ -199,4 +235,25 @@ mod tests { assert_eq!(formats, vec![Tar, Gzip]); } + + #[test] + fn builds_suggestion_correctly() { + assert_eq!(build_archive_file_suggestion(Path::new("linux.png"), ".tar"), None); + assert_eq!( + build_archive_file_suggestion(Path::new("linux.xz.gz.zst"), ".tar").unwrap(), + "linux.tar.xz.gz.zst" + ); + assert_eq!( + build_archive_file_suggestion(Path::new("linux.pkg.xz.gz.zst"), ".tar").unwrap(), + "linux.pkg.tar.xz.gz.zst" + ); + assert_eq!( + build_archive_file_suggestion(Path::new("linux.pkg.zst"), ".tar").unwrap(), + "linux.pkg.tar.zst" + ); + assert_eq!( + build_archive_file_suggestion(Path::new("linux.pkg.info.zst"), ".tar").unwrap(), + "linux.pkg.info.tar.zst" + ); + } } From 6710987b38f34505913529b4734dd26b5f80e0e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Fri, 3 Feb 2023 00:39:02 -0300 Subject: [PATCH 06/14] move `check_for_non_archive_formats` to `check.rs` --- src/check.rs | 28 +++++++++++++++++++++++++++- src/commands/mod.rs | 29 ++--------------------------- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/check.rs b/src/check.rs index ce56a89..dfca955 100644 --- a/src/check.rs +++ b/src/check.rs @@ -1,9 +1,10 @@ use std::{ops::ControlFlow, path::PathBuf}; use crate::{ + error::FinalError, extension::Extension, info, - utils::{try_infer_extension, user_wants_to_continue}, + utils::{pretty_format_list_of_paths, try_infer_extension, user_wants_to_continue}, warning, QuestionAction, QuestionPolicy, }; @@ -56,3 +57,28 @@ pub fn check_mime_type( } Ok(ControlFlow::Continue(())) } + +/// In the context of listing archives, this function checks if `ouch` was told to list +/// the contents of a compressed file that is not an archive +pub fn check_for_non_archive_formats(files: &[PathBuf], formats: &[Vec]) -> crate::Result<()> { + let mut not_archives = files + .iter() + .zip(formats) + .filter(|(_, formats)| !formats.first().map(Extension::is_archive).unwrap_or(false)) + .map(|(path, _)| path) + .peekable(); + + if not_archives.peek().is_some() { + let not_archives: Vec<_> = not_archives.collect(); + let error = FinalError::with_title("Cannot list archive contents") + .detail("Only archives can have their contents listed") + .detail(format!( + "Files are not archives: {}", + pretty_format_list_of_paths(¬_archives) + )); + + return Err(error.into()); + } + + Ok(()) +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index a97e0c0..2c89ecc 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -10,11 +10,11 @@ use rayon::prelude::{IndexedParallelIterator, IntoParallelRefIterator, ParallelI use utils::colors; use crate::{ - check::check_mime_type, + check::{check_for_non_archive_formats, check_mime_type}, cli::Subcommand, commands::{compress::compress_files, decompress::decompress_file, list::list_archive_contents}, error::{Error, FinalError}, - extension::{self, build_archive_file_suggestion, parse_format, Extension}, + extension::{self, build_archive_file_suggestion, parse_format}, info, list::ListOptions, utils::{self, pretty_format_list_of_paths, to_utf, EscapedPathDisplay, FileVisibilityPolicy}, @@ -31,31 +31,6 @@ fn warn_user_about_loading_zip_in_memory() { warning!("{}", ZIP_IN_MEMORY_LIMITATION_WARNING); } -/// In the context of listing archives, this function checks if `ouch` was told to list -/// the contents of a compressed file that is not an archive -fn check_for_non_archive_formats(files: &[PathBuf], formats: &[Vec]) -> crate::Result<()> { - let mut not_archives = files - .iter() - .zip(formats) - .filter(|(_, formats)| !formats.first().map(Extension::is_archive).unwrap_or(false)) - .map(|(path, _)| path) - .peekable(); - - if not_archives.peek().is_some() { - let not_archives: Vec<_> = not_archives.collect(); - let error = FinalError::with_title("Cannot list archive contents") - .detail("Only archives can have their contents listed") - .detail(format!( - "Files are not archives: {}", - pretty_format_list_of_paths(¬_archives) - )); - - return Err(error.into()); - } - - Ok(()) -} - /// This function checks what command needs to be run and performs A LOT of ahead-of-time checks /// to assume everything is OK. /// From 54ee52610a2ef10ff72d940a01ba865455c33a79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Fri, 3 Feb 2023 00:39:14 -0300 Subject: [PATCH 07/14] fix rustdoc warnings --- src/utils/formatting.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/formatting.rs b/src/utils/formatting.rs index cf662f9..7a409e5 100644 --- a/src/utils/formatting.rs +++ b/src/utils/formatting.rs @@ -62,7 +62,7 @@ pub fn strip_cur_dir(source_path: &Path) -> &Path { source_path.strip_prefix(current_dir).unwrap_or(source_path) } -/// Converts a slice of AsRef to comma separated String +/// Converts a slice of `AsRef` to comma separated String /// /// Panics if the slice is empty. pub fn pretty_format_list_of_paths(os_strs: &[impl AsRef]) -> String { From fc8bc82296792bb78bc70e694b65ee17889403f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Fri, 3 Feb 2023 00:52:26 -0300 Subject: [PATCH 08/14] separate function `check_archive_formats_position` --- src/check.rs | 38 +++++++++++++++++++++++++++++++++----- src/commands/mod.rs | 23 ++--------------------- 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/src/check.rs b/src/check.rs index dfca955..058b7e8 100644 --- a/src/check.rs +++ b/src/check.rs @@ -1,18 +1,22 @@ -use std::{ops::ControlFlow, path::PathBuf}; +use std::{ + ops::ControlFlow, + path::{Path, PathBuf}, +}; use crate::{ error::FinalError, + extension, extension::Extension, info, - utils::{pretty_format_list_of_paths, try_infer_extension, user_wants_to_continue}, - warning, QuestionAction, QuestionPolicy, + utils::{pretty_format_list_of_paths, try_infer_extension, user_wants_to_continue, EscapedPathDisplay}, + warning, QuestionAction, QuestionPolicy, Result, }; pub fn check_mime_type( files: &[PathBuf], formats: &mut [Vec], question_policy: QuestionPolicy, -) -> crate::Result> { +) -> Result> { for (path, format) in files.iter().zip(formats.iter_mut()) { if format.is_empty() { // File with no extension @@ -60,7 +64,7 @@ pub fn check_mime_type( /// In the context of listing archives, this function checks if `ouch` was told to list /// the contents of a compressed file that is not an archive -pub fn check_for_non_archive_formats(files: &[PathBuf], formats: &[Vec]) -> crate::Result<()> { +pub fn check_for_non_archive_formats(files: &[PathBuf], formats: &[Vec]) -> Result<()> { let mut not_archives = files .iter() .zip(formats) @@ -82,3 +86,27 @@ pub fn check_for_non_archive_formats(files: &[PathBuf], formats: &[Vec Result<()> { + if let Some(format) = formats.iter().skip(1).find(|format| format.is_archive()) { + let error = FinalError::with_title(format!( + "Cannot compress to '{}'.", + EscapedPathDisplay::new(output_path) + )) + .detail(format!("Found the format '{format}' in an incorrect position.")) + .detail(format!( + "'{format}' can only be used at the start of the file extension." + )) + .hint(format!( + "If you wish to compress multiple files, start the extension with '{format}'." + )) + .hint(format!( + "Otherwise, remove the last '{}' from '{}'.", + format, + EscapedPathDisplay::new(output_path) + )); + + return Err(error.into()); + } + Ok(()) +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 2c89ecc..ddaf971 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -10,7 +10,7 @@ use rayon::prelude::{IndexedParallelIterator, IntoParallelRefIterator, ParallelI use utils::colors; use crate::{ - check::{check_for_non_archive_formats, check_mime_type}, + check::{check_archive_formats_position, check_for_non_archive_formats, check_mime_type}, cli::Subcommand, commands::{compress::compress_files, decompress::decompress_file, list::list_archive_contents}, error::{Error, FinalError}, @@ -117,26 +117,7 @@ pub fn run( return Err(error.into()); } - if let Some(format) = formats.iter().skip(1).find(|format| format.is_archive()) { - let error = FinalError::with_title(format!( - "Cannot compress to '{}'.", - EscapedPathDisplay::new(&output_path) - )) - .detail(format!("Found the format '{format}' in an incorrect position.")) - .detail(format!( - "'{format}' can only be used at the start of the file extension." - )) - .hint(format!( - "If you wish to compress multiple files, start the extension with '{format}'." - )) - .hint(format!( - "Otherwise, remove the last '{}' from '{}'.", - format, - EscapedPathDisplay::new(&output_path) - )); - - return Err(error.into()); - } + check_archive_formats_position(&formats, &output_path)?; let output_file = match utils::ask_to_create_file(&output_path, question_policy)? { Some(writer) => writer, From 3748e1d31ec5afcf06790ccd7a170fdcc5a71b49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Fri, 3 Feb 2023 01:10:25 -0300 Subject: [PATCH 09/14] add `#![warn(missing_docs)]` to `check.rs` --- src/check.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/check.rs b/src/check.rs index 058b7e8..b5b5f46 100644 --- a/src/check.rs +++ b/src/check.rs @@ -1,3 +1,7 @@ +//! Checks for errors. + +#![warn(missing_docs)] + use std::{ ops::ControlFlow, path::{Path, PathBuf}, @@ -12,6 +16,12 @@ use crate::{ warning, QuestionAction, QuestionPolicy, Result, }; +/// Check, for each file, if the mime type matches the detected extensions. +/// +/// In case the file doesn't has any extensions, try to infer the format. +/// +/// TODO: maybe the name of this should be "magic numbers" or "file signature", +/// and not MIME. pub fn check_mime_type( files: &[PathBuf], formats: &mut [Vec], @@ -87,6 +97,7 @@ pub fn check_for_non_archive_formats(files: &[PathBuf], formats: &[Vec Result<()> { if let Some(format) = formats.iter().skip(1).find(|format| format.is_archive()) { let error = FinalError::with_title(format!( From db62f1c53413f60171574f1b610b472d1a5b82d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Fri, 3 Feb 2023 01:26:50 -0300 Subject: [PATCH 10/14] `--help`: add `.sz` to list of supported formats --- src/cli/args.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli/args.rs b/src/cli/args.rs index 6f31969..d310864 100644 --- a/src/cli/args.rs +++ b/src/cli/args.rs @@ -5,7 +5,7 @@ use clap::{Parser, ValueHint}; // Ouch command line options (docstrings below are part of --help) /// A command-line utility for easily compressing and decompressing files and directories. /// -/// Supported formats: tar, zip, bz/bz2, gz, lz4, xz/lzma, zst. +/// Supported formats: tar, zip, gz, xz/lzma, bz/bz2, lz4, sz, zst. /// /// Repository: https://github.com/ouch-org/ouch #[derive(Parser, Debug)] From f33c9c0f39455793da901fd7c39f396c111da7c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Fri, 3 Feb 2023 01:42:45 -0300 Subject: [PATCH 11/14] separate function `check_missing_formats_when_decompressing` --- src/check.rs | 33 +++++++++++++++++++++++++++++++-- src/commands/mod.rs | 38 +++++++------------------------------- 2 files changed, 38 insertions(+), 33 deletions(-) diff --git a/src/check.rs b/src/check.rs index b5b5f46..e84650c 100644 --- a/src/check.rs +++ b/src/check.rs @@ -9,7 +9,6 @@ use std::{ use crate::{ error::FinalError, - extension, extension::Extension, info, utils::{pretty_format_list_of_paths, try_infer_extension, user_wants_to_continue, EscapedPathDisplay}, @@ -98,7 +97,7 @@ pub fn check_for_non_archive_formats(files: &[PathBuf], formats: &[Vec Result<()> { +pub fn check_archive_formats_position(formats: &[Extension], output_path: &Path) -> Result<()> { if let Some(format) = formats.iter().skip(1).find(|format| format.is_archive()) { let error = FinalError::with_title(format!( "Cannot compress to '{}'.", @@ -121,3 +120,33 @@ pub fn check_archive_formats_position(formats: &[extension::Extension], output_p } Ok(()) } + +/// Check if all provided files have formats to decompress. +pub fn check_missing_formats_when_decompressing(files: &[PathBuf], formats: &[Vec]) -> Result<()> { + let files_missing_format: Vec = files + .iter() + .zip(formats) + .filter(|(_, format)| format.is_empty()) + .map(|(input_path, _)| PathBuf::from(input_path)) + .collect(); + + if let Some(path) = files_missing_format.first() { + let error = FinalError::with_title("Cannot decompress files without extensions") + .detail(format!( + "Files without supported extensions: {}", + pretty_format_list_of_paths(&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", + EscapedPathDisplay::new(path), + )); + + return Err(error.into()); + } + Ok(()) +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index ddaf971..9543d44 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -10,14 +10,14 @@ use rayon::prelude::{IndexedParallelIterator, IntoParallelRefIterator, ParallelI use utils::colors; use crate::{ - check::{check_archive_formats_position, check_for_non_archive_formats, check_mime_type}, + check, cli::Subcommand, commands::{compress::compress_files, decompress::decompress_file, list::list_archive_contents}, error::{Error, FinalError}, extension::{self, build_archive_file_suggestion, parse_format}, info, list::ListOptions, - utils::{self, pretty_format_list_of_paths, to_utf, EscapedPathDisplay, FileVisibilityPolicy}, + utils::{self, to_utf, EscapedPathDisplay, FileVisibilityPolicy}, warning, CliArgs, QuestionPolicy, }; @@ -117,7 +117,7 @@ pub fn run( return Err(error.into()); } - check_archive_formats_position(&formats, &output_path)?; + check::check_archive_formats_position(&formats, &output_path)?; let output_file = match utils::ask_to_create_file(&output_path, question_policy)? { Some(writer) => writer, @@ -183,35 +183,11 @@ pub fn run( } } - if let ControlFlow::Break(_) = check_mime_type(&files, &mut formats, question_policy)? { + if let ControlFlow::Break(_) = check::check_mime_type(&files, &mut formats, question_policy)? { return Ok(()); } - let files_missing_format: Vec = files - .iter() - .zip(&formats) - .filter(|(_, formats)| formats.is_empty()) - .map(|(input_path, _)| PathBuf::from(input_path)) - .collect(); - - if let Some(path) = files_missing_format.first() { - let error = FinalError::with_title("Cannot decompress files without extensions") - .detail(format!( - "Files without supported extensions: {}", - pretty_format_list_of_paths(&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", - EscapedPathDisplay::new(path), - )); - - return Err(error.into()); - } + check::check_missing_formats_when_decompressing(&files, &formats)?; // The directory that will contain the output files // We default to the current directory if the user didn't specify an output directory with --dir @@ -252,13 +228,13 @@ pub fn run( formats.push(file_formats); } - if let ControlFlow::Break(_) = check_mime_type(&files, &mut formats, question_policy)? { + if let ControlFlow::Break(_) = check::check_mime_type(&files, &mut formats, question_policy)? { return Ok(()); } } // Ensure we were not told to list the content of a non-archive compressed file - check_for_non_archive_formats(&files, &formats)?; + check::check_for_non_archive_formats(&files, &formats)?; let list_options = ListOptions { tree }; 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 12/14] 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; From 1f4eba2bcb36fa94603446d1dc29ed52127b5ef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Fri, 3 Feb 2023 02:07:49 -0300 Subject: [PATCH 13/14] separate function `check_invalid_compression_with_non_archive_format` --- src/check.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++- src/commands/mod.rs | 56 ++++++------------------------------------ 2 files changed, 65 insertions(+), 50 deletions(-) diff --git a/src/check.rs b/src/check.rs index dd5b6a0..f7d0237 100644 --- a/src/check.rs +++ b/src/check.rs @@ -3,13 +3,14 @@ #![warn(missing_docs)] use std::{ + ffi::OsString, ops::ControlFlow, path::{Path, PathBuf}, }; use crate::{ error::FinalError, - extension::Extension, + extension::{build_archive_file_suggestion, Extension}, info, utils::{pretty_format_list_of_paths, try_infer_extension, user_wants_to_continue, EscapedPathDisplay}, warning, QuestionAction, QuestionPolicy, Result, @@ -166,3 +167,59 @@ pub fn check_first_format_when_compressing<'a>(formats: &'a [Extension], output_ .into() }) } + +/// Check if compression is invalid because an archive format is necessary. +pub fn check_invalid_compression_with_non_archive_format( + formats: &[Extension], + output_path: &Path, + files: &[PathBuf], + formats_from_flag: Option<&OsString>, +) -> Result<()> { + let first_format = 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; + + // 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); + + 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(()) +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 2c0cdaf..483ec25 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -14,7 +14,7 @@ use crate::{ cli::Subcommand, commands::{compress::compress_files, decompress::decompress_file, list::list_archive_contents}, error::{Error, FinalError}, - extension::{self, build_archive_file_suggestion, parse_format}, + extension::{self, parse_format}, info, list::ListOptions, utils::{self, to_utf, EscapedPathDisplay, FileVisibilityPolicy}, @@ -59,54 +59,12 @@ pub fn run( None => (None, extension::extensions_from_path(&output_path)), }; - 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; - - // If first format is not archive, can't compress folder, or multiple files - // Index safety: empty formats should be checked above. - 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); - - 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()); - } - + check::check_invalid_compression_with_non_archive_format( + &formats, + &output_path, + &files, + formats_from_flag.as_ref(), + )?; check::check_archive_formats_position(&formats, &output_path)?; let output_file = match utils::ask_to_create_file(&output_path, question_policy)? { From 99d9c09fb78f59c9df1d6bc459d48f4e862904c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Fri, 3 Feb 2023 02:13:00 -0300 Subject: [PATCH 14/14] refac: `check_invalid_compression_with_non_archive_format` --- src/check.rs | 86 +++++++++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 41 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); - - 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()); + // 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(()); } - Ok(()) + + 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); + + Err(error.into()) }