forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiBcbbd6e2Tahinli2 months ago2369 commits

lib.rs64 lines, 2.0 KiBHistoryRawDownload
1//! `oxideav-subtitle` as edith consumes it, over [`ec_subs`].
2//!
3//! Three items and their types: `ir::plain_text`, `ir::SubtitleTrack`,
4//! `srt::parse` and `webvtt::parse`. The IR is `ec_subs`'s own — the field
5//! names were chosen to match, so this is a re-export and not a conversion —
6//! and the only adapter is the error type, because the replica writes the parse
7//! functions as `fn(&[u8]) -> oxideav_core::Result<SubtitleTrack>` and takes
8//! their address.
9
10#![forbid(unsafe_code)]
11
12/// The cue IR, which is [`ec_subs::ir`] under its incumbent name.
13pub mod ir {
14 pub use ec_subs::ir::{
15 CuePosition, Segment, SourceFormat, SubtitleCue, SubtitleStyle, SubtitleTrack, TextAlign,
16 plain_text,
17 };
18}
19
20pub use ir::{SourceFormat, SubtitleTrack};
21
22/// SubRip.
23pub mod srt {
24 use oxideav_core::Result;
25
26 use crate::ir::SubtitleTrack;
27
28 /// Parse a SubRip document.
29 pub fn parse(bytes: &[u8]) -> Result<SubtitleTrack> {
30 Ok(ec_subs::srt::parse(bytes)?)
31 }
32}
33
34/// WebVTT.
35pub mod webvtt {
36 use oxideav_core::Result;
37
38 use crate::ir::SubtitleTrack;
39
40 /// Parse a WebVTT document.
41 pub fn parse(bytes: &[u8]) -> Result<SubtitleTrack> {
42 Ok(ec_subs::webvtt::parse(bytes)?)
43 }
44}
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49
50 /// The replica takes these as function pointers of one type and calls
51 /// `plain_text` on whatever comes back; that is the whole contract.
52 #[test]
53 fn the_parsers_are_interchangeable_function_pointers() {
54 type Parse = fn(&[u8]) -> oxideav_core::Result<ir::SubtitleTrack>;
55 for parse in [srt::parse as Parse, webvtt::parse as Parse] {
56 let track = parse(b"1\n00:00:01,000 --> 00:00:02,000\n<i>hi</i>\n").unwrap();
57 assert_eq!(ir::plain_text(&track.cues[0].segments), "hi");
58 assert_eq!(track.cues[0].start_us, 1_000_000);
59 }
60 // A file of the wrong kind is an error the replica can print.
61 let error = srt::parse(b"prose, not subtitles").unwrap_err();
62 assert!(!error.to_string().is_empty());
63 }
64}