pub(super) const MOD_LOGFILE_BUF_SIZE: usize = 2048;
pub(super) const WRITE_LIMIT: usize = MOD_LOGFILE_BUF_SIZE - 1;
pub(super) const DECODE_DRIFT: usize = 2;
#[derive(Debug, Default)]
pub(super) struct WriteCursor(Option<usize>);
impl WriteCursor {
pub(super) fn begin(&mut self) {
self.0 = Some(0);
}
pub(super) fn lose(&mut self) {
self.0 = None;
}
fn budget_ends(spent: usize, line_len: usize) -> Option<usize> {
let remaining = WRITE_LIMIT.checked_sub(spent)?;
(remaining <= line_len).then_some(remaining)
}
pub(super) fn boundary_in(&self, line_len: usize) -> Option<usize> {
Self::budget_ends(self.0?, line_len)
}
pub(super) fn boundary_after(prepended: bool, at: usize, line_len: usize) -> Option<usize> {
if !prepended {
return None;
}
Some(at + Self::budget_ends(0, line_len - at)?)
}
pub(super) fn advance(&mut self, line_len: usize) {
self.0 = match self.0 {
Some(spent) if spent + line_len + 1 < WRITE_LIMIT => Some(spent + line_len + 1),
_ => None,
};
}
}