Function parse_str

Source
pub fn parse_str(
    format: SubtitleFormat,
    content: &str,
    fps: f64,
) -> Result<SubtitleFile>
Expand description

Parse text subtitles, invoking the right parser given by format.

Returns an Err(ErrorKind::TextFormatOnly) if attempted on a binary file format.

ยงMandatory format specific options

See parse_bytes.

Examples found in repository?
examples/example1.rs (line 23)
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}