1use std::io;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct TypeWrapper {
7 pub type_id: String,
8 pub data: Vec<u8>,
9}
10
11pub fn encode_history_frames(
13 frames: &[TypeWrapper],
14 compress: bool,
15) -> io::Result<Vec<u8>> {
16 let bytes =
17 bincode::serde::encode_to_vec(frames, bincode::config::standard())
18 .map_err(io::Error::other)?;
19 if compress {
20 Ok(zstd::stream::encode_all(&bytes[..], 1).map_err(io::Error::other)?)
21 } else {
22 Ok(bytes)
23 }
24}
25
26pub fn decode_history_frames(
28 bytes: &[u8],
29 compressed: bool,
30) -> io::Result<Vec<TypeWrapper>> {
31 let raw = if compressed {
32 zstd::stream::decode_all(bytes).map_err(io::Error::other)?
33 } else {
34 bytes.to_vec()
35 };
36 let (frames, _) = bincode::serde::decode_from_slice::<Vec<TypeWrapper>, _>(
37 &raw,
38 bincode::config::standard(),
39 )
40 .map_err(io::Error::other)?;
41 Ok(frames)
42}