use byteorder::{BigEndian, ReadBytesExt};
use bytes::{BufMut, BytesMut};
use std::io::{self, Cursor};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum DupCountLnEntryError {
#[error("I/O error: {0}")]
Io(#[from] io::Error),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DupCountLnEntry {
pub dup_count: i32,
}
impl DupCountLnEntry {
pub fn new(dup_count: i32) -> Self {
Self { dup_count }
}
pub fn log_size(&self) -> usize {
4 }
pub fn write_to_log(&self, buf: &mut BytesMut) {
buf.put_i32(self.dup_count);
}
pub fn read_from_log(buf: &[u8]) -> Result<Self, DupCountLnEntryError> {
let mut cursor = Cursor::new(buf);
let dup_count = cursor.read_i32::<BigEndian>()?;
Ok(Self { dup_count })
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_dup_count_ln_roundtrip() {
let entry = DupCountLnEntry::new(42);
let mut buf = BytesMut::new();
entry.write_to_log(&mut buf);
let decoded = DupCountLnEntry::read_from_log(&buf).unwrap();
assert_eq!(entry, decoded);
assert_eq!(decoded.dup_count, 42);
}
#[test]
fn test_dup_count_ln_zero() {
let entry = DupCountLnEntry::new(0);
let mut buf = BytesMut::new();
entry.write_to_log(&mut buf);
let decoded = DupCountLnEntry::read_from_log(&buf).unwrap();
assert_eq!(entry, decoded);
assert_eq!(decoded.dup_count, 0);
}
#[test]
fn test_dup_count_ln_negative() {
let entry = DupCountLnEntry::new(-1);
let mut buf = BytesMut::new();
entry.write_to_log(&mut buf);
let decoded = DupCountLnEntry::read_from_log(&buf).unwrap();
assert_eq!(entry, decoded);
assert_eq!(decoded.dup_count, -1);
}
#[test]
fn test_log_size() {
let entry = DupCountLnEntry::new(100);
assert_eq!(entry.log_size(), 4);
let mut buf = BytesMut::new();
entry.write_to_log(&mut buf);
assert_eq!(buf.len(), entry.log_size());
}
}