hang/catalog/text/format.rs
1use crate::Error;
2
3use derive_more::Display;
4use std::str::FromStr;
5
6/// The serialization format of a text track's frames.
7///
8/// The frame timestamp always carries the cue's start time (so the media-agnostic relay never
9/// parses the payload); the payload carries the cue itself in this format.
10#[derive(Debug, Clone, PartialEq, Eq, Display, Default)]
11#[non_exhaustive]
12pub enum TextFormat {
13 /// [WebVTT]. Each frame's payload is a self-contained `WEBVTT` segment whose cues carry absolute
14 /// timing, so a consumer can render it without prior context.
15 ///
16 /// [WebVTT]: https://www.w3.org/TR/webvtt1/
17 #[display("vtt")]
18 #[default]
19 Vtt,
20
21 /// TTML / IMSC1 ([W3C IMSC]). Each frame's payload is one complete TTML document with absolute
22 /// timing, clipped to the frame: its `body` begins at the frame timestamp.
23 ///
24 /// [W3C IMSC]: https://www.w3.org/TR/ttml-imsc1.1/
25 #[display("ttml")]
26 Ttml,
27
28 /// Raw UTF-8 text with no embedded timing or styling. Each cue is shown from its frame
29 /// timestamp until the next cue (an empty payload clears the caption), i.e. roll-up captions.
30 #[display("utf8")]
31 Utf8,
32
33 /// An unknown or unsupported format string, preserved verbatim so a newer format survives an
34 /// older consumer.
35 #[display("{_0}")]
36 Unknown(String),
37}
38
39impl FromStr for TextFormat {
40 type Err = Error;
41
42 fn from_str(s: &str) -> Result<Self, Self::Err> {
43 Ok(match s {
44 "vtt" => Self::Vtt,
45 "ttml" => Self::Ttml,
46 "utf8" => Self::Utf8,
47 _ => Self::Unknown(s.to_string()),
48 })
49 }
50}
51
52/// The accessibility role of a text track, mirroring the roles registered by
53/// [draft-ietf-moq-msf](https://datatracker.ietf.org/doc/draft-ietf-moq-msf/).
54#[derive(Debug, Clone, PartialEq, Eq, Display, Default)]
55#[non_exhaustive]
56pub enum TextRole {
57 /// A transcription of the spoken dialogue, same-language or translated (the common subtitle).
58 #[display("subtitle")]
59 #[default]
60 Subtitle,
61
62 /// A textual representation of all audio, including non-speech sounds, for viewers who cannot
63 /// hear it (subtitles for the deaf and hard-of-hearing).
64 #[display("caption")]
65 Caption,
66
67 /// An unknown or unsupported role string, preserved verbatim so a newer role survives an older
68 /// consumer. The MSF registry it borrows from is expected to grow, and a rendition we can't
69 /// classify is still worth listing in a picker.
70 #[display("{_0}")]
71 Unknown(String),
72}
73
74impl FromStr for TextRole {
75 type Err = Error;
76
77 fn from_str(s: &str) -> Result<Self, Self::Err> {
78 Ok(match s {
79 "subtitle" => Self::Subtitle,
80 "caption" => Self::Caption,
81 _ => Self::Unknown(s.to_string()),
82 })
83 }
84}
85
86#[cfg(test)]
87mod tests {
88 use super::*;
89
90 #[test]
91 fn format_unknown_preserved() {
92 let parsed = TextFormat::from_str("cea708").unwrap();
93 assert_eq!(parsed, TextFormat::Unknown("cea708".to_string()));
94 assert_eq!(parsed.to_string(), "cea708");
95 }
96
97 #[test]
98 fn role_unknown_preserved() {
99 let parsed = TextRole::from_str("commentary").unwrap();
100 assert_eq!(parsed, TextRole::Unknown("commentary".to_string()));
101 assert_eq!(parsed.to_string(), "commentary");
102 }
103}