Skip to main content

dvb_si/descriptors/
adaptation_field_data.rs

1//! Adaptation Field Data Descriptor — ETSI EN 300 468 §6.2.1 (tag 0x70, Table 13, PDF p. 54).
2//!
3//! Carried inside the PMT ES_info loop. Fixed 1-byte body: a bit-flag field
4//! `adaptation_field_data_identifier` signalling which data fields are carried
5//! in the adaptation field private_data (Table 14: announcement_switching_data,
6//! AU_information, PVR_assist_information). We carry the raw flag byte.
7
8use crate::error::{Error, Result};
9use crate::traits::Descriptor;
10use dvb_common::{Parse, Serialize};
11
12/// Descriptor tag for adaptation_field_data_descriptor.
13pub const TAG: u8 = 0x70;
14/// Length of the header (tag byte + length byte).
15pub const HEADER_LEN: usize = 2;
16/// Fixed body length: one identifier flag byte.
17pub const BODY_LEN: usize = 1;
18
19/// Adaptation Field Data Descriptor.
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21#[cfg_attr(feature = "serde", derive(serde::Serialize))]
22pub struct AdaptationFieldDataDescriptor {
23    /// 8-bit adaptation_field_data_identifier flag field (Table 14).
24    pub adaptation_field_data_identifier: u8,
25}
26
27impl<'a> Parse<'a> for AdaptationFieldDataDescriptor {
28    type Error = crate::error::Error;
29    fn parse(bytes: &'a [u8]) -> Result<Self> {
30        if bytes.len() < HEADER_LEN {
31            return Err(Error::BufferTooShort {
32                need: HEADER_LEN,
33                have: bytes.len(),
34                what: "AdaptationFieldDataDescriptor header",
35            });
36        }
37        if bytes[0] != TAG {
38            return Err(Error::InvalidDescriptor {
39                tag: bytes[0],
40                reason: "unexpected tag for adaptation_field_data_descriptor",
41            });
42        }
43        let length = bytes[1] as usize;
44        if length != BODY_LEN {
45            return Err(Error::InvalidDescriptor {
46                tag: TAG,
47                reason: "adaptation_field_data_descriptor length must be exactly 1",
48            });
49        }
50        let end = HEADER_LEN + length;
51        if bytes.len() < end {
52            return Err(Error::BufferTooShort {
53                need: end,
54                have: bytes.len(),
55                what: "AdaptationFieldDataDescriptor body",
56            });
57        }
58        Ok(Self {
59            adaptation_field_data_identifier: bytes[HEADER_LEN],
60        })
61    }
62}
63
64impl Serialize for AdaptationFieldDataDescriptor {
65    type Error = crate::error::Error;
66    fn serialized_len(&self) -> usize {
67        HEADER_LEN + BODY_LEN
68    }
69
70    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
71        let len = self.serialized_len();
72        if buf.len() < len {
73            return Err(Error::OutputBufferTooSmall {
74                need: len,
75                have: buf.len(),
76            });
77        }
78        buf[0] = TAG;
79        buf[1] = BODY_LEN as u8;
80        buf[HEADER_LEN] = self.adaptation_field_data_identifier;
81        Ok(len)
82    }
83}
84
85impl<'a> Descriptor<'a> for AdaptationFieldDataDescriptor {
86    const TAG: u8 = TAG;
87    fn descriptor_length(&self) -> u8 {
88        BODY_LEN as u8
89    }
90}
91
92impl<'a> crate::traits::DescriptorDef<'a> for AdaptationFieldDataDescriptor {
93    const TAG: u8 = TAG;
94    const NAME: &'static str = "ADAPTATION_FIELD_DATA";
95}
96
97#[cfg(test)]
98mod tests {
99    use super::*;
100
101    #[test]
102    fn parse_extracts_identifier() {
103        let bytes = [TAG, 1, 0x07];
104        let d = AdaptationFieldDataDescriptor::parse(&bytes).unwrap();
105        assert_eq!(d.adaptation_field_data_identifier, 0x07);
106    }
107
108    #[test]
109    fn parse_rejects_wrong_tag() {
110        assert!(matches!(
111            AdaptationFieldDataDescriptor::parse(&[0x71, 1, 0]).unwrap_err(),
112            Error::InvalidDescriptor { tag: 0x71, .. }
113        ));
114    }
115
116    #[test]
117    fn parse_rejects_wrong_length() {
118        assert!(matches!(
119            AdaptationFieldDataDescriptor::parse(&[TAG, 0]).unwrap_err(),
120            Error::InvalidDescriptor { tag: TAG, .. }
121        ));
122    }
123
124    #[test]
125    fn parse_rejects_short_body() {
126        assert!(matches!(
127            AdaptationFieldDataDescriptor::parse(&[TAG, 1]).unwrap_err(),
128            Error::BufferTooShort { .. }
129        ));
130    }
131
132    #[test]
133    fn serialize_round_trip() {
134        let d = AdaptationFieldDataDescriptor {
135            adaptation_field_data_identifier: 0x05,
136        };
137        let mut buf = vec![0u8; d.serialized_len()];
138        d.serialize_into(&mut buf).unwrap();
139        assert_eq!(buf, [TAG, 1, 0x05]);
140        assert_eq!(AdaptationFieldDataDescriptor::parse(&buf).unwrap(), d);
141    }
142
143    #[test]
144    fn serialize_rejects_too_small_buffer() {
145        let d = AdaptationFieldDataDescriptor {
146            adaptation_field_data_identifier: 0,
147        };
148        let mut buf = vec![0u8; 2];
149        assert!(matches!(
150            d.serialize_into(&mut buf).unwrap_err(),
151            Error::OutputBufferTooSmall { .. }
152        ));
153    }
154
155    #[cfg(feature = "serde")]
156    #[test]
157    fn serde_round_trip() {
158        let d = AdaptationFieldDataDescriptor {
159            adaptation_field_data_identifier: 0x05,
160        };
161        let json = serde_json::to_string(&d).unwrap();
162        // Serialize-only: assert the emitted JSON re-parses (serialize-stable).
163        let _v: serde_json::Value = serde_json::from_str(&json).unwrap();
164    }
165}