Skip to main content

mlt_core/decoder/
layer.rs

1use crate::codecs::varint::parse_varint;
2use crate::decoder::{Layer01, Unknown};
3use crate::utils::{parse_u8, take};
4use crate::{DecodeState, Decoder, Layer, MltError, MltRefResult, MltResult, ParsedLayer, Parser};
5
6impl<'a, S: DecodeState> Layer<'a, S> {
7    /// Returns the inner `Layer01` if this is a Tag01 layer, or `None` otherwise.
8    #[must_use]
9    pub fn as_layer01(&self) -> Option<&Layer01<'a, S>> {
10        match self {
11            Self::Tag01(l) => Some(l),
12            Self::Unknown(_) => None,
13        }
14    }
15
16    /// Consumes this layer and returns the inner `Layer01`, or `None` if it is not a Tag01 layer.
17    #[must_use]
18    pub fn into_layer01(self) -> Option<Layer01<'a, S>> {
19        match self {
20            Self::Tag01(l) => Some(l),
21            Self::Unknown(_) => None,
22        }
23    }
24}
25
26impl<'a> Layer<'a> {
27    /// Parse a single tuple that consists of `size (varint)`, `tag (varint)`, and `value (bytes)`.
28    /// Reserves memory for decoded data against the parser's budget.
29    pub(crate) fn from_bytes(input: &'a [u8], parser: &mut Parser) -> MltRefResult<'a, Self> {
30        let (input, size) = parse_varint::<u32>(input)?;
31
32        // tag is a varint, but we know fewer than 127 tags for now,
33        // so we can use a faster u8 and fail if it is bigger than 127.
34        let (input, tag) = parse_u8(input)?;
35        // 1 byte must be parsed for the tag, so if size is 0, it's invalid
36        let size = size.checked_sub(1).ok_or(MltError::ZeroLayerSize)?;
37        let (input, value) = take(input, size)?;
38
39        let layer = match tag {
40            // For now, we only support tag 0x01 layers, but more will be added soon
41            1 => Layer::Tag01(Layer01::from_bytes(value, parser)?),
42            tag => Layer::Unknown(Unknown { tag, value }),
43        };
44
45        Ok((input, layer))
46    }
47
48    /// Decode all columns and return a fully-decoded [`ParsedLayer`].
49    ///
50    /// Consumes `self`.  For partial / incremental decoding, destructure with
51    /// `Layer::Tag01(lazy)` and call the individual methods on [`Layer01`].
52    pub fn decode_all(self, dec: &mut Decoder) -> MltResult<ParsedLayer<'a>> {
53        match self {
54            Layer::Tag01(lazy) => Ok(Layer::Tag01(lazy.decode_all(dec)?)),
55            Layer::Unknown(u) => Ok(Layer::Unknown(u)),
56        }
57    }
58}