Skip to main content

mlt_core/encoder/
unknown.rs

1use integer_encoding::VarIntWriter as _;
2
3use crate::MltResult;
4use crate::decoder::Unknown;
5use crate::encoder::Encoder;
6use crate::encoder::model::EncodedUnknown;
7use crate::utils::{BinarySerializer as _, checked_sum2};
8
9impl EncodedUnknown {
10    /// Serialize an unknown layer record directly to [`enc.data`](Encoder::data).
11    ///
12    /// Writes the complete `[varint(size)][tag][value]` record — the bytes are
13    /// already in wire format so no `hdr`/`meta` split is needed.
14    pub fn write_to(&self, mut enc: Encoder) -> MltResult<Encoder> {
15        let buffer_len = u32::try_from(self.value.len())?;
16        let size = checked_sum2(buffer_len, 1)?;
17        enc.write_varint(size)?;
18        enc.write_u8(self.tag)?;
19        enc.data.extend_from_slice(&self.value);
20        Ok(enc)
21    }
22}
23
24impl<'a> From<Unknown<'a>> for EncodedUnknown {
25    fn from(u: Unknown<'a>) -> Self {
26        Self {
27            tag: u.tag,
28            value: u.value.to_vec(),
29        }
30    }
31}
32
33#[cfg(all(not(test), feature = "arbitrary"))]
34impl arbitrary::Arbitrary<'_> for EncodedUnknown {
35    fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<Self> {
36        let mut tag: u8 = u.arbitrary()?;
37        // Tag 1 is the known Tag01 format; producing it as Unknown would break round-trip-ability
38        if tag == 1 {
39            tag = 0;
40        }
41        Ok(Self {
42            tag,
43            value: u.arbitrary()?,
44        })
45    }
46}