separate function check_archive_formats_position

This commit is contained in:
João M. Bezerra 2023-02-03 00:52:26 -03:00
parent 54ee52610a
commit fc8bc82296
2 changed files with 35 additions and 26 deletions

View File

@ -1,18 +1,22 @@
use std::{ops::ControlFlow, path::PathBuf}; use std::{
ops::ControlFlow,
path::{Path, PathBuf},
};
use crate::{ use crate::{
error::FinalError, error::FinalError,
extension,
extension::Extension, extension::Extension,
info, info,
utils::{pretty_format_list_of_paths, try_infer_extension, user_wants_to_continue}, utils::{pretty_format_list_of_paths, try_infer_extension, user_wants_to_continue, EscapedPathDisplay},
warning, QuestionAction, QuestionPolicy, warning, QuestionAction, QuestionPolicy, Result,
}; };
pub fn check_mime_type( pub fn check_mime_type(
files: &[PathBuf], files: &[PathBuf],
formats: &mut [Vec<Extension>], formats: &mut [Vec<Extension>],
question_policy: QuestionPolicy, question_policy: QuestionPolicy,
) -> crate::Result<ControlFlow<()>> { ) -> Result<ControlFlow<()>> {
for (path, format) in files.iter().zip(formats.iter_mut()) { for (path, format) in files.iter().zip(formats.iter_mut()) {
if format.is_empty() { if format.is_empty() {
// File with no extension // 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 /// 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 /// the contents of a compressed file that is not an archive
pub fn check_for_non_archive_formats(files: &[PathBuf], formats: &[Vec<Extension>]) -> crate::Result<()> { pub fn check_for_non_archive_formats(files: &[PathBuf], formats: &[Vec<Extension>]) -> Result<()> {
let mut not_archives = files let mut not_archives = files
.iter() .iter()
.zip(formats) .zip(formats)
@ -82,3 +86,27 @@ pub fn check_for_non_archive_formats(files: &[PathBuf], formats: &[Vec<Extension
Ok(()) Ok(())
} }
pub fn check_archive_formats_position(formats: &[extension::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 '{}'.",
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(())
}

View File

@ -10,7 +10,7 @@ use rayon::prelude::{IndexedParallelIterator, IntoParallelRefIterator, ParallelI
use utils::colors; use utils::colors;
use crate::{ 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, cli::Subcommand,
commands::{compress::compress_files, decompress::decompress_file, list::list_archive_contents}, commands::{compress::compress_files, decompress::decompress_file, list::list_archive_contents},
error::{Error, FinalError}, error::{Error, FinalError},
@ -117,26 +117,7 @@ pub fn run(
return Err(error.into()); return Err(error.into());
} }
if let Some(format) = formats.iter().skip(1).find(|format| format.is_archive()) { check_archive_formats_position(&formats, &output_path)?;
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());
}
let output_file = match utils::ask_to_create_file(&output_path, question_policy)? { let output_file = match utils::ask_to_create_file(&output_path, question_policy)? {
Some(writer) => writer, Some(writer) => writer,