| 1 |
use core::fmt; |
| 2 |
#[cfg(feature = "std")] |
| 3 |
use std::error::Error; |
| 4 |
#[cfg(feature = "std")] |
| 5 |
use std::io; |
| 6 |
|
| 7 |
#[non_exhaustive] |
| 8 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 9 |
pub enum CompressionMethod { |
| 10 |
/// Special "meta" method marking a directory. Also used for symlinks. |
| 11 |
Lhd, |
| 12 |
Lzs, |
| 13 |
Lz4, |
| 14 |
Lz5, |
| 15 |
Lh0, |
| 16 |
Lh1, |
| 17 |
Lh4, |
| 18 |
Lh5, |
| 19 |
Lh6, |
| 20 |
Lh7, |
| 21 |
Lhx, |
| 22 |
Pm0, |
| 23 |
Pm1, |
| 24 |
Pm2, |
| 25 |
} |
| 26 |
|
| 27 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 28 |
pub struct UnrecognizedCompressionMethod([u8;5]); |
| 29 |
|
| 30 |
impl TryFrom<&[u8;5]> for CompressionMethod { |
| 31 |
type Error = UnrecognizedCompressionMethod; |
| 32 |
fn try_from(s: &[u8;5]) -> Result<Self, Self::Error> { |
| 33 |
Ok(match s { |
| 34 |
b"-lhd-" => CompressionMethod::Lhd, |
| 35 |
b"-lzs-" => CompressionMethod::Lzs, |
| 36 |
b"-lz4-" => CompressionMethod::Lz4, |
| 37 |
b"-lz5-" => CompressionMethod::Lz5, |
| 38 |
b"-lh0-" => CompressionMethod::Lh0, |
| 39 |
b"-lh1-" => CompressionMethod::Lh1, |
| 40 |
b"-lh4-" => CompressionMethod::Lh4, |
| 41 |
b"-lh5-" => CompressionMethod::Lh5, |
| 42 |
b"-lh6-" => CompressionMethod::Lh6, |
| 43 |
b"-lh7-" => CompressionMethod::Lh7, |
| 44 |
b"-lhx-" => CompressionMethod::Lhx, |
| 45 |
b"-pm0-" => CompressionMethod::Pm0, |
| 46 |
b"-pm1-" => CompressionMethod::Pm1, |
| 47 |
b"-pm2-" => CompressionMethod::Pm2, |
| 48 |
_ => return Err(UnrecognizedCompressionMethod(*s)) |
| 49 |
}) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
impl CompressionMethod { |
| 54 |
pub fn is_directory(&self) -> bool { |
| 55 |
if let CompressionMethod::Lhd = self { |
| 56 |
return true |
| 57 |
} |
| 58 |
false |
| 59 |
} |
| 60 |
|
| 61 |
pub fn as_identifier(self) -> &'static [u8;5] { |
| 62 |
match self { |
| 63 |
CompressionMethod::Lhd => b"-lhd-", |
| 64 |
CompressionMethod::Lzs => b"-lzs-", |
| 65 |
CompressionMethod::Lz4 => b"-lz4-", |
| 66 |
CompressionMethod::Lz5 => b"-lz5-", |
| 67 |
CompressionMethod::Lh0 => b"-lh0-", |
| 68 |
CompressionMethod::Lh1 => b"-lh1-", |
| 69 |
CompressionMethod::Lh4 => b"-lh4-", |
| 70 |
CompressionMethod::Lh5 => b"-lh5-", |
| 71 |
CompressionMethod::Lh6 => b"-lh6-", |
| 72 |
CompressionMethod::Lh7 => b"-lh7-", |
| 73 |
CompressionMethod::Lhx => b"-lhx-", |
| 74 |
CompressionMethod::Pm0 => b"-pm0-", |
| 75 |
CompressionMethod::Pm1 => b"-pm1-", |
| 76 |
CompressionMethod::Pm2 => b"-pm2-", |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
#[cfg(feature = "std")] |
| 82 |
impl Error for UnrecognizedCompressionMethod {} |
| 83 |
|
| 84 |
impl fmt::Display for UnrecognizedCompressionMethod { |
| 85 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 86 |
write!(f, "Unrecognized compression method: {:x?}", self.0) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
impl fmt::Display for CompressionMethod { |
| 91 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 92 |
let strid = self.as_identifier(); |
| 93 |
assert!(strid.is_ascii()); |
| 94 |
unsafe { |
| 95 |
core::str::from_utf8_unchecked(strid) |
| 96 |
}.fmt(f) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
#[cfg(feature = "std")] |
| 101 |
impl From<UnrecognizedCompressionMethod> for io::Error { |
| 102 |
fn from(e: UnrecognizedCompressionMethod) -> Self { |
| 103 |
io::Error::new(io::ErrorKind::InvalidData, e) |
| 104 |
} |
| 105 |
} |