mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-07 12:05:46 +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,
|
archive,
|
||||||
commands::warn_user_about_in_memory_zip_compression,
|
commands::warn_user_about_in_memory_zip_compression,
|
||||||
extension::{
|
extension::{
|
||||||
split_first_extension,
|
split_first_compression_format,
|
||||||
CompressionFormat::{self, *},
|
CompressionFormat::{self, *},
|
||||||
Extension,
|
Extension,
|
||||||
},
|
},
|
||||||
@ -70,7 +70,7 @@ pub fn compress_files(
|
|||||||
Ok(encoder)
|
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() {
|
for format in extensions.iter().rev() {
|
||||||
writer = chain_writer_encoder(format, writer)?;
|
writer = chain_writer_encoder(format, writer)?;
|
||||||
|
@ -9,7 +9,7 @@ use fs_err as fs;
|
|||||||
use crate::{
|
use crate::{
|
||||||
commands::warn_user_about_in_memory_zip_decompression,
|
commands::warn_user_about_in_memory_zip_decompression,
|
||||||
extension::{
|
extension::{
|
||||||
split_first_extension,
|
split_first_compression_format,
|
||||||
CompressionFormat::{self, *},
|
CompressionFormat::{self, *},
|
||||||
Extension,
|
Extension,
|
||||||
},
|
},
|
||||||
@ -95,7 +95,7 @@ pub fn decompress_file(
|
|||||||
Ok(decoder)
|
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() {
|
for format in extensions.iter().rev() {
|
||||||
reader = chain_reader_decoder(format, reader)?;
|
reader = chain_reader_decoder(format, reader)?;
|
||||||
|
@ -18,7 +18,7 @@ use utils::colors;
|
|||||||
use crate::{
|
use crate::{
|
||||||
commands::{compress::compress_files, decompress::decompress_file, list::list_archive_contents},
|
commands::{compress::compress_files, decompress::decompress_file, list::list_archive_contents},
|
||||||
error::FinalError,
|
error::FinalError,
|
||||||
extension::{self, Extension},
|
extension::{self, flatten_compression_formats, Extension},
|
||||||
info,
|
info,
|
||||||
list::ListOptions,
|
list::ListOptions,
|
||||||
utils::{
|
utils::{
|
||||||
@ -296,7 +296,7 @@ pub fn run(
|
|||||||
if i > 0 {
|
if i > 0 {
|
||||||
println!();
|
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)?;
|
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`
|
// Safety: we check that `compression_formats` is not empty in `Self::new`
|
||||||
self.compression_formats[0].is_archive_format()
|
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 {
|
impl fmt::Display for Extension {
|
||||||
@ -166,15 +161,19 @@ mod tests {
|
|||||||
let path = Path::new("bolovo.tar.gz");
|
let path = Path::new("bolovo.tar.gz");
|
||||||
|
|
||||||
let extensions: Vec<Extension> = extensions_from_path(path);
|
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
|
// Panics if formats has an empty list of compression formats
|
||||||
pub fn split_first_extension(formats: &[Extension]) -> (CompressionFormat, Vec<CompressionFormat>) {
|
pub fn split_first_compression_format(formats: &[Extension]) -> (CompressionFormat, Vec<CompressionFormat>) {
|
||||||
let mut extensions: Vec<CompressionFormat> = formats.iter().flat_map(Extension::iter).copied().collect();
|
let mut extensions: Vec<CompressionFormat> = flatten_compression_formats(&formats);
|
||||||
let first_extension = extensions.remove(0);
|
let first_extension = extensions.remove(0);
|
||||||
(first_extension, extensions)
|
(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