example1/
example1.rs

1use std::path::Path;
2use std::path::PathBuf;
3use subparse::timetypes::TimeDelta;
4use subparse::SubtitleEntry;
5use subparse::{get_subtitle_format, parse_str};
6
7/// This function reads the content of a file to a `String`.
8fn read_file(path: &Path) -> String {
9    use std::io::Read;
10    let mut file = std::fs::File::open(path).unwrap();
11    let mut s = String::new();
12    file.read_to_string(&mut s).unwrap();
13    s
14}
15
16fn main() {
17    // your setup goes here
18    let path = PathBuf::from("path/your_example_file.ssa");
19    let file_content: String = read_file(&path); // your own load routine
20
21    // parse the file
22    let format = get_subtitle_format(path.extension(), file_content.as_bytes()).expect("unknown format");
23    let mut subtitle_file = parse_str(format, &file_content, 25.0).expect("parser error");
24    let mut subtitle_entries: Vec<SubtitleEntry> = subtitle_file.get_subtitle_entries().expect("unexpected error");
25
26    // shift all subtitle entries by 1 minute and append "subparse" to each subtitle line
27    for subtitle_entry in &mut subtitle_entries {
28        subtitle_entry.timespan += TimeDelta::from_mins(1);
29
30        // image based subtitles like .idx (VobSub) don't have text, so
31        // a text is optional
32        if let Some(ref mut line_ref) = subtitle_entry.line {
33            line_ref.push_str("subparse");
34        }
35    }
36
37    // update the entries in the subtitle file
38    subtitle_file.update_subtitle_entries(&subtitle_entries).expect("unexpected error");
39
40    // print the corrected file to stdout
41    let data: Vec<u8> = subtitle_file.to_data().expect("unexpected errror");
42    let data_string = String::from_utf8(data).expect("UTF-8 conversion error");
43    println!("{}", data_string);
44}