flow_record/
raw_flow_record.rs1use std::io::Cursor;
2
3use binrw::{helpers::count, BinRead, BinReaderExt, BinWrite};
4use rmpv::Value;
5
6pub struct RawFlowRecord(Value);
7
8impl BinWrite for RawFlowRecord {
9 type Args<'a> = ();
10
11 fn write_options<W: std::io::Write + std::io::Seek>(
12 &self,
13 writer: &mut W,
14 _endian: binrw::Endian,
15 _args: Self::Args<'_>,
16 ) -> binrw::BinResult<()> {
17 let mut data = Vec::new();
18
19 if let Err(why) = rmpv::encode::write_value(&mut data, &self.0) {
20 return Err(binrw::Error::Custom {
21 pos: writer.stream_position()?,
22 err: Box::new(why),
23 });
24 }
25
26 let length: u32 = data.len().try_into().unwrap();
27 length.write_be(writer)?;
28 data.write_be(writer)?;
29 Ok(())
30 }
31}
32
33impl BinRead for RawFlowRecord {
34 type Args<'a> = ();
35
36 fn read_options<R: std::io::Read + std::io::Seek>(
37 reader: &mut R,
38 endian: binrw::Endian,
39 args: Self::Args<'_>,
40 ) -> binrw::BinResult<Self> {
41 let length: u32 = reader.read_be()?;
42 let data: Vec<u8> = count(length.try_into().unwrap())(reader, endian, args)?;
43
44 match rmpv::decode::read_value(&mut Cursor::new(data)) {
45 Ok(value) => Ok(Self(value)),
46 Err(why) => Err(binrw::Error::Custom {
47 pos: reader.stream_position()?,
48 err: Box::new(why),
49 }),
50 }
51 }
52}
53
54impl From<Value> for RawFlowRecord {
55 fn from(data: Value) -> Self {
56 Self(data)
57 }
58}
59
60impl From<RawFlowRecord> for Value {
61 fn from(value: RawFlowRecord) -> Self {
62 value.0
63 }
64}