use std::io;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TypeWrapper {
pub type_id: String,
pub data: Vec<u8>,
}
pub fn encode_history_frames(
frames: &[TypeWrapper],
compress: bool,
) -> io::Result<Vec<u8>> {
let bytes =
bincode::serde::encode_to_vec(frames, bincode::config::standard())
.map_err(io::Error::other)?;
if compress {
Ok(zstd::stream::encode_all(&bytes[..], 1).map_err(io::Error::other)?)
} else {
Ok(bytes)
}
}
pub fn decode_history_frames(
bytes: &[u8],
compressed: bool,
) -> io::Result<Vec<TypeWrapper>> {
let raw = if compressed {
zstd::stream::decode_all(bytes).map_err(io::Error::other)?
} else {
bytes.to_vec()
};
let (frames, _) = bincode::serde::decode_from_slice::<Vec<TypeWrapper>, _>(
&raw,
bincode::config::standard(),
)
.map_err(io::Error::other)?;
Ok(frames)
}