From b938dc014ce4b2114a2ffde8c4cb5c8f67b61a81 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Thu, 2 Feb 2023 21:56:50 -0300 Subject: [PATCH] move `build_archive_file_suggestion` to `extension.rs` --- src/commands/mod.rs | 69 ++--------------------------------------------------- src/extension.rs | 57 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 67 deletions(-) diff --git a/src/commands/mod.rs b/src/commands/mod.rs index ea07640..a97e0c0 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -4,10 +4,7 @@ mod compress; mod decompress; mod list; -use std::{ - ops::ControlFlow, - path::{Path, PathBuf}, -}; +use std::{ops::ControlFlow, path::PathBuf}; use rayon::prelude::{IndexedParallelIterator, IntoParallelRefIterator, ParallelIterator}; use utils::colors; @@ -17,7 +14,7 @@ use crate::{ cli::Subcommand, commands::{compress::compress_files, decompress::decompress_file, list::list_archive_contents}, error::{Error, FinalError}, - extension::{self, parse_format, Extension}, + extension::{self, build_archive_file_suggestion, parse_format, Extension}, info, list::ListOptions, utils::{self, pretty_format_list_of_paths, to_utf, EscapedPathDisplay, FileVisibilityPolicy}, @@ -34,40 +31,6 @@ fn warn_user_about_loading_zip_in_memory() { warning!("{}", ZIP_IN_MEMORY_LIMITATION_WARNING); } -/// Builds a suggested output file in scenarios where the user tried to compress -/// a folder into a non-archive compression format, for error message purposes -/// -/// E.g.: `build_suggestion("file.bz.xz", ".tar")` results in `Some("file.tar.bz.xz")` -fn build_archive_file_suggestion(path: &Path, suggested_extension: &str) -> Option { - let path = path.to_string_lossy(); - let mut rest = &*path; - let mut position_to_insert = 0; - - // Walk through the path to find the first supported compression extension - while let Some(pos) = rest.find('.') { - // Use just the text located after the dot we found - rest = &rest[pos + 1..]; - position_to_insert += pos + 1; - - // If the string contains more chained extensions, clip to the immediate one - let maybe_extension = { - let idx = rest.find('.').unwrap_or(rest.len()); - &rest[..idx] - }; - - // If the extension we got is a supported extension, generate the suggestion - // at the position we found - if extension::SUPPORTED_EXTENSIONS.contains(&maybe_extension) { - let mut path = path.to_string(); - path.insert_str(position_to_insert - 1, suggested_extension); - - return Some(path); - } - } - - None -} - /// 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 fn check_for_non_archive_formats(files: &[PathBuf], formats: &[Vec]) -> crate::Result<()> { @@ -354,31 +317,3 @@ pub fn run( } Ok(()) } - -#[cfg(test)] -mod tests { - use std::path::Path; - - use super::build_archive_file_suggestion; - - #[test] - fn builds_suggestion_correctly() { - assert_eq!(build_archive_file_suggestion(Path::new("linux.png"), ".tar"), None); - assert_eq!( - build_archive_file_suggestion(Path::new("linux.xz.gz.zst"), ".tar").unwrap(), - "linux.tar.xz.gz.zst" - ); - assert_eq!( - build_archive_file_suggestion(Path::new("linux.pkg.xz.gz.zst"), ".tar").unwrap(), - "linux.pkg.tar.xz.gz.zst" - ); - assert_eq!( - build_archive_file_suggestion(Path::new("linux.pkg.zst"), ".tar").unwrap(), - "linux.pkg.tar.zst" - ); - assert_eq!( - build_archive_file_suggestion(Path::new("linux.pkg.info.zst"), ".tar").unwrap(), - "linux.pkg.info.tar.zst" - ); - } -} diff --git a/src/extension.rs b/src/extension.rs index 7e57b8d..016da7d 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -185,8 +185,44 @@ pub fn flatten_compression_formats(extensions: &[Extension]) -> Vec Option { + let path = path.to_string_lossy(); + let mut rest = &*path; + let mut position_to_insert = 0; + + // Walk through the path to find the first supported compression extension + while let Some(pos) = rest.find('.') { + // Use just the text located after the dot we found + rest = &rest[pos + 1..]; + position_to_insert += pos + 1; + + // If the string contains more chained extensions, clip to the immediate one + let maybe_extension = { + let idx = rest.find('.').unwrap_or(rest.len()); + &rest[..idx] + }; + + // If the extension we got is a supported extension, generate the suggestion + // at the position we found + if SUPPORTED_EXTENSIONS.contains(&maybe_extension) { + let mut path = path.to_string(); + path.insert_str(position_to_insert - 1, suggested_extension); + + return Some(path); + } + } + + None +} + #[cfg(test)] mod tests { + use std::path::Path; + use super::*; #[test] @@ -199,4 +235,25 @@ mod tests { assert_eq!(formats, vec![Tar, Gzip]); } + + #[test] + fn builds_suggestion_correctly() { + assert_eq!(build_archive_file_suggestion(Path::new("linux.png"), ".tar"), None); + assert_eq!( + build_archive_file_suggestion(Path::new("linux.xz.gz.zst"), ".tar").unwrap(), + "linux.tar.xz.gz.zst" + ); + assert_eq!( + build_archive_file_suggestion(Path::new("linux.pkg.xz.gz.zst"), ".tar").unwrap(), + "linux.pkg.tar.xz.gz.zst" + ); + assert_eq!( + build_archive_file_suggestion(Path::new("linux.pkg.zst"), ".tar").unwrap(), + "linux.pkg.tar.zst" + ); + assert_eq!( + build_archive_file_suggestion(Path::new("linux.pkg.info.zst"), ".tar").unwrap(), + "linux.pkg.info.tar.zst" + ); + } } -- 2.11.4.GIT