dvb_si/descriptors/
adaptation_field_data.rs1use super::descriptor_body;
9use crate::error::{Error, Result};
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))]
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 let body = descriptor_body(
31 bytes,
32 TAG,
33 "AdaptationFieldDataDescriptor",
34 "unexpected tag for adaptation_field_data_descriptor",
35 )?;
36 if body.len() != BODY_LEN {
37 return Err(Error::InvalidDescriptor {
38 tag: TAG,
39 reason: "adaptation_field_data_descriptor length must be exactly 1",
40 });
41 }
42 Ok(Self {
43 adaptation_field_data_identifier: body[0],
44 })
45 }
46}
47
48impl Serialize for AdaptationFieldDataDescriptor {
49 type Error = crate::error::Error;
50 fn serialized_len(&self) -> usize {
51 HEADER_LEN + BODY_LEN
52 }
53
54 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
55 let len = self.serialized_len();
56 if buf.len() < len {
57 return Err(Error::OutputBufferTooSmall {
58 need: len,
59 have: buf.len(),
60 });
61 }
62 buf[0] = TAG;
63 buf[1] = BODY_LEN as u8;
64 buf[HEADER_LEN] = self.adaptation_field_data_identifier;
65 Ok(len)
66 }
67}
68impl<'a> crate::traits::DescriptorDef<'a> for AdaptationFieldDataDescriptor {
69 const TAG: u8 = TAG;
70 const NAME: &'static str = "ADAPTATION_FIELD_DATA";
71}
72
73#[cfg(test)]
74mod tests {
75 use super::*;
76
77 #[test]
78 fn parse_extracts_identifier() {
79 let bytes = [TAG, 1, 0x07];
80 let d = AdaptationFieldDataDescriptor::parse(&bytes).unwrap();
81 assert_eq!(d.adaptation_field_data_identifier, 0x07);
82 }
83
84 #[test]
85 fn parse_rejects_wrong_tag() {
86 assert!(matches!(
87 AdaptationFieldDataDescriptor::parse(&[0x71, 1, 0]).unwrap_err(),
88 Error::InvalidDescriptor { tag: 0x71, .. }
89 ));
90 }
91
92 #[test]
93 fn parse_rejects_wrong_length() {
94 assert!(matches!(
95 AdaptationFieldDataDescriptor::parse(&[TAG, 0]).unwrap_err(),
96 Error::InvalidDescriptor { tag: TAG, .. }
97 ));
98 }
99
100 #[test]
101 fn parse_rejects_short_body() {
102 assert!(matches!(
103 AdaptationFieldDataDescriptor::parse(&[TAG, 1]).unwrap_err(),
104 Error::BufferTooShort { .. }
105 ));
106 }
107
108 #[test]
109 fn serialize_round_trip() {
110 let d = AdaptationFieldDataDescriptor {
111 adaptation_field_data_identifier: 0x05,
112 };
113 let mut buf = vec![0u8; d.serialized_len()];
114 d.serialize_into(&mut buf).unwrap();
115 assert_eq!(buf, [TAG, 1, 0x05]);
116 assert_eq!(AdaptationFieldDataDescriptor::parse(&buf).unwrap(), d);
117 }
118
119 #[test]
120 fn serialize_rejects_too_small_buffer() {
121 let d = AdaptationFieldDataDescriptor {
122 adaptation_field_data_identifier: 0,
123 };
124 let mut buf = vec![0u8; 2];
125 assert!(matches!(
126 d.serialize_into(&mut buf).unwrap_err(),
127 Error::OutputBufferTooSmall { .. }
128 ));
129 }
130
131 #[cfg(feature = "serde")]
132 #[test]
133 fn serde_round_trip() {
134 let d = AdaptationFieldDataDescriptor {
135 adaptation_field_data_identifier: 0x05,
136 };
137 let json = serde_json::to_string(&d).unwrap();
138 let _v: serde_json::Value = serde_json::from_str(&json).unwrap();
140 }
141}