Skip to main content

dvb_si/descriptors/
service_move.rs

1//! Service Move Descriptor — ETSI EN 300 468 §6.2.37 (tag 0x60).
2//!
3//! Table 92 (PDF p. 102). Carried in the SDT when a service is being moved to a
4//! new location; gives the new (original_network_id, transport_stream_id,
5//! service_id) triple. Fixed 6-byte payload.
6
7use super::descriptor_body;
8use crate::error::{Error, Result};
9use dvb_common::{Parse, Serialize};
10
11/// Descriptor tag for service_move_descriptor.
12pub const TAG: u8 = 0x60;
13const HEADER_LEN: usize = 2;
14/// Fixed payload length: three 16-bit identifiers (EN 300 468 Table 92).
15const BODY_LEN: u8 = 6;
16
17/// Service Move Descriptor (tag 0x60).
18#[derive(Debug, Clone, PartialEq, Eq)]
19#[cfg_attr(feature = "serde", derive(serde::Serialize))]
20pub struct ServiceMoveDescriptor {
21    /// original_network_id of the service's new location.
22    pub new_original_network_id: u16,
23    /// transport_stream_id of the service's new location.
24    pub new_transport_stream_id: u16,
25    /// service_id of the service at its new location.
26    pub new_service_id: u16,
27}
28
29impl<'a> Parse<'a> for ServiceMoveDescriptor {
30    type Error = crate::error::Error;
31    fn parse(bytes: &'a [u8]) -> Result<Self> {
32        let body = descriptor_body(
33            bytes,
34            TAG,
35            "ServiceMoveDescriptor",
36            "unexpected tag for service_move_descriptor",
37        )?;
38        if body.len() != BODY_LEN as usize {
39            return Err(Error::InvalidDescriptor {
40                tag: TAG,
41                reason: "service_move_descriptor length must equal 6",
42            });
43        }
44        let (hdr, _) = body
45            .split_first_chunk::<6>()
46            .ok_or(Error::InvalidDescriptor {
47                tag: TAG,
48                reason: "service_move_descriptor length must equal 6",
49            })?;
50        Ok(Self {
51            new_original_network_id: u16::from_be_bytes([hdr[0], hdr[1]]),
52            new_transport_stream_id: u16::from_be_bytes([hdr[2], hdr[3]]),
53            new_service_id: u16::from_be_bytes([hdr[4], hdr[5]]),
54        })
55    }
56}
57
58impl Serialize for ServiceMoveDescriptor {
59    type Error = crate::error::Error;
60    fn serialized_len(&self) -> usize {
61        HEADER_LEN + BODY_LEN as usize
62    }
63
64    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
65        let len = self.serialized_len();
66        if buf.len() < len {
67            return Err(Error::OutputBufferTooSmall {
68                need: len,
69                have: buf.len(),
70            });
71        }
72        buf[0] = TAG;
73        buf[1] = BODY_LEN;
74        let b = HEADER_LEN;
75        buf[b..b + 2].copy_from_slice(&self.new_original_network_id.to_be_bytes());
76        buf[b + 2..b + 4].copy_from_slice(&self.new_transport_stream_id.to_be_bytes());
77        buf[b + 4..b + 6].copy_from_slice(&self.new_service_id.to_be_bytes());
78        Ok(len)
79    }
80}
81impl<'a> crate::traits::DescriptorDef<'a> for ServiceMoveDescriptor {
82    const TAG: u8 = TAG;
83    const NAME: &'static str = "SERVICE_MOVE";
84}
85
86#[cfg(test)]
87mod tests {
88    use super::*;
89
90    #[test]
91    fn parse_extracts_triple() {
92        let bytes = [TAG, 6, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03];
93        let d = ServiceMoveDescriptor::parse(&bytes).unwrap();
94        assert_eq!(d.new_original_network_id, 0x0001);
95        assert_eq!(d.new_transport_stream_id, 0x0002);
96        assert_eq!(d.new_service_id, 0x0003);
97    }
98
99    #[test]
100    fn parse_rejects_wrong_tag() {
101        let err = ServiceMoveDescriptor::parse(&[0x61, 6, 0, 0, 0, 0, 0, 0]).unwrap_err();
102        assert!(matches!(err, Error::InvalidDescriptor { tag: 0x61, .. }));
103    }
104
105    #[test]
106    fn parse_rejects_short_buffer() {
107        let err = ServiceMoveDescriptor::parse(&[TAG]).unwrap_err();
108        assert!(matches!(err, Error::BufferTooShort { .. }));
109    }
110
111    #[test]
112    fn parse_rejects_truncated_body() {
113        // length=6 but only 4 payload bytes present.
114        let err = ServiceMoveDescriptor::parse(&[TAG, 6, 0, 1, 0, 2]).unwrap_err();
115        assert!(matches!(err, Error::BufferTooShort { .. }));
116    }
117
118    #[test]
119    fn parse_rejects_wrong_length() {
120        let err = ServiceMoveDescriptor::parse(&[TAG, 5, 0, 1, 0, 2, 0]).unwrap_err();
121        assert!(matches!(err, Error::InvalidDescriptor { .. }));
122    }
123
124    #[test]
125    fn serialize_round_trip() {
126        let d = ServiceMoveDescriptor {
127            new_original_network_id: 0x1234,
128            new_transport_stream_id: 0x5678,
129            new_service_id: 0x9ABC,
130        };
131        let mut buf = vec![0u8; d.serialized_len()];
132        d.serialize_into(&mut buf).unwrap();
133        let re = ServiceMoveDescriptor::parse(&buf).unwrap();
134        assert_eq!(d, re);
135    }
136
137    #[test]
138    fn serialize_rejects_too_small_buffer() {
139        let d = ServiceMoveDescriptor {
140            new_original_network_id: 1,
141            new_transport_stream_id: 2,
142            new_service_id: 3,
143        };
144        let mut tiny = [0u8; 5];
145        let err = d.serialize_into(&mut tiny).unwrap_err();
146        assert!(matches!(err, Error::OutputBufferTooSmall { .. }));
147    }
148
149    #[test]
150    fn descriptor_length_matches_payload() {
151        let d = ServiceMoveDescriptor {
152            new_original_network_id: 0,
153            new_transport_stream_id: 0,
154            new_service_id: 0,
155        };
156        assert_eq!(d.serialized_len() - 2, 6);
157    }
158
159    #[cfg(feature = "serde")]
160    #[test]
161    fn serde_round_trip() {
162        let d = ServiceMoveDescriptor {
163            new_original_network_id: 0x0001,
164            new_transport_stream_id: 0x0002,
165            new_service_id: 0x0003,
166        };
167        let json = serde_json::to_string(&d).unwrap();
168        // Serialize-only: assert the emitted JSON re-parses (serialize-stable).
169        let _v: serde_json::Value = serde_json::from_str(&json).unwrap();
170    }
171}