pub const BLOCK_SIZE: usize = 32 * 1024;
pub const HEADER_SIZE: usize = 7;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum RecordType {
Zero = 0,
Full = 1,
First = 2,
Middle = 3,
Last = 4,
}
impl RecordType {
pub fn from_u8(v: u8) -> Option<Self> {
match v {
0 => Some(Self::Zero),
1 => Some(Self::Full),
2 => Some(Self::First),
3 => Some(Self::Middle),
4 => Some(Self::Last),
_ => None,
}
}
}
pub fn encode_header(
buf: &mut [u8; HEADER_SIZE],
checksum: u32,
length: u16,
record_type: RecordType,
) {
buf[0..4].copy_from_slice(&checksum.to_le_bytes());
buf[4..6].copy_from_slice(&length.to_le_bytes());
buf[6] = record_type as u8;
}
pub fn decode_header(buf: &[u8; HEADER_SIZE]) -> (u32, u16, Option<RecordType>) {
let checksum = u32::from_le_bytes([buf[0], buf[1], buf[2], buf[3]]);
let length = u16::from_le_bytes([buf[4], buf[5]]);
let record_type = RecordType::from_u8(buf[6]);
(checksum, length, record_type)
}