dvb_si/descriptors/
scrambling.rs1use super::descriptor_body;
8use crate::error::{Error, Result};
9use dvb_common::{Parse, Serialize};
10
11pub const TAG: u8 = 0x65;
13const HEADER_LEN: usize = 2;
14const BODY_LEN: u8 = 1;
16
17#[derive(Debug, Clone, PartialEq, Eq)]
19#[cfg_attr(feature = "serde", derive(serde::Serialize))]
20pub struct ScramblingDescriptor {
21 pub scrambling_mode: u8,
23}
24
25impl<'a> Parse<'a> for ScramblingDescriptor {
26 type Error = crate::error::Error;
27 fn parse(bytes: &'a [u8]) -> Result<Self> {
28 let body = descriptor_body(
29 bytes,
30 TAG,
31 "ScramblingDescriptor",
32 "unexpected tag for scrambling_descriptor",
33 )?;
34 if body.len() != BODY_LEN as usize {
35 return Err(Error::InvalidDescriptor {
36 tag: TAG,
37 reason: "scrambling_descriptor length must equal 1",
38 });
39 }
40 Ok(Self {
41 scrambling_mode: body[0],
42 })
43 }
44}
45
46impl Serialize for ScramblingDescriptor {
47 type Error = crate::error::Error;
48 fn serialized_len(&self) -> usize {
49 HEADER_LEN + BODY_LEN as usize
50 }
51
52 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
53 let len = self.serialized_len();
54 if buf.len() < len {
55 return Err(Error::OutputBufferTooSmall {
56 need: len,
57 have: buf.len(),
58 });
59 }
60 buf[0] = TAG;
61 buf[1] = BODY_LEN;
62 buf[HEADER_LEN] = self.scrambling_mode;
63 Ok(len)
64 }
65}
66impl<'a> crate::traits::DescriptorDef<'a> for ScramblingDescriptor {
67 const TAG: u8 = TAG;
68 const NAME: &'static str = "SCRAMBLING";
69}
70
71#[cfg(test)]
72mod tests {
73 use super::*;
74
75 #[test]
76 fn parse_extracts_scrambling_mode() {
77 let bytes = [TAG, 1, 0x02];
78 let d = ScramblingDescriptor::parse(&bytes).unwrap();
79 assert_eq!(d.scrambling_mode, 0x02);
80 }
81
82 #[test]
83 fn parse_rejects_wrong_tag() {
84 let err = ScramblingDescriptor::parse(&[0x66, 1, 0x02]).unwrap_err();
85 assert!(matches!(err, Error::InvalidDescriptor { tag: 0x66, .. }));
86 }
87
88 #[test]
89 fn parse_rejects_short_buffer() {
90 let err = ScramblingDescriptor::parse(&[TAG]).unwrap_err();
91 assert!(matches!(err, Error::BufferTooShort { .. }));
92 }
93
94 #[test]
95 fn parse_rejects_truncated_body() {
96 let err = ScramblingDescriptor::parse(&[TAG, 1]).unwrap_err();
98 assert!(matches!(err, Error::BufferTooShort { .. }));
99 }
100
101 #[test]
102 fn parse_rejects_wrong_length() {
103 let err = ScramblingDescriptor::parse(&[TAG, 2, 0x02, 0x03]).unwrap_err();
104 assert!(matches!(err, Error::InvalidDescriptor { .. }));
105 }
106
107 #[test]
108 fn serialize_round_trip() {
109 let d = ScramblingDescriptor {
110 scrambling_mode: 0x10,
111 };
112 let mut buf = vec![0u8; d.serialized_len()];
113 d.serialize_into(&mut buf).unwrap();
114 let re = ScramblingDescriptor::parse(&buf).unwrap();
115 assert_eq!(d, re);
116 }
117
118 #[test]
119 fn serialize_rejects_too_small_buffer() {
120 let d = ScramblingDescriptor {
121 scrambling_mode: 0x03,
122 };
123 let mut tiny = [0u8; 1];
124 let err = d.serialize_into(&mut tiny).unwrap_err();
125 assert!(matches!(err, Error::OutputBufferTooSmall { .. }));
126 }
127
128 #[test]
129 fn descriptor_length_matches_payload() {
130 let d = ScramblingDescriptor {
131 scrambling_mode: 0x01,
132 };
133 assert_eq!(d.serialized_len() - 2, 1);
134 }
135
136 #[cfg(feature = "serde")]
137 #[test]
138 fn serde_round_trip() {
139 let d = ScramblingDescriptor {
140 scrambling_mode: 0x02,
141 };
142 let json = serde_json::to_string(&d).unwrap();
143 let _v: serde_json::Value = serde_json::from_str(&json).unwrap();
145 }
146}