Skip to main content

dvb_si/tables/
registry.rs

1//! Runtime table registry — open registration of client private table_ids.
2//!
3//! [`TableRegistry`] allows clients to register their own private table types.
4//! Registered custom parsers win over built-in dispatch; an unregistered
5//! table_id falls through to [`crate::tables::AnyTableSection::Unknown`].
6//!
7//! # Owned types only
8//!
9//! Registered types must be `'static` (i.e. owned — no borrowed slices).
10//! This is required because the parsed value is heap-allocated as a
11//! `Box<dyn TableObject>` whose concrete type is erased; `dyn Any`
12//! downcast demands `'static`.  If your wire layout contains borrowed bytes,
13//! copy them into a `Vec<u8>` in the struct.
14//!
15//! # Example
16//!
17//! ```rust,no_run
18//! use dvb_si::tables::{TableRegistry, AnyTableSection};
19//! use dvb_si::traits::TableDef;
20//! use dvb_common::Parse;
21//!
22//! // A registered type must be `serde::Serialize` only when the `serde`
23//! // feature is on (that is what `TableObject` requires there).
24//! #[derive(Debug)]
25//! #[cfg_attr(feature = "serde", derive(serde::Serialize))]
26//! struct MyPrivate { x: u8 }
27//!
28//! impl<'a> Parse<'a> for MyPrivate {
29//!     type Error = dvb_si::Error;
30//!     fn parse(bytes: &'a [u8]) -> dvb_si::Result<Self> {
31//!         if bytes.len() < 2 {
32//!             return Err(dvb_si::Error::BufferTooShort {
33//!                 need: 2, have: bytes.len(), what: "MyPrivate",
34//!             });
35//!         }
36//!         Ok(Self { x: bytes[1] })
37//!     }
38//! }
39//!
40//! impl<'a> TableDef<'a> for MyPrivate {
41//!     const TABLE_ID_RANGES: &'static [(u8, u8)] = &[(0x90, 0x90)];
42//!     const NAME: &'static str = "MY_PRIVATE";
43//! }
44//!
45//! let mut reg = TableRegistry::new();
46//! reg.register::<MyPrivate>();
47//!
48//! let bytes = [0x90, 0x00, 0x42u8];
49//! match AnyTableSection::parse_with(&reg, &bytes).unwrap() {
50//!     AnyTableSection::Other { table_id, ref value } => {
51//!         assert_eq!(table_id, 0x90);
52//!         assert_eq!(value.downcast_ref::<MyPrivate>().unwrap().x, 0x42);
53//!     }
54//!     other => panic!("expected Other, got {other:?}"),
55//! }
56//! ```
57
58use alloc::boxed::Box;
59use alloc::collections::BTreeMap;
60use core::any::Any;
61
62// ---------------------------------------------------------------------------
63// TableObject trait
64// ---------------------------------------------------------------------------
65
66/// Object-safe face of a runtime-registered table-section value.
67///
68/// Registered types must be owned (`'static`) because the `dyn Any` downcast
69/// path requires it.  See the [module docs][self] for details.
70///
71/// Implemented automatically via the blanket impl for any `T` satisfying the
72/// supertraits; you do not need to write this by hand.
73#[cfg(not(feature = "serde"))]
74pub trait TableObject: core::fmt::Debug + Any + Send + Sync {
75    /// Borrow as `&dyn Any` so the caller can downcast to the concrete type.
76    fn as_any(&self) -> &dyn Any;
77}
78
79/// Object-safe face of a runtime-registered table-section value.
80///
81/// Registered types must be owned (`'static`) because the `dyn Any` downcast
82/// path requires it.  See the [module docs][self] for details.
83///
84/// Implemented automatically via the blanket impl for any `T` satisfying the
85/// supertraits; you do not need to write this by hand.
86#[cfg(feature = "serde")]
87pub trait TableObject: core::fmt::Debug + Any + Send + Sync + erased_serde::Serialize {
88    /// Borrow as `&dyn Any` so the caller can downcast to the concrete type.
89    fn as_any(&self) -> &dyn Any;
90}
91
92// Blanket impl — no-serde arm.
93#[cfg(not(feature = "serde"))]
94impl<T> TableObject for T
95where
96    T: core::fmt::Debug + Any + Send + Sync,
97{
98    fn as_any(&self) -> &dyn Any {
99        self
100    }
101}
102
103// Blanket impl — serde arm.
104#[cfg(feature = "serde")]
105impl<T> TableObject for T
106where
107    T: core::fmt::Debug + Any + Send + Sync + serde::Serialize,
108{
109    fn as_any(&self) -> &dyn Any {
110        self
111    }
112}
113
114// Downcast helpers ON THE TRAIT OBJECT (not the blanket).
115//
116// The blanket `impl<T> TableObject for T` also covers `Box<dyn TableObject>`
117// itself whenever the box satisfies the bounds — it does under
118// `--no-default-features`, where the bound is just `Debug + Any + Send + Sync`.
119// So `the_box.as_any()` resolves to the *box's* impl and reports the box's
120// `TypeId`, not the inner value's — a silent downcast failure. (Under `serde`
121// the extra `serde::Serialize` bound excludes the box, which is why the footgun
122// only bites without default features.) Calling through `dyn TableObject`
123// (which `Box` derefs to) always hits the inner value, so always downcast via
124// these methods rather than `the_box.as_any()`.
125impl dyn TableObject {
126    /// Downcast a registered table section to its concrete type `T`.
127    ///
128    /// Works for `Box<dyn TableObject>` (it derefs to the trait object) under
129    /// every feature configuration.
130    #[must_use]
131    pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
132        self.as_any().downcast_ref::<T>()
133    }
134
135    /// `true` if the registered table section's concrete type is `T`.
136    #[must_use]
137    pub fn is<T: Any>(&self) -> bool {
138        self.as_any().is::<T>()
139    }
140}
141
142// ---------------------------------------------------------------------------
143// Erased serialisation helper (serde-gated)
144// ---------------------------------------------------------------------------
145
146/// `serialize_with` helper used on [`AnyTableSection::Other`]'s `value` field.
147///
148/// Delegates to [`erased_serde::serialize`] so the concrete type's
149/// `serde::Serialize` impl is invoked through the trait object.
150///
151/// The `&Box<T>` is required by serde's `serialize_with` codegen — the field
152/// type is `Box<dyn TableObject>` so serde passes `&Box<dyn TableObject>`.
153#[cfg(feature = "serde")]
154#[allow(clippy::borrowed_box)]
155pub(crate) fn serialize_erased<S: serde::Serializer>(
156    v: &Box<dyn TableObject>,
157    s: S,
158) -> Result<S::Ok, S::Error> {
159    erased_serde::serialize(&**v, s)
160}
161
162// ---------------------------------------------------------------------------
163// Internal parse closure type
164// ---------------------------------------------------------------------------
165
166/// A heap-allocated parse closure that takes a full section (header + body)
167/// and returns an owned, type-erased table-section value.
168pub(crate) type CustomParse =
169    Box<dyn for<'a> Fn(&'a [u8]) -> crate::Result<Box<dyn TableObject>> + Send + Sync>;
170
171// ---------------------------------------------------------------------------
172// TableRegistry
173// ---------------------------------------------------------------------------
174
175/// Runtime-configurable table registry.
176///
177/// By default the registry has no custom parsers.  Use
178/// [`register`][Self::register] to add one.
179///
180/// # Precedence (per table_id)
181///
182/// 1. Custom-registered parser (table_id in the [`custom`][Self::register] map) →
183///    [`crate::tables::AnyTableSection::Other`]
184/// 2. Built-in dispatch ([`crate::tables::AnyTableSection::parse`]) → typed variant
185/// 3. Unknown → [`crate::tables::AnyTableSection::Unknown`]
186#[derive(Default)]
187pub struct TableRegistry {
188    custom: BTreeMap<u8, CustomParse>,
189}
190
191impl TableRegistry {
192    /// Create an empty registry (built-in dispatch only).
193    #[must_use]
194    pub fn new() -> Self {
195        Self::default()
196    }
197
198    /// Look up a custom parser for the given `table_id`.
199    #[must_use]
200    pub(crate) fn lookup(&self, table_id: u8) -> Option<&CustomParse> {
201        self.custom.get(&table_id)
202    }
203
204    /// Register an owned custom table-section type for every table_id in its
205    /// [`TableDef::TABLE_ID_RANGES`][crate::traits::TableDef::TABLE_ID_RANGES].
206    ///
207    /// # Owned types only
208    ///
209    /// `T` must be `'static` — no borrowed slices.  The registered value is
210    /// type-erased as `Box<dyn TableObject>`; `dyn Any` downcast requires
211    /// the concrete type to be `'static`.
212    ///
213    /// # Multi-range registration
214    ///
215    /// A type may cover multiple table_id ranges (e.g.
216    /// `&[(0x90, 0x90), (0x92, 0x93)]`).  Every table_id in every range is
217    /// registered under the same parse closure.
218    ///
219    /// Registering a type whose table_id is already used by a built-in **overrides**
220    /// the built-in for that table_id.
221    ///
222    /// Re-registering the same table_id replaces the prior custom parser (last wins).
223    /// A failing custom parse surfaces the client's `Parse::Error` unwrapped —
224    /// embed identifying context (type/table_id) in your error's `what`/`reason` fields.
225    pub fn register<T>(&mut self) -> &mut Self
226    where
227        T: for<'a> crate::traits::TableDef<'a> + TableObject + 'static,
228    {
229        let ranges = <T as crate::traits::TableDef<'static>>::TABLE_ID_RANGES;
230        for &(lo, hi) in ranges {
231            for id in lo..=hi {
232                self.custom.insert(
233                    id,
234                    Box::new(|b| {
235                        Ok(Box::new(<T as dvb_common::Parse>::parse(b)?) as Box<dyn TableObject>)
236                    }),
237                );
238            }
239        }
240        self
241    }
242}
243
244#[cfg(test)]
245mod tests {
246    use super::*;
247    use crate::tables::AnyTableSection;
248    use crate::traits::TableDef;
249    use dvb_common::Parse;
250
251    // -- A trivial custom table for a private table_id -------------------------
252    const CUSTOM_TABLE_ID: u8 = 0x90;
253
254    #[derive(Debug, PartialEq)]
255    #[cfg_attr(feature = "serde", derive(serde::Serialize))]
256    struct CustomTable {
257        table_id: u8,
258        payload: Vec<u8>,
259    }
260
261    impl<'a> Parse<'a> for CustomTable {
262        type Error = crate::Error;
263        fn parse(bytes: &'a [u8]) -> crate::Result<Self> {
264            if bytes.is_empty() {
265                return Err(crate::Error::BufferTooShort {
266                    need: 1,
267                    have: 0,
268                    what: "CustomTable",
269                });
270            }
271            Ok(Self {
272                table_id: bytes[0],
273                payload: bytes[1..].to_vec(),
274            })
275        }
276    }
277
278    impl<'a> TableDef<'a> for CustomTable {
279        const TABLE_ID_RANGES: &'static [(u8, u8)] = &[(CUSTOM_TABLE_ID, CUSTOM_TABLE_ID)];
280        const NAME: &'static str = "CUSTOM_TABLE";
281    }
282
283    // -- A multi-range custom table --------------------------------------------
284    const MULTI_LO: u8 = 0x90;
285    const MULTI_MID: u8 = 0x92;
286    const MULTI_HI: u8 = 0x93;
287
288    #[derive(Debug, PartialEq)]
289    #[cfg_attr(feature = "serde", derive(serde::Serialize))]
290    struct MultiRangeTable {
291        table_id: u8,
292    }
293
294    impl<'a> Parse<'a> for MultiRangeTable {
295        type Error = crate::Error;
296        fn parse(bytes: &'a [u8]) -> crate::Result<Self> {
297            if bytes.is_empty() {
298                return Err(crate::Error::BufferTooShort {
299                    need: 1,
300                    have: 0,
301                    what: "MultiRangeTable",
302                });
303            }
304            Ok(Self { table_id: bytes[0] })
305        }
306    }
307
308    impl<'a> TableDef<'a> for MultiRangeTable {
309        const TABLE_ID_RANGES: &'static [(u8, u8)] = &[(MULTI_LO, MULTI_LO), (MULTI_MID, MULTI_HI)];
310        const NAME: &'static str = "MULTI_RANGE_TABLE";
311    }
312
313    // -- A custom table that overrides a built-in (PAT = 0x00) -----------------
314    #[derive(Debug, PartialEq)]
315    #[cfg_attr(feature = "serde", derive(serde::Serialize))]
316    struct OverridePat {
317        table_id: u8,
318    }
319
320    impl<'a> Parse<'a> for OverridePat {
321        type Error = crate::Error;
322        fn parse(bytes: &'a [u8]) -> crate::Result<Self> {
323            if bytes.is_empty() {
324                return Err(crate::Error::BufferTooShort {
325                    need: 1,
326                    have: 0,
327                    what: "OverridePat",
328                });
329            }
330            Ok(Self { table_id: bytes[0] })
331        }
332    }
333
334    impl<'a> TableDef<'a> for OverridePat {
335        const TABLE_ID_RANGES: &'static [(u8, u8)] = &[(0x00, 0x00)];
336        const NAME: &'static str = "OVERRIDE_PAT";
337    }
338
339    #[test]
340    fn custom_table_dispatches_to_other() {
341        let mut reg = TableRegistry::new();
342        reg.register::<CustomTable>();
343
344        let bytes = [CUSTOM_TABLE_ID, 0xAA, 0xBB];
345        let result = AnyTableSection::parse_with(&reg, &bytes).unwrap();
346        match result {
347            AnyTableSection::Other {
348                table_id,
349                ref value,
350            } => {
351                assert_eq!(table_id, CUSTOM_TABLE_ID);
352                let ct = value.downcast_ref::<CustomTable>().unwrap();
353                assert_eq!(ct.table_id, CUSTOM_TABLE_ID);
354                assert_eq!(ct.payload, &[0xAA, 0xBB]);
355            }
356            other => panic!("expected Other, got {other:?}"),
357        }
358    }
359
360    #[test]
361    fn multi_range_registers_all_ids() {
362        let mut reg = TableRegistry::new();
363        reg.register::<MultiRangeTable>();
364
365        assert!(reg.lookup(MULTI_LO).is_some());
366        assert!(reg.lookup(MULTI_MID).is_some());
367        assert!(reg.lookup(MULTI_MID + 1).is_some());
368        assert!(reg.lookup(MULTI_HI).is_some());
369        // 0x91 is NOT in any range
370        assert!(reg.lookup(MULTI_LO + 1).is_none());
371
372        // Each registered id dispatches to Other
373        for id in [MULTI_LO, MULTI_MID, MULTI_MID + 1, MULTI_HI] {
374            let bytes = [id, 0x00];
375            let result = AnyTableSection::parse_with(&reg, &bytes).unwrap();
376            match result {
377                AnyTableSection::Other { table_id, .. } => assert_eq!(table_id, id),
378                other => panic!("id {id:#04x}: expected Other, got {other:?}"),
379            }
380        }
381
382        // 0x91 falls to Unknown
383        let bytes = [MULTI_LO + 1, 0x00];
384        let result = AnyTableSection::parse_with(&reg, &bytes).unwrap();
385        assert!(
386            matches!(result, AnyTableSection::Unknown { .. }),
387            "expected Unknown for unregistered id 0x91, got {result:?}",
388        );
389    }
390
391    #[test]
392    fn fallback_to_builtin_for_unregistered() {
393        let reg = TableRegistry::new();
394        // Build a minimal PAT (table_id 0x00)
395        use crate::tables::pat::{PatEntry, PatSection};
396        use dvb_common::Serialize;
397        let pat = PatSection {
398            transport_stream_id: 1,
399            version_number: 0,
400            current_next_indicator: true,
401            section_number: 0,
402            last_section_number: 0,
403            entries: vec![PatEntry {
404                program_number: 1,
405                pid: 0x0100,
406            }],
407        };
408        let mut buf = vec![0u8; pat.serialized_len()];
409        pat.serialize_into(&mut buf).unwrap();
410
411        let result = AnyTableSection::parse_with(&reg, &buf).unwrap();
412        match result {
413            AnyTableSection::PatSection(p) => assert_eq!(p.entries.len(), 1),
414            other => panic!("expected PatSection, got {other:?}"),
415        }
416    }
417
418    #[test]
419    fn override_builtin_yields_other() {
420        let mut reg = TableRegistry::new();
421        reg.register::<OverridePat>();
422
423        // Minimal PAT bytes (just table_id + enough to not error in OverridePat)
424        let bytes = [0x00u8, 0x01, 0x02];
425        let result = AnyTableSection::parse_with(&reg, &bytes).unwrap();
426        match result {
427            AnyTableSection::Other {
428                table_id,
429                ref value,
430            } => {
431                assert_eq!(table_id, 0x00);
432                let op = value.downcast_ref::<OverridePat>().unwrap();
433                assert_eq!(op.table_id, 0x00);
434            }
435            other => panic!("expected Other (override), got {other:?}"),
436        }
437    }
438
439    #[test]
440    fn empty_bytes_returns_buffer_too_short() {
441        let reg = TableRegistry::new();
442        let result = AnyTableSection::parse_with(&reg, &[]);
443        assert!(matches!(
444            result,
445            Err(crate::Error::BufferTooShort {
446                need: 1,
447                have: 0,
448                ..
449            })
450        ));
451    }
452
453    #[cfg(feature = "serde")]
454    #[test]
455    fn serde_other_variant() {
456        let mut reg = TableRegistry::new();
457        reg.register::<CustomTable>();
458
459        let bytes = [CUSTOM_TABLE_ID, 0xAA, 0xBB];
460        let result = AnyTableSection::parse_with(&reg, &bytes).unwrap();
461
462        let json = serde_json::to_string(&result).unwrap();
463        assert!(json.contains("\"other\""), "unexpected JSON: {json}");
464    }
465}