use tessarikv_format::{FRAME_HEADER_BYTES, frame_bytes};
use crate::{Error, Result};
pub(crate) struct Frame {
pub(super) bytes: Vec<u8>,
pub(super) checksum: u32,
}
impl Frame {
pub(crate) fn with_checksum(bytes: Vec<u8>, checksum: u32) -> Result<Self> {
let payload_len =
bytes
.len()
.checked_sub(FRAME_HEADER_BYTES)
.ok_or(Error::Unsupported {
what: "a log frame without room for its header",
})?;
frame_bytes(payload_len).map_err(|_| Error::Unsupported {
what: "a log frame larger than the frame limit",
})?;
Ok(Self { bytes, checksum })
}
#[cfg(test)]
pub(crate) fn new(bytes: Vec<u8>) -> Result<Self> {
let checksum =
tessarikv_format::payload_checksum(bytes.get(FRAME_HEADER_BYTES..).unwrap_or_default());
Self::with_checksum(bytes, checksum)
}
}