dvb_si/descriptors/
dsng.rs1use super::descriptor_body;
9use crate::error::{Error, Result};
10use dvb_common::{Parse, Serialize};
11
12pub const TAG: u8 = 0x68;
14pub const HEADER_LEN: usize = 2;
16
17#[derive(Debug, Clone, PartialEq, Eq)]
19#[cfg_attr(feature = "serde", derive(serde::Serialize))]
20#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
21pub struct DsngDescriptor<'a> {
22 pub bytes: &'a [u8],
24}
25
26impl<'a> Parse<'a> for DsngDescriptor<'a> {
27 type Error = crate::error::Error;
28 fn parse(bytes: &'a [u8]) -> Result<Self> {
29 let body = descriptor_body(
30 bytes,
31 TAG,
32 "DsngDescriptor",
33 "unexpected tag for DSNG_descriptor",
34 )?;
35 Ok(Self { bytes: body })
36 }
37}
38
39impl Serialize for DsngDescriptor<'_> {
40 type Error = crate::error::Error;
41 fn serialized_len(&self) -> usize {
42 HEADER_LEN + self.bytes.len()
43 }
44
45 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
46 if self.bytes.len() > u8::MAX as usize {
47 return Err(Error::InvalidDescriptor {
48 tag: TAG,
49 reason: "DSNG_descriptor body exceeds 255 bytes",
50 });
51 }
52 let len = self.serialized_len();
53 if buf.len() < len {
54 return Err(Error::OutputBufferTooSmall {
55 need: len,
56 have: buf.len(),
57 });
58 }
59 buf[0] = TAG;
60 buf[1] = self.bytes.len() as u8;
61 buf[HEADER_LEN..len].copy_from_slice(self.bytes);
62 Ok(len)
63 }
64}
65impl<'a> crate::traits::DescriptorDef<'a> for DsngDescriptor<'a> {
66 const TAG: u8 = TAG;
67 const NAME: &'static str = "DSNG";
68}
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73
74 #[test]
75 fn parse_extracts_ascii() {
76 let bytes = [TAG, 4, b'D', b'S', b'N', b'G'];
77 let d = DsngDescriptor::parse(&bytes).unwrap();
78 assert_eq!(d.bytes, b"DSNG");
79 }
80
81 #[test]
82 fn empty_body_is_valid() {
83 let bytes = [TAG, 0];
84 let d = DsngDescriptor::parse(&bytes).unwrap();
85 assert!(d.bytes.is_empty());
86 }
87
88 #[test]
89 fn parse_rejects_wrong_tag() {
90 assert!(matches!(
91 DsngDescriptor::parse(&[0x69, 0]).unwrap_err(),
92 Error::InvalidDescriptor { tag: 0x69, .. }
93 ));
94 }
95
96 #[test]
97 fn parse_rejects_short_header() {
98 assert!(matches!(
99 DsngDescriptor::parse(&[TAG]).unwrap_err(),
100 Error::BufferTooShort { .. }
101 ));
102 }
103
104 #[test]
105 fn parse_rejects_length_overrunning_buffer() {
106 let bytes = [TAG, 5, 1, 2, 3];
107 assert!(matches!(
108 DsngDescriptor::parse(&bytes).unwrap_err(),
109 Error::BufferTooShort { .. }
110 ));
111 }
112
113 #[test]
114 fn serialize_round_trip() {
115 let d = DsngDescriptor {
116 bytes: b"News Truck 4",
117 };
118 let mut buf = vec![0u8; d.serialized_len()];
119 d.serialize_into(&mut buf).unwrap();
120 assert_eq!(DsngDescriptor::parse(&buf).unwrap(), d);
121 }
122
123 #[test]
124 fn serialize_rejects_too_small_buffer() {
125 let d = DsngDescriptor { bytes: b"DSNG" };
126 let mut buf = vec![0u8; 1];
127 assert!(matches!(
128 d.serialize_into(&mut buf).unwrap_err(),
129 Error::OutputBufferTooSmall { .. }
130 ));
131 }
132
133 #[test]
134 fn serialize_rejects_over_range_body() {
135 let big = vec![0u8; 256];
136 let d = DsngDescriptor { bytes: &big };
137 let mut buf = vec![0u8; d.serialized_len()];
138 assert!(matches!(
139 d.serialize_into(&mut buf).unwrap_err(),
140 Error::InvalidDescriptor { tag: TAG, .. }
141 ));
142 }
143
144 }