dvb_si/descriptors/
private_data_indicator.rs1use super::descriptor_body;
7pub use super::private_data_specifier::private_data_specifier_name;
8use crate::error::{Error, Result};
9use dvb_common::{Parse, Serialize};
10
11pub const TAG: u8 = 0x0F;
13const HEADER_LEN: usize = 2;
14const BODY_LEN: u8 = 4;
15
16#[derive(Debug, Clone, PartialEq, Eq)]
22#[cfg_attr(feature = "serde", derive(serde::Serialize))]
23pub struct PrivateDataIndicatorDescriptor {
24 pub private_data_specifier: u32,
26}
27
28impl<'a> Parse<'a> for PrivateDataIndicatorDescriptor {
29 type Error = crate::error::Error;
30
31 fn parse(bytes: &'a [u8]) -> Result<Self> {
32 let body = descriptor_body(
33 bytes,
34 TAG,
35 "PrivateDataIndicatorDescriptor",
36 "unexpected tag for private_data_indicator_descriptor",
37 )?;
38 if body.len() != BODY_LEN as usize {
39 return Err(Error::InvalidDescriptor {
40 tag: TAG,
41 reason: "private_data_indicator_descriptor length must equal 4",
42 });
43 }
44 let private_data_specifier = u32::from_be_bytes([body[0], body[1], body[2], body[3]]);
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.to_be_bytes());
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, 0x0000_0001);
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];
97 let err = PrivateDataIndicatorDescriptor::parse(&bytes).unwrap_err();
98 assert!(matches!(err, Error::InvalidDescriptor { .. }));
99 }
100
101 #[test]
102 fn parse_rejects_short_buffer() {
103 let err = PrivateDataIndicatorDescriptor::parse(&[TAG]).unwrap_err();
104 assert!(matches!(err, Error::BufferTooShort { .. }));
105 }
106
107 #[test]
108 fn serialize_round_trip() {
109 let d = PrivateDataIndicatorDescriptor {
110 private_data_specifier: 0xAABB_CCDD,
111 };
112 let mut buf = vec![0u8; d.serialized_len()];
113 d.serialize_into(&mut buf).unwrap();
114 let reparsed = PrivateDataIndicatorDescriptor::parse(&buf).unwrap();
115 assert_eq!(d, reparsed);
116 }
117
118 #[test]
119 fn descriptor_length_matches_payload() {
120 let d = PrivateDataIndicatorDescriptor {
121 private_data_specifier: 0x0000_0001,
122 };
123 assert_eq!(d.serialized_len() - 2, 4);
124 }
125}