extension: Use hardcoded slices instead of Vecs when creating an Extension

This commit is contained in:
Vinícius Rodrigues Miguel 2021-11-03 12:16:20 -03:00
parent a1c4f0373f
commit a798d20106
2 changed files with 21 additions and 19 deletions

View File

@ -128,7 +128,7 @@ pub fn run(args: Opts, question_policy: QuestionPolicy) -> crate::Result<()> {
} else if inp_ext } else if inp_ext
.compression_formats .compression_formats
.iter() .iter()
.zip(&out_ext.compression_formats) .zip(out_ext.compression_formats.iter())
.all(|(inp, out)| inp == out) .all(|(inp, out)| inp == out)
{ {
let new_ext = Extension::new( let new_ext = Extension::new(
@ -348,7 +348,7 @@ fn decompress_file(
// in-memory decompression/copying first. // in-memory decompression/copying first.
// //
// Any other Zip decompression done can take up the whole RAM and freeze ouch. // Any other Zip decompression done can take up the whole RAM and freeze ouch.
if formats.len() == 1 && *formats[0].compression_formats.as_slice() == [Zip] { if formats.len() == 1 && *formats[0].compression_formats == [Zip] {
utils::create_dir_if_non_existent(output_dir)?; utils::create_dir_if_non_existent(output_dir)?;
let zip_archive = zip::ZipArchive::new(reader)?; let zip_archive = zip::ZipArchive::new(reader)?;
let _files = crate::archive::zip::unpack_archive(zip_archive, output_dir, question_policy)?; let _files = crate::archive::zip::unpack_archive(zip_archive, output_dir, question_policy)?;

View File

@ -8,7 +8,7 @@ use self::CompressionFormat::*;
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct Extension { pub struct Extension {
/// One extension like "tgz" can be made of multiple CompressionFormats ([Tar, Gz]) /// One extension like "tgz" can be made of multiple CompressionFormats ([Tar, Gz])
pub compression_formats: Vec<CompressionFormat>, pub compression_formats: &'static [CompressionFormat],
/// The input text for this extension, like "tgz", "tar" or "xz" /// The input text for this extension, like "tgz", "tar" or "xz"
pub display_text: String, pub display_text: String,
} }
@ -16,8 +16,7 @@ pub struct Extension {
impl Extension { impl Extension {
/// # Panics: /// # Panics:
/// Will panic if `formats` is empty /// Will panic if `formats` is empty
pub fn new(formats: impl Into<Vec<CompressionFormat>>, text: impl Into<String>) -> Self { pub fn new(formats: &'static [CompressionFormat], text: impl Into<String>) -> Self {
let formats = formats.into();
assert!(!formats.is_empty()); assert!(!formats.is_empty());
Self { compression_formats: formats, display_text: text.into() } Self { compression_formats: formats, display_text: text.into() }
} }
@ -110,21 +109,24 @@ pub fn separate_known_extensions_from_name(mut path: &Path) -> (&Path, Vec<Exten
// While there is known extensions at the tail, grab them // While there is known extensions at the tail, grab them
while let Some(extension) = path.extension().and_then(OsStr::to_str) { while let Some(extension) = path.extension().and_then(OsStr::to_str) {
extensions.push(match extension { let formats: &[CompressionFormat] = match extension {
"tar" => Extension::new([Tar], extension), "tar" => &[Tar],
"tgz" => Extension::new([Tar, Gzip], extension), "tgz" => &[Tar, Gzip],
"tbz" | "tbz2" => Extension::new([Tar, Bzip], extension), "tbz" | "tbz2" => &[Tar, Bzip],
"tlz4" => Extension::new([Tar, Lz4], extension), "tlz4" => &[Tar, Lz4],
"txz" | "tlz" | "tlzma" => Extension::new([Tar, Lzma], extension), "txz" | "tlz" | "tlzma" => &[Tar, Lzma],
"tzst" => Extension::new([Tar, Zstd], ".tzst"), "tzst" => &[Tar, Zstd],
"zip" => Extension::new([Zip], extension), "zip" => &[Zip],
"bz" | "bz2" => Extension::new([Bzip], extension), "bz" | "bz2" => &[Bzip],
"gz" => Extension::new([Gzip], extension), "gz" => &[Gzip],
"lz4" => Extension::new([Lz4], extension), "lz4" => &[Lz4],
"xz" | "lzma" | "lz" => Extension::new([Lzma], extension), "xz" | "lzma" | "lz" => &[Lzma],
"zst" => Extension::new([Zstd], extension), "zst" => &[Zstd],
_ => break, _ => break,
}); };
let extension = Extension::new(formats, extension);
extensions.push(extension);
// Update for the next iteration // Update for the next iteration
path = if let Some(stem) = path.file_stem() { Path::new(stem) } else { Path::new("") }; path = if let Some(stem) = path.file_stem() { Path::new(stem) } else { Path::new("") };