dvb_si/descriptors/
adaptation_field_data.rs1use crate::error::{Error, Result};
9use crate::traits::Descriptor;
10use dvb_common::{Parse, Serialize};
11
12pub const TAG: u8 = 0x70;
14pub const HEADER_LEN: usize = 2;
16pub const BODY_LEN: usize = 1;
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
22pub struct AdaptationFieldDataDescriptor {
23 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
92#[cfg(test)]
93mod tests {
94 use super::*;
95
96 #[test]
97 fn parse_extracts_identifier() {
98 let bytes = [TAG, 1, 0x07];
99 let d = AdaptationFieldDataDescriptor::parse(&bytes).unwrap();
100 assert_eq!(d.adaptation_field_data_identifier, 0x07);
101 }
102
103 #[test]
104 fn parse_rejects_wrong_tag() {
105 assert!(matches!(
106 AdaptationFieldDataDescriptor::parse(&[0x71, 1, 0]).unwrap_err(),
107 Error::InvalidDescriptor { tag: 0x71, .. }
108 ));
109 }
110
111 #[test]
112 fn parse_rejects_wrong_length() {
113 assert!(matches!(
114 AdaptationFieldDataDescriptor::parse(&[TAG, 0]).unwrap_err(),
115 Error::InvalidDescriptor { tag: TAG, .. }
116 ));
117 }
118
119 #[test]
120 fn parse_rejects_short_body() {
121 assert!(matches!(
122 AdaptationFieldDataDescriptor::parse(&[TAG, 1]).unwrap_err(),
123 Error::BufferTooShort { .. }
124 ));
125 }
126
127 #[test]
128 fn serialize_round_trip() {
129 let d = AdaptationFieldDataDescriptor {
130 adaptation_field_data_identifier: 0x05,
131 };
132 let mut buf = vec![0u8; d.serialized_len()];
133 d.serialize_into(&mut buf).unwrap();
134 assert_eq!(buf, [TAG, 1, 0x05]);
135 assert_eq!(AdaptationFieldDataDescriptor::parse(&buf).unwrap(), d);
136 }
137
138 #[test]
139 fn serialize_rejects_too_small_buffer() {
140 let d = AdaptationFieldDataDescriptor {
141 adaptation_field_data_identifier: 0,
142 };
143 let mut buf = vec![0u8; 2];
144 assert!(matches!(
145 d.serialize_into(&mut buf).unwrap_err(),
146 Error::OutputBufferTooSmall { .. }
147 ));
148 }
149
150 #[cfg(feature = "serde")]
151 #[test]
152 fn serde_round_trip() {
153 let d = AdaptationFieldDataDescriptor {
154 adaptation_field_data_identifier: 0x05,
155 };
156 let json = serde_json::to_string(&d).unwrap();
157 let back: AdaptationFieldDataDescriptor = serde_json::from_str(&json).unwrap();
158 assert_eq!(back, d);
159 }
160}