mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-06 11:35:45 +00:00
replace flat_map by flatten_compression_formats
This commit is contained in:
parent
19769223c8
commit
f5fcf7f2a3
@ -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)?;
|
||||
|
@ -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)?;
|
||||
|
@ -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)?;
|
||||
}
|
||||
}
|
||||
|
@ -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<Item = &CompressionFormat> {
|
||||
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<Extension> = extensions_from_path(path);
|
||||
let formats: Vec<&CompressionFormat> = extensions.iter().flat_map(Extension::iter).collect::<Vec<_>>();
|
||||
let formats: Vec<CompressionFormat> = 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<CompressionFormat>) {
|
||||
let mut extensions: Vec<CompressionFormat> = formats.iter().flat_map(Extension::iter).copied().collect();
|
||||
pub fn split_first_compression_format(formats: &[Extension]) -> (CompressionFormat, Vec<CompressionFormat>) {
|
||||
let mut extensions: Vec<CompressionFormat> = flatten_compression_formats(&formats);
|
||||
let first_extension = extensions.remove(0);
|
||||
(first_extension, extensions)
|
||||
}
|
||||
|
||||
pub fn flatten_compression_formats(extensions: &[Extension]) -> Vec<CompressionFormat> {
|
||||
extensions.iter().flat_map(|extension| extension.compression_formats.iter()).copied().collect()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user