From f5fcf7f2a39f9f9bf3d7770eaab5998c84996d00 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jo=C3=A3o=20M=2E=20Bezerra?= Date: Sat, 4 Jun 2022 13:01:24 -0300 Subject: [PATCH] replace flat_map by `flatten_compression_formats` --- src/commands/compress.rs | 4 ++-- src/commands/decompress.rs | 4 ++-- src/commands/mod.rs | 4 ++-- src/extension.rs | 17 ++++++++--------- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/commands/compress.rs b/src/commands/compress.rs index 3969509..4186996 100644 --- a/src/commands/compress.rs +++ b/src/commands/compress.rs @@ -9,7 +9,7 @@ use crate::{ archive, commands::warn_user_about_in_memory_zip_compression, extension::{ - split_first_extension, + split_first_compression_format, CompressionFormat::{self, *}, Extension, }, @@ -70,7 +70,7 @@ pub fn compress_files( Ok(encoder) }; - let (first_extension, extensions) = split_first_extension(&formats); + let (first_extension, extensions) = split_first_compression_format(&formats); for format in extensions.iter().rev() { writer = chain_writer_encoder(format, writer)?; diff --git a/src/commands/decompress.rs b/src/commands/decompress.rs index 82bd5c4..8cff065 100644 --- a/src/commands/decompress.rs +++ b/src/commands/decompress.rs @@ -9,7 +9,7 @@ use fs_err as fs; use crate::{ commands::warn_user_about_in_memory_zip_decompression, extension::{ - split_first_extension, + split_first_compression_format, CompressionFormat::{self, *}, Extension, }, @@ -95,7 +95,7 @@ pub fn decompress_file( Ok(decoder) }; - let (first_extension, extensions) = split_first_extension(&formats); + let (first_extension, extensions) = split_first_compression_format(&formats); for format in extensions.iter().rev() { reader = chain_reader_decoder(format, reader)?; diff --git a/src/commands/mod.rs b/src/commands/mod.rs index dfe8c8c..790d370 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -18,7 +18,7 @@ use utils::colors; use crate::{ commands::{compress::compress_files, decompress::decompress_file, list::list_archive_contents}, error::FinalError, - extension::{self, Extension}, + extension::{self, flatten_compression_formats, Extension}, info, list::ListOptions, utils::{ @@ -296,7 +296,7 @@ pub fn run( if i > 0 { println!(); } - let formats = formats.iter().flat_map(Extension::iter).map(Clone::clone).collect(); + let formats = flatten_compression_formats(&formats); list_archive_contents(archive_path, formats, list_options, question_policy)?; } } diff --git a/src/extension.rs b/src/extension.rs index 580599c..92c255d 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -33,11 +33,6 @@ impl Extension { // Safety: we check that `compression_formats` is not empty in `Self::new` self.compression_formats[0].is_archive_format() } - - /// Iteration to inner compression formats, useful for flat_mapping - pub fn iter(&self) -> impl Iterator { - self.compression_formats.iter() - } } impl fmt::Display for Extension { @@ -166,15 +161,19 @@ mod tests { let path = Path::new("bolovo.tar.gz"); let extensions: Vec = extensions_from_path(path); - let formats: Vec<&CompressionFormat> = extensions.iter().flat_map(Extension::iter).collect::>(); + let formats: Vec = flatten_compression_formats(&extensions); - assert_eq!(formats, vec![&Tar, &Gzip]); + assert_eq!(formats, vec![Tar, Gzip]); } } // Panics if formats has an empty list of compression formats -pub fn split_first_extension(formats: &[Extension]) -> (CompressionFormat, Vec) { - let mut extensions: Vec = formats.iter().flat_map(Extension::iter).copied().collect(); +pub fn split_first_compression_format(formats: &[Extension]) -> (CompressionFormat, Vec) { + let mut extensions: Vec = flatten_compression_formats(&formats); let first_extension = extensions.remove(0); (first_extension, extensions) } + +pub fn flatten_compression_formats(extensions: &[Extension]) -> Vec { + extensions.iter().flat_map(|extension| extension.compression_formats.iter()).copied().collect() +} -- 2.11.4.GIT