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] 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,