use std::io;
use bincode::{config::standard, decode_from_slice, Decode, Encode};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)]
pub struct PersistedCounters {
pub revision: u64,
pub next_block: u64,
pub next_snapshot: u64,
pub next_branch: u64,
pub next_session: u32,
}
impl PersistedCounters {
pub fn new(
revision: u64,
next_block: u64,
next_snapshot: u64,
next_branch: u64,
next_session: u32,
) -> Self {
Self {
revision,
next_block,
next_snapshot,
next_branch,
next_session,
}
}
}
pub fn decode_counters(bytes: &[u8]) -> io::Result<PersistedCounters> {
if let Ok((c, _)) = decode_from_slice::<PersistedCounters, _>(bytes, standard()) {
return Ok(c);
}
if let Ok((c, _)) = decode_from_slice::<[u64; 4], _>(bytes, standard()) {
return Ok(PersistedCounters::new(c[0], c[1], c[2], c[3], 1));
}
let (c, _): ([u64; 3], _) = decode_from_slice(bytes, standard())
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
Ok(PersistedCounters::new(c[0], c[1], c[2], 2, 1))
}