Skip to main content

moq_mux/catalog/
format.rs

1//! Filename-style format extensions for broadcast names.
2//!
3//! Broadcast names use a filename-style suffix to advertise their catalog format,
4//! e.g. `demo/bbb.hang` or `demo/bbb.msf`. Consumers parse the suffix to pick a
5//! catalog track without explicit configuration; publishers should include the
6//! suffix in the name they publish so consumers can detect it.
7
8/// The catalog format advertised by a broadcast name suffix.
9#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
10#[non_exhaustive]
11pub enum CatalogFormat {
12	/// `hang` JSON catalog (track `catalog.json`).
13	#[default]
14	Hang,
15	/// MSF catalog (track `catalog`).
16	Msf,
17}
18
19impl CatalogFormat {
20	/// The fallback used when a broadcast name has no recognized extension.
21	///
22	/// Matches `<Self as Default>::default()`.
23	pub const DEFAULT: Self = Self::Hang;
24
25	/// The filename-style suffix (including leading dot) for this format.
26	pub fn extension(self) -> &'static str {
27		match self {
28			Self::Hang => ".hang",
29			Self::Msf => ".msf",
30		}
31	}
32
33	/// Detect the catalog format from a broadcast name suffix.
34	///
35	/// Returns `None` if the name has no recognized extension.
36	pub fn detect(name: &str) -> Option<Self> {
37		if name.ends_with(Self::Hang.extension()) {
38			Some(Self::Hang)
39		} else if name.ends_with(Self::Msf.extension()) {
40			Some(Self::Msf)
41		} else {
42			None
43		}
44	}
45}
46
47#[cfg(test)]
48mod test {
49	use super::*;
50
51	#[test]
52	fn detect_hang() {
53		assert_eq!(CatalogFormat::detect("demo/bbb.hang"), Some(CatalogFormat::Hang));
54		assert_eq!(CatalogFormat::detect("bbb.hang"), Some(CatalogFormat::Hang));
55	}
56
57	#[test]
58	fn detect_msf() {
59		assert_eq!(CatalogFormat::detect("demo/bbb.msf"), Some(CatalogFormat::Msf));
60	}
61
62	#[test]
63	fn detect_none() {
64		assert_eq!(CatalogFormat::detect("demo/bbb"), None);
65		assert_eq!(CatalogFormat::detect(""), None);
66		assert_eq!(CatalogFormat::detect("demo/foo.v2"), None);
67	}
68}