use std::convert::TryFrom;
use crate::{error::Error, format_code::EncodingCodes};
pub const OFFSET_LIST8: usize = 1;
pub const OFFSET_LIST32: usize = 4;
pub const OFFSET_MAP8: usize = 1;
pub const OFFSET_MAP32: usize = 4;
pub const OFFSET_ARRAY8: usize = 2;
pub const OFFSET_ARRAY32: usize = 5;
pub enum Category {
Fixed(FixedWidth),
Encoded(EncodedWidth),
}
#[repr(u8)]
pub enum FixedWidth {
Zero = 0,
One = 1,
Two = 2,
Four = 4,
Eight = 8,
Sixteen = 16,
}
#[repr(u8)]
pub enum EncodedWidth {
Zero = 0,
One = 1,
Four = 4,
}
impl TryFrom<EncodingCodes> for Category {
type Error = Error;
fn try_from(value: EncodingCodes) -> Result<Self, Self::Error> {
let value = match value {
EncodingCodes::DescribedType => return Err(Error::IsDescribedType),
EncodingCodes::Null => Category::Fixed(FixedWidth::Zero),
EncodingCodes::Boolean => Category::Fixed(FixedWidth::One),
EncodingCodes::BooleanTrue => Category::Fixed(FixedWidth::Zero),
EncodingCodes::BooleanFalse => Category::Fixed(FixedWidth::Zero),
EncodingCodes::UByte => Category::Fixed(FixedWidth::One),
EncodingCodes::UShort => Category::Fixed(FixedWidth::Two),
EncodingCodes::UInt => Category::Fixed(FixedWidth::Four),
EncodingCodes::SmallUInt => Category::Fixed(FixedWidth::One),
EncodingCodes::UInt0 => Category::Fixed(FixedWidth::Zero),
EncodingCodes::ULong => Category::Fixed(FixedWidth::Eight),
EncodingCodes::SmallULong => Category::Fixed(FixedWidth::One),
EncodingCodes::ULong0 => Category::Fixed(FixedWidth::Zero),
EncodingCodes::Byte => Category::Fixed(FixedWidth::One),
EncodingCodes::Short => Category::Fixed(FixedWidth::Two),
EncodingCodes::Int => Category::Fixed(FixedWidth::Four),
EncodingCodes::SmallInt => Category::Fixed(FixedWidth::One),
EncodingCodes::Long => Category::Fixed(FixedWidth::Eight),
EncodingCodes::SmallLong => Category::Fixed(FixedWidth::One),
EncodingCodes::Float => Category::Fixed(FixedWidth::Four),
EncodingCodes::Double => Category::Fixed(FixedWidth::Eight),
EncodingCodes::Decimal32 => Category::Fixed(FixedWidth::Four),
EncodingCodes::Decimal64 => Category::Fixed(FixedWidth::Eight),
EncodingCodes::Decimal128 => Category::Fixed(FixedWidth::Sixteen),
EncodingCodes::Char => Category::Fixed(FixedWidth::Four),
EncodingCodes::Timestamp => Category::Fixed(FixedWidth::Eight),
EncodingCodes::Uuid => Category::Fixed(FixedWidth::Sixteen),
EncodingCodes::VBin8 => Category::Encoded(EncodedWidth::One),
EncodingCodes::VBin32 => Category::Encoded(EncodedWidth::Four),
EncodingCodes::Str8 => Category::Encoded(EncodedWidth::One),
EncodingCodes::Str32 => Category::Encoded(EncodedWidth::Four),
EncodingCodes::Sym8 => Category::Encoded(EncodedWidth::One),
EncodingCodes::Sym32 => Category::Encoded(EncodedWidth::Four),
EncodingCodes::List0 => Category::Encoded(EncodedWidth::Zero),
EncodingCodes::List8 => Category::Encoded(EncodedWidth::One),
EncodingCodes::List32 => Category::Encoded(EncodedWidth::Four),
EncodingCodes::Map8 => Category::Encoded(EncodedWidth::One),
EncodingCodes::Map32 => Category::Encoded(EncodedWidth::Four),
EncodingCodes::Array8 => Category::Encoded(EncodedWidth::One),
EncodingCodes::Array32 => Category::Encoded(EncodedWidth::Four),
};
Ok(value)
}
}