forked from agavra/compression-golf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnaive.rs
More file actions
36 lines (29 loc) · 874 Bytes
/
naive.rs
File metadata and controls
36 lines (29 loc) · 874 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//! # Naive Codec (Baseline)
//!
//! **Strategy:** Plain JSON serialization using serde_json.
//!
//! This is the simplest possible implementation - just serialize the entire
//! event list to JSON. It serves as the baseline for comparison.
use bytes::Bytes;
use std::error::Error;
use crate::codec::EventCodec;
use crate::{EventKey, EventValue};
pub struct NaiveCodec;
impl NaiveCodec {
pub fn new() -> Self {
Self
}
}
impl EventCodec for NaiveCodec {
fn name(&self) -> &str {
"Naive"
}
fn encode(&self, events: &[(EventKey, EventValue)]) -> Result<Bytes, Box<dyn Error>> {
let json = serde_json::to_vec(events)?;
Ok(Bytes::from(json))
}
fn decode(&self, bytes: &[u8]) -> Result<Vec<(EventKey, EventValue)>, Box<dyn Error>> {
let events = serde_json::from_slice(bytes)?;
Ok(events)
}
}