dvb_si/descriptors/
muxcode.rs1use super::descriptor_body;
7use crate::error::{Error, Result};
8use dvb_common::{Parse, Serialize};
9
10pub const TAG: u8 = 0x21;
12const HEADER_LEN: usize = 2;
13
14#[derive(Debug, Clone, PartialEq, Eq)]
16#[cfg_attr(feature = "serde", derive(serde::Serialize))]
17#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
18pub struct MuxcodeDescriptor<'a> {
19 #[cfg_attr(feature = "serde", serde(borrow))]
21 pub mux_code_table_entries: &'a [u8],
22}
23
24impl<'a> Parse<'a> for MuxcodeDescriptor<'a> {
25 type Error = crate::error::Error;
26
27 fn parse(bytes: &'a [u8]) -> Result<Self> {
28 let body = descriptor_body(
29 bytes,
30 TAG,
31 "MuxcodeDescriptor",
32 "unexpected tag for Muxcode_descriptor",
33 )?;
34 Ok(Self {
35 mux_code_table_entries: body,
36 })
37 }
38}
39
40impl Serialize for MuxcodeDescriptor<'_> {
41 type Error = crate::error::Error;
42
43 fn serialized_len(&self) -> usize {
44 HEADER_LEN + self.mux_code_table_entries.len()
45 }
46
47 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
48 let len = self.serialized_len();
49 if buf.len() < len {
50 return Err(Error::OutputBufferTooSmall {
51 need: len,
52 have: buf.len(),
53 });
54 }
55 buf[0] = TAG;
56 buf[1] = self.mux_code_table_entries.len() as u8;
57 buf[HEADER_LEN..len].copy_from_slice(self.mux_code_table_entries);
58 Ok(len)
59 }
60}
61impl<'a> crate::traits::DescriptorDef<'a> for MuxcodeDescriptor<'a> {
62 const TAG: u8 = TAG;
63 const NAME: &'static str = "MUXCODE";
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 #[test]
71 fn parse_empty() {
72 let d = MuxcodeDescriptor::parse(&[TAG, 0]).unwrap();
73 assert!(d.mux_code_table_entries.is_empty());
74 }
75
76 #[test]
77 fn parse_with_data() {
78 let bytes = [TAG, 3, 0xAA, 0xBB, 0xCC];
79 let d = MuxcodeDescriptor::parse(&bytes).unwrap();
80 assert_eq!(d.mux_code_table_entries, &[0xAA, 0xBB, 0xCC]);
81 }
82
83 #[test]
84 fn parse_rejects_wrong_tag() {
85 let err = MuxcodeDescriptor::parse(&[0x02, 0]).unwrap_err();
86 assert!(matches!(err, Error::InvalidDescriptor { tag: 0x02, .. }));
87 }
88
89 #[test]
90 fn serialize_round_trip() {
91 let d = MuxcodeDescriptor {
92 mux_code_table_entries: &[0x11, 0x22, 0x33],
93 };
94 let mut buf = vec![0u8; d.serialized_len()];
95 d.serialize_into(&mut buf).unwrap();
96 let reparsed = MuxcodeDescriptor::parse(&buf).unwrap();
97 assert_eq!(d, reparsed);
98 }
99
100 #[test]
101 fn serialize_rejects_small_buffer() {
102 let d = MuxcodeDescriptor {
103 mux_code_table_entries: &[1, 2],
104 };
105 let mut tiny = vec![0u8; 2];
106 let err = d.serialize_into(&mut tiny).unwrap_err();
107 assert!(matches!(err, Error::OutputBufferTooSmall { .. }));
108 }
109}