use std::fmt;
pub const MAGIC: [u8; 8] = [0xDC, 0xDF, 0x4A, 0x53, 0x4C, 0x42, 0x01, 0x00];
pub const VERSION: u32 = 1;
pub const SLAB_REF_KEY: &str = "$s";
pub const FIXED_HEADER_SIZE: usize = 20;
pub const SLAB_TABLE_ENTRY_SIZE: usize = 12;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
pub enum SlabType {
Int8 = 0x00,
Uint8 = 0x01,
Int16 = 0x02,
Uint16 = 0x03,
Int32 = 0x04,
Uint32 = 0x05,
Float32 = 0x06,
Float64 = 0x07,
Int64 = 0x08,
Uint64 = 0x09,
Json = 0x0a,
}
impl SlabType {
pub fn element_size(self) -> usize {
match self {
Self::Int16 | Self::Uint16 => 2,
Self::Int32 | Self::Uint32 | Self::Float32 => 4,
Self::Float64 | Self::Int64 | Self::Uint64 => 8,
_ => 1,
}
}
pub fn name(self) -> &'static str {
match self {
Self::Int8 => "i8",
Self::Uint8 => "u8",
Self::Int16 => "i16",
Self::Uint16 => "u16",
Self::Int32 => "i32",
Self::Uint32 => "u32",
Self::Float32 => "f32",
Self::Float64 => "f64",
Self::Int64 => "i64",
Self::Uint64 => "u64",
Self::Json => "json",
}
}
}
impl fmt::Display for SlabType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
impl TryFrom<u32> for SlabType {
type Error = u32;
fn try_from(v: u32) -> Result<Self, Self::Error> {
Ok(match v {
0x00 => Self::Int8,
0x01 => Self::Uint8,
0x02 => Self::Int16,
0x03 => Self::Uint16,
0x04 => Self::Int32,
0x05 => Self::Uint32,
0x06 => Self::Float32,
0x07 => Self::Float64,
0x08 => Self::Int64,
0x09 => Self::Uint64,
0x0a => Self::Json,
_ => return Err(v),
})
}
}
#[derive(Debug, Clone, Copy)]
pub struct SlabBytes<'a> {
pub slab_type: SlabType,
pub bytes: &'a [u8],
}
impl<'a> SlabBytes<'a> {
pub fn element_count(&self) -> usize {
self.bytes.len() / self.slab_type.element_size()
}
}
pub trait SlabPrimitive: Copy + sealed::Sealed {
const SLAB_TYPE: SlabType;
type LeBytes: AsRef<[u8]>;
fn to_le_bytes(self) -> Self::LeBytes;
fn from_le_bytes(bytes: &[u8]) -> Self;
}
mod sealed {
pub unsafe trait Sealed {}
}
unsafe impl sealed::Sealed for u8 {}
unsafe impl sealed::Sealed for i8 {}
unsafe impl sealed::Sealed for u16 {}
unsafe impl sealed::Sealed for i16 {}
unsafe impl sealed::Sealed for u32 {}
unsafe impl sealed::Sealed for i32 {}
unsafe impl sealed::Sealed for u64 {}
unsafe impl sealed::Sealed for i64 {}
unsafe impl sealed::Sealed for f32 {}
unsafe impl sealed::Sealed for f64 {}
impl SlabPrimitive for u8 {
const SLAB_TYPE: SlabType = SlabType::Uint8;
type LeBytes = [u8; 1];
fn to_le_bytes(self) -> [u8; 1] {
[self]
}
fn from_le_bytes(bytes: &[u8]) -> Self {
bytes[0]
}
}
impl SlabPrimitive for i8 {
const SLAB_TYPE: SlabType = SlabType::Int8;
type LeBytes = [u8; 1];
fn to_le_bytes(self) -> [u8; 1] {
[self as u8]
}
fn from_le_bytes(bytes: &[u8]) -> Self {
bytes[0] as i8
}
}
macro_rules! impl_slab_primitive {
($t:ty, $tag:ident, $n:literal) => {
impl SlabPrimitive for $t {
const SLAB_TYPE: SlabType = SlabType::$tag;
type LeBytes = [u8; $n];
fn to_le_bytes(self) -> [u8; $n] {
<$t>::to_le_bytes(self)
}
fn from_le_bytes(bytes: &[u8]) -> Self {
<$t>::from_le_bytes(bytes.try_into().unwrap())
}
}
};
}
impl_slab_primitive!(u16, Uint16, 2);
impl_slab_primitive!(i16, Int16, 2);
impl_slab_primitive!(u32, Uint32, 4);
impl_slab_primitive!(i32, Int32, 4);
impl_slab_primitive!(u64, Uint64, 8);
impl_slab_primitive!(i64, Int64, 8);
impl_slab_primitive!(f32, Float32, 4);
impl_slab_primitive!(f64, Float64, 8);
#[derive(Debug, Clone, Copy)]
pub struct Header {
pub slab_count: usize,
pub root_json_slab_index: usize,
}
#[derive(Debug, Clone, Copy)]
pub struct SlabTableEntry {
pub slab_type: SlabType,
pub start_offset: u64,
pub byte_length: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SlabPlaceholder(pub usize);
impl SlabPlaceholder {
pub fn index(self) -> usize {
self.0
}
}
impl fmt::Display for SlabPlaceholder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if f.alternate() {
write!(f, r#"{{"{SLAB_REF_KEY}":{}}}"#, self.0)
} else {
self.0.fmt(f)
}
}
}