mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-07 20:15:27 +00:00
simplify check_mime_type
This commit is contained in:
parent
e78fee0d48
commit
8102260da4
13
src/check.rs
13
src/check.rs
@ -23,12 +23,11 @@ use crate::{
|
|||||||
/// TODO: maybe the name of this should be "magic numbers" or "file signature",
|
/// TODO: maybe the name of this should be "magic numbers" or "file signature",
|
||||||
/// and not MIME.
|
/// and not MIME.
|
||||||
pub fn check_mime_type(
|
pub fn check_mime_type(
|
||||||
files: &[PathBuf],
|
path: &Path,
|
||||||
formats: &mut [Vec<Extension>],
|
formats: &mut Vec<Extension>,
|
||||||
question_policy: QuestionPolicy,
|
question_policy: QuestionPolicy,
|
||||||
) -> Result<ControlFlow<()>> {
|
) -> Result<ControlFlow<()>> {
|
||||||
for (path, format) in files.iter().zip(formats.iter_mut()) {
|
if formats.is_empty() {
|
||||||
if format.is_empty() {
|
|
||||||
// File with no extension
|
// File with no extension
|
||||||
// Try to detect it automatically and prompt the user about it
|
// Try to detect it automatically and prompt the user about it
|
||||||
if let Some(detected_format) = try_infer_extension(path) {
|
if let Some(detected_format) = try_infer_extension(path) {
|
||||||
@ -41,7 +40,7 @@ pub fn check_mime_type(
|
|||||||
detected_format
|
detected_format
|
||||||
);
|
);
|
||||||
if user_wants_to_continue(path, question_policy, QuestionAction::Decompression)? {
|
if user_wants_to_continue(path, question_policy, QuestionAction::Decompression)? {
|
||||||
format.push(detected_format);
|
formats.push(detected_format);
|
||||||
} else {
|
} else {
|
||||||
return Ok(ControlFlow::Break(()));
|
return Ok(ControlFlow::Break(()));
|
||||||
}
|
}
|
||||||
@ -49,7 +48,8 @@ pub fn check_mime_type(
|
|||||||
} else if let Some(detected_format) = try_infer_extension(path) {
|
} else if let Some(detected_format) = try_infer_extension(path) {
|
||||||
// File ending with extension
|
// File ending with extension
|
||||||
// Try to detect the extension and warn the user if it differs from the written one
|
// Try to detect the extension and warn the user if it differs from the written one
|
||||||
let outer_ext = format.iter().next_back().unwrap();
|
|
||||||
|
let outer_ext = formats.iter().next_back().unwrap();
|
||||||
if !outer_ext
|
if !outer_ext
|
||||||
.compression_formats
|
.compression_formats
|
||||||
.ends_with(detected_format.compression_formats)
|
.ends_with(detected_format.compression_formats)
|
||||||
@ -68,7 +68,6 @@ pub fn check_mime_type(
|
|||||||
// to a warning and ask the user if he wants to continue decompressing.
|
// to a warning and ask the user if he wants to continue decompressing.
|
||||||
info!(accessible, "Could not detect the extension of `{}`", path.display());
|
info!(accessible, "Could not detect the extension of `{}`", path.display());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Ok(ControlFlow::Continue(()))
|
Ok(ControlFlow::Continue(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,13 +125,14 @@ pub fn run(
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for path in files.iter() {
|
for path in files.iter() {
|
||||||
let (file_output_path, file_formats) = extension::separate_known_extensions_from_name(path);
|
let (path, mut file_formats) = extension::separate_known_extensions_from_name(path);
|
||||||
output_paths.push(file_output_path);
|
|
||||||
formats.push(file_formats);
|
if let ControlFlow::Break(_) = check::check_mime_type(path, &mut file_formats, question_policy)? {
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
if let ControlFlow::Break(_) = check::check_mime_type(&files, &mut formats, question_policy)? {
|
output_paths.push(path);
|
||||||
return Ok(());
|
formats.push(file_formats);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -172,12 +173,13 @@ pub fn run(
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for path in files.iter() {
|
for path in files.iter() {
|
||||||
let file_formats = extension::extensions_from_path(path);
|
let mut file_formats = extension::extensions_from_path(path);
|
||||||
formats.push(file_formats);
|
|
||||||
|
if let ControlFlow::Break(_) = check::check_mime_type(path, &mut file_formats, question_policy)? {
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
if let ControlFlow::Break(_) = check::check_mime_type(&files, &mut formats, question_policy)? {
|
formats.push(file_formats);
|
||||||
return Ok(());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user