mlt_core/decoder/
layer.rs1use 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 #[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 #[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 pub(crate) fn from_bytes(input: &'a [u8], parser: &mut Parser) -> MltRefResult<'a, Self> {
30 let (input, size) = parse_varint::<u32>(input)?;
31
32 let (input, tag) = parse_u8(input)?;
35 let size = size.checked_sub(1).ok_or(MltError::ZeroLayerSize)?;
37 let (input, value) = take(input, size)?;
38
39 let layer = match tag {
40 1 => Layer::Tag01(Layer01::from_bytes(value, parser)?),
42 tag => Layer::Unknown(Unknown { tag, value }),
43 };
44
45 Ok((input, layer))
46 }
47
48 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}