Skip to main content

dvb_si/descriptors/
private_data_indicator.rs

1//! Private Data Indicator Descriptor — ISO/IEC 13818-1 §2.6.22 (tag 0x0F).
2//!
3//! Carries a 4-byte private data specifier that identifies the organization
4//! or entity that defined the private data carried in the associated stream.
5
6use super::descriptor_body;
7use crate::error::{Error, Result};
8use dvb_common::{Parse, Serialize};
9
10/// Descriptor tag for private_data_indicator_descriptor.
11pub const TAG: u8 = 0x0F;
12const HEADER_LEN: usize = 2;
13const BODY_LEN: u8 = 4;
14
15/// Private Data Indicator Descriptor.
16///
17/// The `private_data_specifier` is a 4-byte value assigned by a standards body
18/// or industry consortium to identify the organization that defined the private
19/// data format used in the associated elementary stream.
20#[derive(Debug, Clone, PartialEq, Eq)]
21#[cfg_attr(feature = "serde", derive(serde::Serialize))]
22pub struct PrivateDataIndicatorDescriptor {
23    /// 4-byte private data specifier identifier.
24    pub private_data_specifier: [u8; 4],
25}
26
27impl<'a> Parse<'a> for PrivateDataIndicatorDescriptor {
28    type Error = crate::error::Error;
29
30    fn parse(bytes: &'a [u8]) -> Result<Self> {
31        let body = descriptor_body(
32            bytes,
33            TAG,
34            "PrivateDataIndicatorDescriptor",
35            "unexpected tag for private_data_indicator_descriptor",
36        )?;
37        if body.len() != BODY_LEN as usize {
38            return Err(Error::InvalidDescriptor {
39                tag: TAG,
40                reason: "private_data_indicator_descriptor length must equal 4",
41            });
42        }
43        let mut private_data_specifier = [0u8; 4];
44        private_data_specifier.copy_from_slice(&body[..4]);
45        Ok(Self {
46            private_data_specifier,
47        })
48    }
49}
50
51impl Serialize for PrivateDataIndicatorDescriptor {
52    type Error = crate::error::Error;
53
54    fn serialized_len(&self) -> usize {
55        HEADER_LEN + BODY_LEN as usize
56    }
57
58    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
59        let len = self.serialized_len();
60        if buf.len() < len {
61            return Err(Error::OutputBufferTooSmall {
62                need: len,
63                have: buf.len(),
64            });
65        }
66        buf[0] = TAG;
67        buf[1] = BODY_LEN;
68        buf[HEADER_LEN..HEADER_LEN + 4].copy_from_slice(&self.private_data_specifier);
69        Ok(len)
70    }
71}
72impl<'a> crate::traits::DescriptorDef<'a> for PrivateDataIndicatorDescriptor {
73    const TAG: u8 = TAG;
74    const NAME: &'static str = "PRIVATE_DATA_INDICATOR";
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    #[test]
82    fn parse_private_data_specifier() {
83        let bytes = [TAG, 4, 0x00, 0x00, 0x00, 0x01];
84        let d = PrivateDataIndicatorDescriptor::parse(&bytes).unwrap();
85        assert_eq!(d.private_data_specifier, [0x00, 0x00, 0x00, 0x01]);
86    }
87
88    #[test]
89    fn parse_rejects_wrong_tag() {
90        let err = PrivateDataIndicatorDescriptor::parse(&[0x10, 4, 0, 0, 0, 0]).unwrap_err();
91        assert!(matches!(err, Error::InvalidDescriptor { tag: 0x10, .. }));
92    }
93
94    #[test]
95    fn parse_rejects_wrong_length() {
96        // Length says 3 but valid is 4 — buffer is large enough to read header+3
97        let bytes = [TAG, 3, 0, 0, 0, 0];
98        let err = PrivateDataIndicatorDescriptor::parse(&bytes).unwrap_err();
99        assert!(matches!(err, Error::InvalidDescriptor { .. }));
100    }
101
102    #[test]
103    fn parse_rejects_short_buffer() {
104        let err = PrivateDataIndicatorDescriptor::parse(&[TAG]).unwrap_err();
105        assert!(matches!(err, Error::BufferTooShort { .. }));
106    }
107
108    #[test]
109    fn serialize_round_trip() {
110        let d = PrivateDataIndicatorDescriptor {
111            private_data_specifier: [0xAA, 0xBB, 0xCC, 0xDD],
112        };
113        let mut buf = vec![0u8; d.serialized_len()];
114        d.serialize_into(&mut buf).unwrap();
115        let reparsed = PrivateDataIndicatorDescriptor::parse(&buf).unwrap();
116        assert_eq!(d, reparsed);
117    }
118
119    #[test]
120    fn descriptor_length_matches_payload() {
121        let d = PrivateDataIndicatorDescriptor {
122            private_data_specifier: [0x00, 0x00, 0x00, 0x01],
123        };
124        assert_eq!(d.serialized_len() - 2, 4);
125    }
126}