use crate::{Bytes, Error, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Format {
Bytes,
ZeroCopy,
Pco = 64,
LZ4 = 65,
Zstd = 66,
}
impl Format {
#[inline]
pub fn is_raw(&self) -> bool {
matches!(self, Self::ZeroCopy | Self::Bytes)
}
#[inline]
pub fn is_compressed(&self) -> bool {
matches!(self, Self::Pco | Self::LZ4 | Self::Zstd)
}
#[inline]
pub fn is_zerocopy(&self) -> bool {
*self == Self::ZeroCopy
}
#[inline]
pub fn is_bytes(&self) -> bool {
*self == Self::Bytes
}
#[inline]
pub fn is_pcodec(&self) -> bool {
*self == Self::Pco
}
#[inline]
pub fn is_lz4(&self) -> bool {
*self == Self::LZ4
}
#[inline]
pub fn is_zstd(&self) -> bool {
*self == Self::Zstd
}
}
impl Bytes for Format {
type Array = [u8; size_of::<Self>()];
#[inline]
fn to_bytes(&self) -> Self::Array {
[*self as u8]
}
#[inline]
fn from_bytes(bytes: &[u8]) -> Result<Self> {
let len = bytes.len();
if len != size_of::<Self>() {
return Err(Error::WrongLength {
expected: size_of::<Self>(),
received: len,
});
}
match bytes[0] {
0 => Ok(Self::Bytes),
1 => Ok(Self::ZeroCopy),
64 => Ok(Self::Pco),
65 => Ok(Self::LZ4),
66 => Ok(Self::Zstd),
b => Err(Error::InvalidFormat(b)),
}
}
}