mp4_atom/moov/trak/mdia/minf/stbl/stsd/
fiel.rs

1use crate::*;
2
3#[derive(Clone, Debug, Default, Eq, PartialEq)]
4#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5pub struct Fiel {
6    pub field_count: u8,
7    pub field_order: u8,
8}
9
10impl Fiel {
11    pub fn new(field_count: u8, field_order: u8) -> Result<Self> {
12        Ok(Fiel {
13            field_count,
14            field_order,
15        })
16    }
17}
18
19impl Atom for Fiel {
20    const KIND: FourCC = FourCC::new(b"fiel");
21
22    fn decode_body<B: Buf>(buf: &mut B) -> Result<Self> {
23        let field_count = u8::decode(buf)?;
24        let field_order = u8::decode(buf)?;
25
26        Ok(Fiel {
27            field_count,
28            field_order,
29        })
30    }
31
32    fn encode_body<B: BufMut>(&self, buf: &mut B) -> Result<()> {
33        self.field_count.encode(buf)?;
34        self.field_order.encode(buf)?;
35        Ok(())
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    const ENCODED_FIEL: &[u8] = &[0x00, 0x00, 0x00, 0x0a, b'f', b'i', b'e', b'l', 0x01, 0x06];
44
45    #[test]
46    fn test_fiel_decode() {
47        let buf: &mut std::io::Cursor<&&[u8]> = &mut std::io::Cursor::new(&ENCODED_FIEL);
48
49        let fiel = Fiel::decode(buf).expect("failed to decode fiel");
50
51        assert_eq!(
52            fiel,
53            Fiel {
54                field_count: 1,
55                field_order: 6,
56            }
57        );
58    }
59
60    #[test]
61    fn test_fiel_encode() {
62        let fiel = Fiel {
63            field_count: 1,
64            field_order: 6,
65        };
66
67        let mut buf = Vec::new();
68        fiel.encode(&mut buf).unwrap();
69
70        assert_eq!(buf.as_slice(), ENCODED_FIEL);
71    }
72}