Skip to main content

mlt_core/decoder/
layer.rs

1use crate::codecs::varint::parse_varint;
2#[cfg(feature = "unstable-v2")]
3use crate::decoder::root02::parse_layer02;
4use crate::decoder::{Layer01, ParsedLayer01, Unknown};
5use crate::utils::{parse_u8, take};
6use crate::{
7    DecodeState, Decoder, Layer, Lazy, MltError, MltRefResult, MltResult, ParsedLayer, Parser,
8};
9
10impl<'a, S: DecodeState> Layer<'a, S> {
11    /// Returns the inner [`Layer01`] for any layer stored in the `Layer01`
12    /// in-memory representation (both `Tag01` and `Tag02`), or `None` otherwise.
13    #[must_use]
14    pub fn as_layer01(&self) -> Option<&Layer01<'a, S>> {
15        match self {
16            Self::Tag01(l) => Some(l),
17            #[cfg(feature = "unstable-v2")]
18            Self::Tag02(l) => Some(l),
19            Self::Unknown(_) => None,
20        }
21    }
22
23    /// Consumes this layer and returns the inner [`Layer01`] for any layer stored
24    /// in the `Layer01` in-memory representation, or `None` otherwise.
25    #[must_use]
26    pub fn into_layer01(self) -> Option<Layer01<'a, S>> {
27        match self {
28            Self::Tag01(l) => Some(l),
29            #[cfg(feature = "unstable-v2")]
30            Self::Tag02(l) => Some(l),
31            Self::Unknown(_) => None,
32        }
33    }
34}
35
36impl<'a> Layer<'a> {
37    /// Parse a single tuple that consists of `size (varint)`, `tag (varint)`, and `value (bytes)`.
38    /// Reserves memory for decoded data against the parser's budget.
39    pub(crate) fn from_bytes(input: &'a [u8], parser: &mut Parser) -> MltRefResult<'a, Self> {
40        let (input, size) = parse_varint::<u32>(input)?;
41
42        // tag is a varint, but we know fewer than 127 tags for now,
43        // so we can use a faster u8 and fail if it is bigger than 127.
44        let (input, tag) = parse_u8(input)?;
45        // 1 byte must be parsed for the tag, so if size is 0, it's invalid
46        let size = size.checked_sub(1).ok_or(MltError::ZeroLayerSize)?;
47        let (input, value) = take(input, size)?;
48
49        let layer = match tag {
50            1 => Layer::Tag01(Layer01::from_bytes(value, parser)?),
51            #[cfg(feature = "unstable-v2")]
52            2 => Layer::Tag02(parse_layer02(value, parser)?),
53            tag => Layer::Unknown(Unknown { tag, value }),
54        };
55
56        Ok((input, layer))
57    }
58
59    /// Decode all columns and return a fully-decoded [`ParsedLayer`].
60    ///
61    /// Consumes `self`.  For partial / incremental decoding, destructure with
62    /// `Layer::Tag01(lazy)` and call the individual methods on [`Layer01`].
63    pub fn decode_all(self, dec: &mut Decoder) -> MltResult<ParsedLayer<'a>> {
64        match self {
65            Layer::Tag01(v) => Ok(Layer::Tag01(v.decode_all(dec)?)),
66            #[cfg(feature = "unstable-v2")]
67            Layer::Tag02(v) => Ok(Layer::Tag02(v.decode_all(dec)?)),
68            Layer::Unknown(u) => Ok(Layer::Unknown(u)),
69        }
70    }
71}
72
73impl<'a> Layer01<'a, Lazy> {
74    /// Decode all columns and transition to [`Layer01<Parsed>`].
75    ///
76    /// Consumes `self` (a `Layer01<Lazy>`) and returns a `Layer01<Parsed>` where every
77    /// column field holds its parsed value directly, enabling infallible readonly access.
78    pub fn decode_all(self, dec: &mut Decoder) -> MltResult<ParsedLayer01<'a>> {
79        Ok(Layer01 {
80            name: self.name,
81            extent: self.extent,
82            id: self.id.map(|id| id.into_parsed(dec)).transpose()?,
83            geometry: self.geometry.into_parsed(dec)?,
84            properties: self
85                .properties
86                .into_iter()
87                .map(|p| p.into_parsed(dec))
88                .collect::<MltResult<Vec<_>>>()?,
89            #[cfg(fuzzing)]
90            layer_order: self.layer_order,
91        })
92    }
93}