Skip to main content

dvb_si/descriptors/
dsng.rs

1//! DSNG Descriptor — ETSI EN 300 468 §6.2.15 (tag 0x68, Table 52, PDF p. 78).
2//!
3//! Digital Satellite News Gathering. Carried in the NIT. The body is a single
4//! `for (i=0;i<N;i++) { byte }` loop (Table 52); §6.2.15 defines the bytes as
5//! the textual DSNG identifier carried as ASCII characters. We preserve the
6//! raw bytes verbatim.
7
8use crate::error::{Error, Result};
9use crate::traits::Descriptor;
10use dvb_common::{Parse, Serialize};
11
12/// Descriptor tag for DSNG_descriptor.
13pub const TAG: u8 = 0x68;
14/// Length of the header (tag byte + length byte).
15pub const HEADER_LEN: usize = 2;
16
17/// DSNG Descriptor.
18#[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    /// Raw DSNG identifier bytes (ASCII per §6.2.15).
23    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        if bytes.len() < HEADER_LEN {
30            return Err(Error::BufferTooShort {
31                need: HEADER_LEN,
32                have: bytes.len(),
33                what: "DsngDescriptor header",
34            });
35        }
36        if bytes[0] != TAG {
37            return Err(Error::InvalidDescriptor {
38                tag: bytes[0],
39                reason: "unexpected tag for DSNG_descriptor",
40            });
41        }
42        let length = bytes[1] as usize;
43        let end = HEADER_LEN + length;
44        if bytes.len() < end {
45            return Err(Error::BufferTooShort {
46                need: end,
47                have: bytes.len(),
48                what: "DsngDescriptor body",
49            });
50        }
51        Ok(Self {
52            bytes: &bytes[HEADER_LEN..end],
53        })
54    }
55}
56
57impl Serialize for DsngDescriptor<'_> {
58    type Error = crate::error::Error;
59    fn serialized_len(&self) -> usize {
60        HEADER_LEN + self.bytes.len()
61    }
62
63    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
64        if self.bytes.len() > u8::MAX as usize {
65            return Err(Error::InvalidDescriptor {
66                tag: TAG,
67                reason: "DSNG_descriptor body exceeds 255 bytes",
68            });
69        }
70        let len = self.serialized_len();
71        if buf.len() < len {
72            return Err(Error::OutputBufferTooSmall {
73                need: len,
74                have: buf.len(),
75            });
76        }
77        buf[0] = TAG;
78        buf[1] = self.bytes.len() as u8;
79        buf[HEADER_LEN..len].copy_from_slice(self.bytes);
80        Ok(len)
81    }
82}
83
84impl<'a> Descriptor<'a> for DsngDescriptor<'a> {
85    const TAG: u8 = TAG;
86    fn descriptor_length(&self) -> u8 {
87        self.bytes.len() as u8
88    }
89}
90
91impl<'a> crate::traits::DescriptorDef<'a> for DsngDescriptor<'a> {
92    const TAG: u8 = TAG;
93    const NAME: &'static str = "DSNG";
94}
95
96#[cfg(test)]
97mod tests {
98    use super::*;
99
100    #[test]
101    fn parse_extracts_ascii() {
102        let bytes = [TAG, 4, b'D', b'S', b'N', b'G'];
103        let d = DsngDescriptor::parse(&bytes).unwrap();
104        assert_eq!(d.bytes, b"DSNG");
105    }
106
107    #[test]
108    fn empty_body_is_valid() {
109        let bytes = [TAG, 0];
110        let d = DsngDescriptor::parse(&bytes).unwrap();
111        assert!(d.bytes.is_empty());
112    }
113
114    #[test]
115    fn parse_rejects_wrong_tag() {
116        assert!(matches!(
117            DsngDescriptor::parse(&[0x69, 0]).unwrap_err(),
118            Error::InvalidDescriptor { tag: 0x69, .. }
119        ));
120    }
121
122    #[test]
123    fn parse_rejects_short_header() {
124        assert!(matches!(
125            DsngDescriptor::parse(&[TAG]).unwrap_err(),
126            Error::BufferTooShort { .. }
127        ));
128    }
129
130    #[test]
131    fn parse_rejects_length_overrunning_buffer() {
132        let bytes = [TAG, 5, 1, 2, 3];
133        assert!(matches!(
134            DsngDescriptor::parse(&bytes).unwrap_err(),
135            Error::BufferTooShort { .. }
136        ));
137    }
138
139    #[test]
140    fn serialize_round_trip() {
141        let d = DsngDescriptor {
142            bytes: b"News Truck 4",
143        };
144        let mut buf = vec![0u8; d.serialized_len()];
145        d.serialize_into(&mut buf).unwrap();
146        assert_eq!(DsngDescriptor::parse(&buf).unwrap(), d);
147    }
148
149    #[test]
150    fn serialize_rejects_too_small_buffer() {
151        let d = DsngDescriptor { bytes: b"DSNG" };
152        let mut buf = vec![0u8; 1];
153        assert!(matches!(
154            d.serialize_into(&mut buf).unwrap_err(),
155            Error::OutputBufferTooSmall { .. }
156        ));
157    }
158
159    #[test]
160    fn serialize_rejects_over_range_body() {
161        let big = vec![0u8; 256];
162        let d = DsngDescriptor { bytes: &big };
163        let mut buf = vec![0u8; d.serialized_len()];
164        assert!(matches!(
165            d.serialize_into(&mut buf).unwrap_err(),
166            Error::InvalidDescriptor { tag: TAG, .. }
167        ));
168    }
169
170    // No JSON serde round-trip test: the borrowed `&[u8]` field cannot
171    // round-trip through serde_json (it serializes a slice as a sequence,
172    // not a borrowed byte array). This matches sibling borrowed-bytes
173    // descriptors (linkage.rs, default_authority.rs, content_identifier.rs),
174    // none of which carry a serde_round_trip test. The serde derive itself
175    // is still exercised by compilation under `--all-features`.
176}