Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub fn run(args: Opts, question_policy: QuestionPolicy) -> crate::Result<()> {
} else if inp_ext
.compression_formats
.iter()
.zip(&out_ext.compression_formats)
.zip(out_ext.compression_formats.iter())
.all(|(inp, out)| inp == out)
{
let new_ext = Extension::new(
Expand Down Expand Up @@ -348,7 +348,7 @@ fn decompress_file(
// in-memory decompression/copying first.
//
// 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)?;
let zip_archive = zip::ZipArchive::new(reader)?;
let _files = crate::archive::zip::unpack_archive(zip_archive, output_dir, question_policy)?;
Expand Down
36 changes: 19 additions & 17 deletions src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ use self::CompressionFormat::*;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Extension {
/// 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"
pub display_text: String,
}

impl Extension {
/// # Panics:
/// Will panic if `formats` is empty
pub fn new(formats: impl Into<Vec<CompressionFormat>>, text: impl Into<String>) -> Self {
let formats = formats.into();
pub fn new(formats: &'static [CompressionFormat], text: impl Into<String>) -> Self {
assert!(!formats.is_empty());
Self { compression_formats: formats, display_text: text.into() }
}
Expand Down Expand Up @@ -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 let Some(extension) = path.extension().and_then(OsStr::to_str) {
extensions.push(match extension {
"tar" => Extension::new([Tar], extension),
"tgz" => Extension::new([Tar, Gzip], extension),
"tbz" | "tbz2" => Extension::new([Tar, Bzip], extension),
"tlz4" => Extension::new([Tar, Lz4], extension),
"txz" | "tlz" | "tlzma" => Extension::new([Tar, Lzma], extension),
"tzst" => Extension::new([Tar, Zstd], ".tzst"),
"zip" => Extension::new([Zip], extension),
"bz" | "bz2" => Extension::new([Bzip], extension),
"gz" => Extension::new([Gzip], extension),
"lz4" => Extension::new([Lz4], extension),
"xz" | "lzma" | "lz" => Extension::new([Lzma], extension),
"zst" => Extension::new([Zstd], extension),
let formats: &[CompressionFormat] = match extension {
"tar" => &[Tar],
"tgz" => &[Tar, Gzip],
"tbz" | "tbz2" => &[Tar, Bzip],
"tlz4" => &[Tar, Lz4],
"txz" | "tlz" | "tlzma" => &[Tar, Lzma],
"tzst" => &[Tar, Zstd],
"zip" => &[Zip],
"bz" | "bz2" => &[Bzip],
"gz" => &[Gzip],
"lz4" => &[Lz4],
"xz" | "lzma" | "lz" => &[Lzma],
"zst" => &[Zstd],
_ => break,
});
};

let extension = Extension::new(formats, extension);
extensions.push(extension);

// Update for the next iteration
path = if let Some(stem) = path.file_stem() { Path::new(stem) } else { Path::new("") };
Expand Down