1#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
14#[cfg_attr(feature = "serde", derive(serde::Serialize))]
15#[cfg_attr(feature = "serde", serde(transparent))]
16pub struct Pid(u16);
17
18impl Pid {
19 pub const MASK: u16 = 0x1FFF;
21
22 #[must_use]
35 pub const fn from_const(value: u16) -> Self {
36 assert!(
37 value <= Self::MASK,
38 "PID constant exceeds the 13-bit range 0x0000..=0x1FFF"
39 );
40 Self(value)
41 }
42
43 #[must_use]
57 pub const fn from_wire_masked(value: u16) -> Self {
58 Self(value & Self::MASK)
59 }
60
61 pub const fn try_new(value: u16) -> Result<Self, PidOutOfRange> {
68 if value > Self::MASK {
69 Err(PidOutOfRange { value })
70 } else {
71 Ok(Self(value))
72 }
73 }
74
75 #[must_use]
77 pub const fn value(self) -> u16 {
78 self.0
79 }
80}
81
82#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
84#[error("PID 0x{value:04X} exceeds the 13-bit range 0x0000..=0x1FFF")]
85#[non_exhaustive]
86pub struct PidOutOfRange {
87 pub value: u16,
89}
90
91impl TryFrom<u16> for Pid {
92 type Error = PidOutOfRange;
93 fn try_from(value: u16) -> Result<Self, Self::Error> {
94 Self::try_new(value)
95 }
96}
97
98#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
100#[non_exhaustive]
101pub enum ParsePidError {
102 #[error("PID is not a decimal or 0x-prefixed hexadecimal number: {0}")]
104 Number(#[from] core::num::ParseIntError),
105 #[error(transparent)]
107 OutOfRange(#[from] PidOutOfRange),
108}
109
110const HEX_RADIX: u32 = 16;
112
113impl core::str::FromStr for Pid {
114 type Err = ParsePidError;
115
116 fn from_str(s: &str) -> Result<Self, Self::Err> {
119 let value = match s.strip_prefix("0x").or_else(|| s.strip_prefix("0X")) {
120 Some(hex) => u16::from_str_radix(hex, HEX_RADIX)?,
121 None => s.parse::<u16>()?,
122 };
123 Ok(Self::try_new(value)?)
124 }
125}
126
127impl From<Pid> for u16 {
128 fn from(pid: Pid) -> Self {
129 pid.0
130 }
131}
132
133impl core::fmt::Debug for Pid {
134 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
135 write!(f, "Pid(0x{:04X})", self.0)
136 }
137}
138
139impl core::fmt::UpperHex for Pid {
140 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
141 core::fmt::UpperHex::fmt(&self.0, f)
142 }
143}
144
145impl core::fmt::LowerHex for Pid {
146 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
147 core::fmt::LowerHex::fmt(&self.0, f)
148 }
149}
150
151impl core::fmt::Display for Pid {
152 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
153 write!(f, "0x{:04X}", self.0)
154 }
155}
156
157pub mod well_known {
163 use super::Pid;
164
165 pub const PAT: Pid = Pid::from_const(0x0000);
167 pub const CAT: Pid = Pid::from_const(0x0001);
169 pub const TSDT: Pid = Pid::from_const(0x0002);
171 pub const IPMP_CIT: Pid = Pid::from_const(0x0003);
173 pub const NIT: Pid = Pid::from_const(0x0010);
175 pub const SDT_BAT: Pid = Pid::from_const(0x0011);
177 pub const EIT: Pid = Pid::from_const(0x0012);
179 pub const RST: Pid = Pid::from_const(0x0013);
181 pub const TDT_TOT: Pid = Pid::from_const(0x0014);
183 pub const NETWORK_SYNC: Pid = Pid::from_const(0x0015);
185 pub const RNT: Pid = Pid::from_const(0x0016);
187 pub const SAT: Pid = Pid::from_const(0x001B);
189 pub const INBAND_SIGNALLING: Pid = Pid::from_const(0x001C);
192 pub const MEASUREMENT: Pid = Pid::from_const(0x001D);
194 pub const DIT: Pid = Pid::from_const(0x001E);
196 pub const SIT: Pid = Pid::from_const(0x001F);
198
199 pub const ATSC_PSIP: Pid = Pid::from_const(0x1FFB);
201
202 pub const NULL: Pid = Pid::from_const(0x1FFF);
204}
205
206#[cfg(test)]
207mod tests {
208 #[test]
209 fn from_str_accepts_decimal_and_hex_within_13_bits() {
210 assert_eq!("256".parse::<Pid>(), Ok(Pid::from_const(0x100)));
211 assert_eq!("0x100".parse::<Pid>(), Ok(Pid::from_const(0x100)));
212 assert_eq!("0X1FFF".parse::<Pid>(), Ok(Pid::from_const(0x1FFF)));
213 assert!(matches!(
214 "8192".parse::<Pid>(),
215 Err(ParsePidError::OutOfRange(_))
216 ));
217 assert!(matches!(
218 "0x2000".parse::<Pid>(),
219 Err(ParsePidError::OutOfRange(_))
220 ));
221 assert!(matches!(
222 "pat".parse::<Pid>(),
223 Err(ParsePidError::Number(_))
224 ));
225 assert!(matches!("".parse::<Pid>(), Err(ParsePidError::Number(_))));
226 }
227
228 use super::*;
229
230 #[test]
231 fn from_const_is_the_compile_time_constructor() {
232 const PMT: Pid = Pid::from_const(0x0100);
233 assert_eq!(PMT.value(), 0x0100);
234 assert_eq!(Pid::from_const(0).value(), 0);
235 assert_eq!(Pid::from_const(Pid::MASK).value(), 0x1FFF);
236 }
237
238 #[test]
239 fn try_new_and_from_wire_masked_differ_only_in_policy() {
240 assert_eq!(Pid::try_new(0x0000), Ok(well_known::PAT));
241 assert_eq!(Pid::try_new(Pid::MASK), Ok(well_known::NULL));
242 assert!(Pid::try_new(0x2000).is_err());
243 assert_eq!(Pid::from_wire_masked(0xE100).value(), 0x0100);
244 }
245
246 #[test]
247 fn inband_signalling_sits_between_sat_and_measurement() {
248 assert_eq!(well_known::INBAND_SIGNALLING.value(), 0x001C);
249 assert_eq!(
250 well_known::INBAND_SIGNALLING.value(),
251 well_known::SAT.value() + 1
252 );
253 assert_eq!(
254 well_known::MEASUREMENT.value(),
255 well_known::INBAND_SIGNALLING.value() + 1
256 );
257 }
258}