use crate::CompressionFormat;
use std::fmt;
impl fmt::Display for CompressionFormat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Lz4 => write!(f, "LZ4"),
Self::Snappy => write!(f, "Snappy"),
Self::Gzip => write!(f, "Gzip"),
Self::Zstd => write!(f, "Zstd"),
}
}
}
impl CompressionFormat {
pub(crate) fn feature_name(self) -> &'static str {
match self {
Self::Lz4 => "lz4",
Self::Snappy => "snappy",
Self::Gzip => "gzip",
Self::Zstd => "zstd",
}
}
#[must_use]
pub fn detect(data: &[u8]) -> Option<Self> {
if data.starts_with(&[0x1f, 0x8b]) {
return Some(Self::Gzip);
}
if data.starts_with(&[0x04, 0x22, 0x4d, 0x18])
|| data.starts_with(&[0x02, 0x21, 0x4c, 0x18])
{
return Some(Self::Lz4);
}
if data.starts_with(&[0x28, 0xb5, 0x2f, 0xfd]) {
return Some(Self::Zstd);
}
if data.starts_with(&[0xff, 0x06, 0x00, 0x00, 0x73, 0x4e, 0x61, 0x50, 0x70, 0x59]) {
return Some(Self::Snappy);
}
None
}
}