mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-07 12:05:46 +00:00
separate function check_invalid_compression_with_non_archive_format
This commit is contained in:
parent
93daa7b929
commit
1f4eba2bcb
59
src/check.rs
59
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(())
|
||||
}
|
||||
|
@ -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)? {
|
||||
|
Loading…
x
Reference in New Issue
Block a user