#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum WireType {
Null = 0x00,
Bool = 0x01,
VarUInt = 0x02,
VarInt = 0x03,
Fixed64 = 0x04,
LenDelimited = 0x05,
StartObject = 0x06,
EndObject = 0x07,
StartArray = 0x08,
EndArray = 0x09,
Reference = 0x0A,
}
impl WireType {
pub fn from_tag(tag: u8) -> Option<WireType> {
match tag & 0x0F {
0x00 => Some(WireType::Null),
0x01 => Some(WireType::Bool),
0x02 => Some(WireType::VarUInt),
0x03 => Some(WireType::VarInt),
0x04 => Some(WireType::Fixed64),
0x05 => Some(WireType::LenDelimited),
0x06 => Some(WireType::StartObject),
0x07 => Some(WireType::EndObject),
0x08 => Some(WireType::StartArray),
0x09 => Some(WireType::EndArray),
0x0A => Some(WireType::Reference),
_ => None,
}
}
pub fn to_tag(self) -> u8 {
self as u8
}
pub fn to_tag_with_flags(self, flags: u8) -> u8 {
(flags << 4) | (self as u8)
}
}
pub mod flags {
pub const NONE: u8 = 0x00;
pub const STRING_DICT_REF: u8 = 0x01;
pub const HAS_SCHEMA_ANNOTATION: u8 = 0x02;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum CompressionType {
None = 0x00,
Zstd = 0x01,
Snappy = 0x02,
Lz4 = 0x03,
}
impl CompressionType {
pub fn from_byte(b: u8) -> Option<Self> {
match b {
0x00 => Some(Self::None),
0x01 => Some(Self::Zstd),
0x02 => Some(Self::Snappy),
0x03 => Some(Self::Lz4),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum BlockType {
Data = 0x01,
Index = 0x02,
Schema = 0x03,
StringDict = 0x04,
Trailer = 0xFF,
}
impl BlockType {
pub fn from_byte(b: u8) -> Option<Self> {
match b {
0x01 => Some(Self::Data),
0x02 => Some(Self::Index),
0x03 => Some(Self::Schema),
0x04 => Some(Self::StringDict),
0xFF => Some(Self::Trailer),
_ => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn wire_type_roundtrip() {
for tag in 0x00..=0x0Au8 {
let wt = WireType::from_tag(tag).unwrap();
assert_eq!(wt.to_tag(), tag);
}
}
#[test]
fn unknown_wire_type() {
assert!(WireType::from_tag(0x0B).is_none());
assert!(WireType::from_tag(0x0F).is_none());
}
#[test]
fn tag_with_flags() {
let tag = WireType::VarUInt.to_tag_with_flags(flags::STRING_DICT_REF);
assert_eq!(tag, 0x12); assert_eq!(WireType::from_tag(tag), Some(WireType::VarUInt));
assert_eq!(tag >> 4, flags::STRING_DICT_REF);
}
}