mf_file/
history.rs

1use std::io;
2use serde::{Deserialize, Serialize};
3
4// 步骤帧:type_id 表示类型,data 为该类型的序列化字节
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct TypeWrapper {
7    pub type_id: String,
8    pub data: Vec<u8>,
9}
10
11// 编码步骤帧;可选 zstd 压缩
12pub 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
26// 解码步骤帧;如 compressed 为真先解压
27pub 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}