#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
#[non_exhaustive]
pub enum CompressionType {
None = 0,
Gzip = 1,
Snappy = 2,
Lz4 = 3,
Zstd = 4,
}
impl CompressionType {
#[must_use]
pub fn from_attribute_bits(b: u8) -> Option<Self> {
match b & 0b0000_0111 {
0 => Some(Self::None),
1 => Some(Self::Gzip),
2 => Some(Self::Snappy),
3 => Some(Self::Lz4),
4 => Some(Self::Zstd),
_ => None,
}
}
#[must_use]
pub fn as_attribute_bits(self) -> u8 {
self as u8
}
}
#[cfg(test)]
mod tests {
use super::*;
use assert2::assert;
#[test]
fn attribute_bits_roundtrip() {
for ct in [
CompressionType::None,
CompressionType::Gzip,
CompressionType::Snappy,
CompressionType::Lz4,
CompressionType::Zstd,
] {
assert!(CompressionType::from_attribute_bits(ct.as_attribute_bits()) == Some(ct));
}
}
#[test]
fn attribute_bits_mask() {
assert!(
CompressionType::from_attribute_bits(0b1111_1000 | 0b0000_0001)
== Some(CompressionType::Gzip)
);
}
#[test]
fn attribute_bits_unknown() {
assert!(CompressionType::from_attribute_bits(5) == None);
assert!(CompressionType::from_attribute_bits(7) == None);
}
}