use std::str::FromStr; // we still support rust that doesn't have the inherent methods #[allow(deprecated, unused_imports)] use std::ascii::AsciiExt; use regex::{Captures, Regex}; pub struct LineParser { rule_line: Regex, day_field: Regex, hm_field: Regex, hms_field: Regex, zone_line: Regex, continuation_line: Regex, link_line: Regex, empty_line: Regex, } #[derive(PartialEq, Debug, Clone)] pub enum Error { FailedYearParse(String), FailedMonthParse(String), FailedWeekdayParse(String), InvalidLineType(String), TypeColumnContainedNonHyphen(String), CouldNotParseSaving(String), InvalidDaySpec(String), InvalidTimeSpecAndType(String), NonWallClockInTimeSpec(String), NotParsedAsRuleLine, NotParsedAsZoneLine, NotParsedAsLinkLine, } impl LineParser { pub fn new() -> Self { LineParser { rule_line: Regex::new( r##"(?x) ^ Rule \s+ ( ?P \S+) \s+ ( ?P \S+) \s+ ( ?P \S+) \s+ ( ?P \S+) \s+ ( ?P \S+) \s+ ( ?P \S+) \s+ ( ?P \S+) \s+ ( ?P \S+) \s+ ( ?P \S+) \s* (\#.*)? $ "##, ) .unwrap(), day_field: Regex::new( r##"(?x) ^ ( ?P \w+ ) ( ?P [<>] = ) ( ?P \d+ ) $ "##, ) .unwrap(), hm_field: Regex::new( r##"(?x) ^ ( ?P -? ) ( ?P \d{1,2} ) : ( ?P \d{2} ) ( ?P [wsugz] )? $ "##, ) .unwrap(), hms_field: Regex::new( r##"(?x) ^ ( ?P -? ) ( ?P \d{1,2} ) : ( ?P \d{2} ) : ( ?P \d{2} ) ( ?P [wsugz] )? $ "##, ) .unwrap(), zone_line: Regex::new( r##"(?x) ^ Zone \s+ ( ?P [ A-Z a-z 0-9 / _ + - ]+ ) \s+ ( ?P \S+ ) \s+ ( ?P \S+ ) \s+ ( ?P \S+ ) \s* ( ?P \S+ )? \s* ( ?P \S+ )? \s* ( ?P \S+ )? \s* ( ?P