substudy 0.5.2

Language-learning tools for working with parallel, bilingual subtitles and media files.
Documentation
// Compiled automatically using `build.rs` script and `peg`.

use std::str::FromStr;

use srt::{Subtitle, SubtitleFile};
use time::Period;

#[pub]
subtitle_file -> SubtitleFile
    = blank_lines? result:subtitles blank_lines? {
        SubtitleFile { subtitles: result }
    }

subtitles -> Vec<Subtitle>
    = subs:subtitle ** blank_lines { subs }

subtitle -> Subtitle
    = index:digits newline p:time_period newline l:lines {
        Subtitle { index: index, period: p, lines: l }
    }

time_period -> Period
    = begin:time " --> " end:time {?
        let mut end = end;
        if begin == end {
            // If subtitle has zero length, fix it. These are generated by
            // the Aeneas audio/text alignment tool, which is otherwise
            // excellent for use with audiobooks.
            end += 0.001;
        }
        match Period::new(begin, end) {
          Ok(p) => Ok(p),
          Err(_) => Err("invalid time period"),
        }
    }

time -> f32
    = hh:digits ":" mm:digits ":" ss:comma_float {
        (hh as f32)*3600.0 + (mm as f32)*60.0 + ss
    }

lines -> Vec<String>
    = lines:line ** newline { lines }

line -> String
    = text:$([^\r\n]+) { text.to_string() }

digits -> usize
    = digits:$([0-9]+) { FromStr::from_str(digits).unwrap() }

comma_float -> f32
    = float_str:$([0-9]+ "," [0-9]+) {
        let fixed: String = float_str.replace(",", ".");
        FromStr::from_str(&fixed).unwrap()
    }

newline
    = "\r"? "\n"

blank_lines
    = newline+