Skip to main content

mp4_atom/moov/udta/
rtng.rs

1use crate::*;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5pub struct Rtng {
6    pub entity: FourCC,
7    pub criteria: FourCC,
8    pub language: String,
9    pub rating_info: String,
10}
11
12impl AtomExt for Rtng {
13    type Ext = ();
14
15    const KIND_EXT: FourCC = FourCC::new(b"rtng");
16
17    fn decode_body_ext<B: Buf>(buf: &mut B, _ext: ()) -> Result<Self> {
18        let entity = FourCC::decode(buf)?;
19        let criteria = FourCC::decode(buf)?;
20        let language_code = u16::decode(buf)?;
21        let language = language_string(language_code);
22        let mut rating_info = decode_utf8_string(buf)?;
23        if rating_info.ends_with('\0') {
24            rating_info.truncate(rating_info.len() - 1);
25        }
26        Ok(Rtng {
27            entity,
28            criteria,
29            language,
30            rating_info,
31        })
32    }
33
34    fn encode_body_ext<B: BufMut>(&self, buf: &mut B) -> Result<()> {
35        self.entity.encode(buf)?;
36        self.criteria.encode(buf)?;
37        let language_code = language_code(&self.language);
38        language_code.encode(buf)?;
39        self.rating_info.as_str().encode(buf)?;
40        Ok(())
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use crate::*;
47
48    const ENCODED_RTNG: &[u8] = &[
49        0x00, 0x00, 0x00, 0x1d, 0x72, 0x74, 0x6e, 0x67, 0x00, 0x00, 0x00, 0x00, 0x4d, 0x50, 0x41,
50        0x41, 0x00, 0x00, 0x00, 0x00, 0x15, 0xc7, 0x47, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51    ];
52
53    #[test]
54    fn test_rtng_decode() {
55        let buf: &mut std::io::Cursor<&&[u8]> = &mut std::io::Cursor::new(&ENCODED_RTNG);
56        let rtng = Rtng::decode(buf).expect("failed to decode rtng");
57        assert_eq!(
58            rtng,
59            Rtng {
60                entity: b"MPAA".into(),
61                criteria: b"\0\0\0\0".into(),
62                language: "eng".into(),
63                rating_info: "G\0\0\0\0\0".into(),
64            }
65        );
66    }
67
68    #[test]
69    fn test_rtng_encode() {
70        let rtng = Rtng {
71            entity: b"MPAA".into(),
72            criteria: b"\0\0\0\0".into(),
73            language: "eng".into(),
74            rating_info: "G\0\0\0\0\0".into(),
75        };
76
77        let mut buf = Vec::new();
78        rtng.encode(&mut buf).unwrap();
79        assert_eq!(buf, ENCODED_RTNG);
80    }
81}