use crate::error::DryIceError;
pub trait RecordKey: Ord + Sized {
const WIDTH: u16;
const TYPE_TAG: [u8; 16];
fn encode_into(&self, out: &mut [u8]);
fn decode_from(bytes: &[u8]) -> Result<Self, DryIceError>;
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct NoRecordKey;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Bytes8Key(pub [u8; 8]);
impl From<[u8; 8]> for Bytes8Key {
fn from(value: [u8; 8]) -> Self {
Self(value)
}
}
impl RecordKey for Bytes8Key {
const WIDTH: u16 = 8;
const TYPE_TAG: [u8; 16] = *b"dryi:bytes8:key!";
fn encode_into(&self, out: &mut [u8]) {
debug_assert_eq!(out.len(), usize::from(Self::WIDTH));
out.copy_from_slice(&self.0);
}
fn decode_from(bytes: &[u8]) -> Result<Self, DryIceError> {
let arr: [u8; 8] = bytes
.try_into()
.map_err(|_| DryIceError::InvalidRecordKeyEncoding {
message: "invalid bytes8 key length",
})?;
Ok(Self(arr))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Bytes16Key(pub [u8; 16]);
impl From<[u8; 16]> for Bytes16Key {
fn from(value: [u8; 16]) -> Self {
Self(value)
}
}
impl RecordKey for Bytes16Key {
const WIDTH: u16 = 16;
const TYPE_TAG: [u8; 16] = *b"dryi:bytes16:key";
fn encode_into(&self, out: &mut [u8]) {
debug_assert_eq!(out.len(), usize::from(Self::WIDTH));
out.copy_from_slice(&self.0);
}
fn decode_from(bytes: &[u8]) -> Result<Self, DryIceError> {
let arr: [u8; 16] =
bytes
.try_into()
.map_err(|_| DryIceError::InvalidRecordKeyEncoding {
message: "invalid bytes16 key length",
})?;
Ok(Self(arr))
}
}