rsubs_lib/lib.rs
1//! # rsubs-lib
2//!
3//! This [crate](https://crates.io/crates/rsubs-lib) provides a simple way for parsing, modifying or converting
4//! subtitle files such as `.srt`,`.ssa`,`.ass` and `.vtt`.
5//!
6//! Example usage:
7//! ```
8//! use std::ops::Add;
9//! use std::time::Duration;
10//! use rsubs_lib::SRT;
11//!
12//! let raw_srt = r#"1
13//! 00:00:00,000 --> 00:00:02,000
14//! This is a example .srt file
15//!
16//! 2
17//! 00:00:02,000 --> 00:00:06,000
18//! The following code will delay the subtitles by one second
19//! "#;
20//!
21//! let mut srt = SRT::parse(raw_srt).unwrap();
22//! for line in &mut srt.lines {
23//! line.start.add(Duration::from_secs(1));
24//! line.end.add(Duration::from_secs(1));
25//! }
26//! println!("{}", srt)
27//! ```
28//!
29//!
30mod srt;
31mod ssa;
32pub mod util;
33mod vtt;
34
35pub use srt::*;
36pub use ssa::*;
37pub use vtt::*;
38
39macro_rules! error {
40 ($error_name:ident => $kind_name:ident { $($field:ident $(($($t:ty),*))?),*, }) => {
41 #[derive(Debug, Eq, PartialEq)]
42 pub struct $error_name {
43 line: usize,
44 kind: $kind_name
45 }
46
47 impl $error_name {
48 fn new(kind: $kind_name, line: usize) -> Self {
49 Self { line, kind }
50 }
51
52 pub fn line(&self) -> usize {
53 self.line
54 }
55
56 pub fn kind(&self) -> &$kind_name {
57 &self.kind
58 }
59 }
60
61 impl std::fmt::Display for $error_name {
62 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63 write!(f, "error at line {}: {:?}", self.line, self.kind)
64 }
65 }
66
67 impl std::error::Error for $error_name {}
68
69 #[derive(Debug, Eq, PartialEq)]
70 pub enum $kind_name {
71 $($field $(($($t),*))?),*
72 }
73 };
74}
75pub(crate) use error;