srt/
lib.rs

1#[macro_use]
2extern crate nom;
3
4pub mod parser;
5use std::fmt;
6use std::io::prelude::*;
7use std::fs::File;
8
9pub use parser::ParseError;
10use parser::parse_srt_from_slice;
11
12#[derive(Debug, PartialEq)]
13pub struct Srt {
14    pub subs: Vec<SubTitle>,
15}
16
17#[derive(Debug, PartialEq)]
18pub struct Time {
19    pub hours: u8,
20    pub minutes: u8,
21    pub seconds: u8,
22    pub milliseconds: u16,
23}
24
25impl fmt::Display for Time {
26    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27        write!(f,
28               "{:02}:{:02}:{:02},{:03}",
29               self.hours,
30               self.minutes,
31               self.seconds,
32               self.milliseconds)
33    }
34}
35
36#[derive(Debug, PartialEq)]
37pub struct SubTitle {
38    pub index: u32,
39    pub start_time: Time,
40    pub end_time: Time,
41    pub text: String,
42}
43
44impl fmt::Display for SubTitle {
45    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
46        write!(f,
47               "{}\n{} --> {}\n{}\n",
48               self.index,
49               self.start_time,
50               self.end_time,
51               self.text)
52    }
53}
54
55pub fn parse_srt_from_file(filename: &str) -> Result<Srt, ParseError> {
56    let mut f = try!(File::open(filename));
57    let mut buffer = vec![];
58    try!(f.read_to_end(&mut buffer));
59
60    parse_srt_from_slice(&buffer)
61}
62
63#[cfg(test)]
64mod tests {
65    use super::*;
66    use nom::IResult::*;
67    use nom::{Needed, ErrorKind};
68    use std::str::FromStr;
69    use std::str;
70
71    #[test]
72    fn test_parse_srt() {
73        let srt = parse_srt_from_file("assets/test.srt").unwrap();
74
75        assert_eq!(srt,
76                   Srt {
77                       subs: vec![SubTitle {
78                                      index: 1,
79                                      start_time: Time {
80                                          hours: 0,
81                                          minutes: 0,
82                                          seconds: 1,
83                                          milliseconds: 123,
84                                      },
85                                      end_time: Time {
86                                          hours: 0,
87                                          minutes: 0,
88                                          seconds: 3,
89                                          milliseconds: 456,
90                                      },
91                                      text: String::from_str("First line").unwrap(),
92                                  },
93                                  SubTitle {
94                                      index: 2,
95                                      start_time: Time {
96                                          hours: 0,
97                                          minutes: 1,
98                                          seconds: 5,
99                                          milliseconds: 0,
100                                      },
101                                      end_time: Time {
102                                          hours: 0,
103                                          minutes: 1,
104                                          seconds: 6,
105                                          milliseconds: 010,
106                                      },
107                                      text: String::from_str("Second line,\nand third line.")
108                                          .unwrap(),
109                                  },
110                                  SubTitle {
111                                      index: 3,
112                                      start_time: Time {
113                                          hours: 1,
114                                          minutes: 42,
115                                          seconds: 5,
116                                          milliseconds: 123,
117                                      },
118                                      end_time: Time {
119                                          hours: 1,
120                                          minutes: 42,
121                                          seconds: 6,
122                                          milliseconds: 456,
123                                      },
124                                      text: String::from_str("This is the end!").unwrap(),
125                                  }],
126                   });
127    }
128}