dvb_si/descriptors/
private_data_indicator.rs1use super::descriptor_body;
7use crate::error::{Error, Result};
8use dvb_common::{Parse, Serialize};
9
10pub const TAG: u8 = 0x0F;
12const HEADER_LEN: usize = 2;
13const BODY_LEN: u8 = 4;
14
15#[derive(Debug, Clone, PartialEq, Eq)]
21#[cfg_attr(feature = "serde", derive(serde::Serialize))]
22pub struct PrivateDataIndicatorDescriptor {
23 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 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}