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

Remove Lzip because its incorrect, and improve extension comparison #198

Merged
merged 3 commits into from
Nov 14, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |

Expand Down
12 changes: 9 additions & 3 deletions src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ 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])
pub compression_formats: &'static [CompressionFormat],
/// 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:
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -121,7 +127,7 @@ pub fn separate_known_extensions_from_name(mut path: &Path) -> (&Path, Vec<Exten
"bz" | "bz2" => &[Bzip],
"gz" => &[Gzip],
"lz4" => &[Lz4],
"xz" | "lzma" | "lz" => &[Lzma],
"xz" | "lzma" => &[Lzma],
"zst" => &[Zstd],
_ => break,
};
Expand Down
5 changes: 0 additions & 5 deletions src/utils/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ pub fn try_infer_extension(path: &Path) -> Option<Extension> {
fn is_xz(buf: &[u8]) -> bool {
buf.starts_with(&[0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00])
}
fn is_lz(buf: &[u8]) -> bool {
buf.starts_with(&[0x4C, 0x5A, 0x49, 0x50])
}
fn is_lz4(buf: &[u8]) -> bool {
buf.starts_with(&[0x04, 0x22, 0x4D, 0x18])
}
Expand Down Expand Up @@ -112,8 +109,6 @@ pub fn try_infer_extension(path: &Path) -> Option<Extension> {
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_lz4(&buf) {
Some(Extension::new(&[Lz4], "lz4"))
} else if is_zst(&buf) {
Expand Down
1 change: 0 additions & 1 deletion tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ enum FileExtension {
Bz2,
Gz,
Lz4,
Lz,
Lzma,
Xz,
}
Expand Down