diff --git a/src/check.rs b/src/check.rs index 558e1ec..c5aff1e 100644 --- a/src/check.rs +++ b/src/check.rs @@ -23,51 +23,50 @@ use crate::{ /// 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], + path: &Path, + formats: &mut Vec, question_policy: QuestionPolicy, ) -> 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(())); - } + if formats.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)? { + formats.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()); } + } 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 = formats.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 0c9fd05..a96983d 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -125,13 +125,14 @@ pub fn run( } } else { for path in files.iter() { - let (file_output_path, file_formats) = extension::separate_known_extensions_from_name(path); - output_paths.push(file_output_path); - formats.push(file_formats); - } + let (path, mut file_formats) = extension::separate_known_extensions_from_name(path); - if let ControlFlow::Break(_) = check::check_mime_type(&files, &mut formats, question_policy)? { - return Ok(()); + if let ControlFlow::Break(_) = check::check_mime_type(path, &mut file_formats, question_policy)? { + return Ok(()); + } + + output_paths.push(path); + formats.push(file_formats); } } @@ -172,12 +173,13 @@ pub fn run( } } else { for path in files.iter() { - let file_formats = extension::extensions_from_path(path); - formats.push(file_formats); - } + let mut file_formats = extension::extensions_from_path(path); - if let ControlFlow::Break(_) = check::check_mime_type(&files, &mut formats, question_policy)? { - return Ok(()); + if let ControlFlow::Break(_) = check::check_mime_type(path, &mut file_formats, question_policy)? { + return Ok(()); + } + + formats.push(file_formats); } }