From 0660c2fe5918834bbc09d887987bbd9491c53446 Mon Sep 17 00:00:00 2001 From: Nbiba Bedis Date: Sat, 13 Nov 2021 09:28:19 +0100 Subject: [PATCH] Remove Lzip because its incorrect, and improve extention comparison --- README.md | 2 +- src/extension.rs | 12 +++++++++--- src/utils/fs.rs | 11 +++-------- tests/integration.rs | 1 - 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index bad6981..0d13606 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,7 @@ ouch c file.txt file.zip # Supported formats -| Format | `.tar` | `.zip` | `.bz`, `.bz2` | `.gz` | `.lz4` | `.xz`, `.lz`, `.lzma` | `.zst` | +| Format | `.tar` | `.zip` | `.bz`, `.bz2` | `.gz` | `.lz4` | `.xz`, `.lzma` | `.zst` | |:---------:|:------:|:------:|:-------------:|:-----:|:------:|:---------------------:|:------:| | Supported | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | diff --git a/src/extension.rs b/src/extension.rs index 5e67407..d027876 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -5,7 +5,7 @@ use std::{ffi::OsStr, fmt, path::Path}; use self::CompressionFormat::*; /// A wrapper around `CompressionFormat` that allows combinations like `tgz` -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, Eq)] #[non_exhaustive] pub struct Extension { /// One extension like "tgz" can be made of multiple CompressionFormats ([Tar, Gz]) @@ -13,6 +13,12 @@ pub struct Extension { /// The input text for this extension, like "tgz", "tar" or "xz" pub display_text: String, } +// The display_text should be ignored when comparing extensions +impl PartialEq for Extension { + fn eq(&self, other: &Self) -> bool { + self.compression_formats == other.compression_formats + } +} impl Extension { /// # Panics: @@ -49,7 +55,7 @@ pub enum CompressionFormat { Bzip, /// .lz4 Lz4, - /// .xz .lzma .lz + /// .xz .lzma Lzma, /// tar, tgz, tbz, tbz2, txz, tlz, tlz4, tlzma, tzst Tar, @@ -121,7 +127,7 @@ pub fn separate_known_extensions_from_name(mut path: &Path) -> (&Path, Vec &[Bzip], "gz" => &[Gzip], "lz4" => &[Lz4], - "xz" | "lzma" | "lz" => &[Lzma], + "xz" | "lzma" => &[Lzma], "zst" => &[Zstd], _ => break, }; diff --git a/src/utils/fs.rs b/src/utils/fs.rs index f1d14ab..f822332 100644 --- a/src/utils/fs.rs +++ b/src/utils/fs.rs @@ -75,12 +75,9 @@ pub fn try_infer_extension(path: &Path) -> Option { fn is_bz2(buf: &[u8]) -> bool { buf.len() > 2 && buf[..=2] == [0x42, 0x5A, 0x68] } - fn is_xz(buf: &[u8]) -> bool { + fn is_lzma(buf: &[u8]) -> bool { buf.len() > 5 && buf[..=5] == [0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00] } - fn is_lz(buf: &[u8]) -> bool { - buf.len() > 3 && buf[..=3] == [0x4C, 0x5A, 0x49, 0x50] - } fn is_lz4(buf: &[u8]) -> bool { buf.len() > 3 && buf[..=3] == [0x04, 0x22, 0x4D, 0x18] } @@ -110,10 +107,8 @@ pub fn try_infer_extension(path: &Path) -> Option { Some(Extension::new(&[Gzip], "gz")) } else if is_bz2(&buf) { Some(Extension::new(&[Bzip], "bz2")) - } else if is_xz(&buf) { - Some(Extension::new(&[Lzma], "xz")) - } else if is_lz(&buf) { - Some(Extension::new(&[Lzma], "lz")) + } else if is_lzma(&buf) { + Some(Extension::new(&[Lzma], "lzma")) // lzma can be `xz` or `lzma` } else if is_lz4(&buf) { Some(Extension::new(&[Lz4], "lz4")) } else if is_zst(&buf) { diff --git a/tests/integration.rs b/tests/integration.rs index 8ee7f93..49c2a27 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -34,7 +34,6 @@ enum FileExtension { Bz2, Gz, Lz4, - Lz, Lzma, Xz, }