use crate::{formatter::Formatter, Result, SingleEventCounter};
#[derive(Debug, Clone, Copy)]
pub struct BincodeFormat;
impl Formatter for BincodeFormat {
fn serialize(&self, value: &SingleEventCounter) -> Result<Vec<u8>> {
bincode::serialize(value).map_err(|e| crate::Error::Serialization(e.to_string()))
}
fn deserialize(&self, bytes: &[u8]) -> Result<SingleEventCounter> {
bincode::deserialize(bytes).map_err(|e| crate::Error::Serialization(e.to_string()))
}
fn extension(&self) -> &'static str {
".bin"
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{EventCounterConfig, SingleEventCounter};
use chrono::Utc;
#[test]
fn test_bincode_roundtrip() {
let formatter = BincodeFormat;
let now = Utc::now();
let config = EventCounterConfig::default();
let counter = config.create_counter(now);
let bytes = formatter.serialize(&counter).unwrap();
let decoded: SingleEventCounter = formatter.deserialize(&bytes).unwrap();
assert_eq!(decoded.intervals().len(), counter.intervals().len());
}
#[test]
fn test_bincode_extension() {
let formatter = BincodeFormat;
assert_eq!(formatter.extension(), ".bin");
}
}