dvb_si/descriptors/
iod.rs1use super::descriptor_body;
7use crate::error::{Error, Result};
8use dvb_common::{Parse, Serialize};
9
10pub const TAG: u8 = 0x1D;
12const HEADER_LEN: usize = 2;
13const FIXED_LEN: usize = 2;
14
15#[derive(Debug, Clone, PartialEq, Eq)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize))]
18#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
19pub struct IodDescriptor<'a> {
20 pub scope_of_iod_label: u8,
22 pub iod_label: u8,
24 #[cfg_attr(feature = "serde", serde(borrow))]
26 pub initial_object_descriptor: &'a [u8],
27}
28
29impl<'a> Parse<'a> for IodDescriptor<'a> {
30 type Error = crate::error::Error;
31
32 fn parse(bytes: &'a [u8]) -> Result<Self> {
33 let body = descriptor_body(
34 bytes,
35 TAG,
36 "IodDescriptor",
37 "unexpected tag for IOD_descriptor",
38 )?;
39 if body.len() < FIXED_LEN {
40 return Err(Error::InvalidDescriptor {
41 tag: TAG,
42 reason: "IOD_descriptor length too short (need >= 2)",
43 });
44 }
45 Ok(Self {
46 scope_of_iod_label: body[0],
47 iod_label: body[1],
48 initial_object_descriptor: &body[FIXED_LEN..],
49 })
50 }
51}
52
53impl Serialize for IodDescriptor<'_> {
54 type Error = crate::error::Error;
55
56 fn serialized_len(&self) -> usize {
57 HEADER_LEN + FIXED_LEN + self.initial_object_descriptor.len()
58 }
59
60 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
61 let len = self.serialized_len();
62 if buf.len() < len {
63 return Err(Error::OutputBufferTooSmall {
64 need: len,
65 have: buf.len(),
66 });
67 }
68 buf[0] = TAG;
69 buf[1] = (len - HEADER_LEN) as u8;
70 buf[HEADER_LEN] = self.scope_of_iod_label;
71 buf[HEADER_LEN + 1] = self.iod_label;
72 buf[HEADER_LEN + FIXED_LEN..len].copy_from_slice(self.initial_object_descriptor);
73 Ok(len)
74 }
75}
76impl<'a> crate::traits::DescriptorDef<'a> for IodDescriptor<'a> {
77 const TAG: u8 = TAG;
78 const NAME: &'static str = "IOD";
79}
80
81#[cfg(test)]
82mod tests {
83 use super::*;
84
85 #[test]
86 fn parse_minimal() {
87 let bytes = [TAG, 2, 0x01, 0x02];
88 let d = IodDescriptor::parse(&bytes).unwrap();
89 assert_eq!(d.scope_of_iod_label, 0x01);
90 assert_eq!(d.iod_label, 0x02);
91 assert!(d.initial_object_descriptor.is_empty());
92 }
93
94 #[test]
95 fn parse_with_opaque() {
96 let bytes = [TAG, 5, 0x0A, 0x0B, 0xCC, 0xDD, 0xEE];
97 let d = IodDescriptor::parse(&bytes).unwrap();
98 assert_eq!(d.scope_of_iod_label, 0x0A);
99 assert_eq!(d.iod_label, 0x0B);
100 assert_eq!(d.initial_object_descriptor, &[0xCC, 0xDD, 0xEE]);
101 }
102
103 #[test]
104 fn serialize_round_trip() {
105 let d = IodDescriptor {
106 scope_of_iod_label: 0x03,
107 iod_label: 0x04,
108 initial_object_descriptor: &[0xAA, 0xBB],
109 };
110 let mut buf = vec![0u8; d.serialized_len()];
111 d.serialize_into(&mut buf).unwrap();
112 let reparsed = IodDescriptor::parse(&buf).unwrap();
113 assert_eq!(d, reparsed);
114 }
115
116 #[test]
117 fn parse_rejects_wrong_tag() {
118 let err = IodDescriptor::parse(&[0x02, 2, 0, 0]).unwrap_err();
119 assert!(matches!(err, Error::InvalidDescriptor { tag: 0x02, .. }));
120 }
121
122 #[test]
123 fn parse_rejects_too_short() {
124 let err = IodDescriptor::parse(&[TAG, 1, 0]).unwrap_err();
125 assert!(matches!(err, Error::InvalidDescriptor { tag: TAG, .. }));
126 }
127
128 #[test]
129 fn serialize_rejects_small_buffer() {
130 let d = IodDescriptor {
131 scope_of_iod_label: 0,
132 iod_label: 0,
133 initial_object_descriptor: &[],
134 };
135 let mut tiny = vec![0u8; 3];
136 let err = d.serialize_into(&mut tiny).unwrap_err();
137 assert!(matches!(err, Error::OutputBufferTooSmall { .. }));
138 }
139}