Impl Parse::parse for Time

This commit is contained in:
Matthias Beyer 2016-05-28 20:19:09 +02:00
parent e25ab854ee
commit fbb576d51a
1 changed files with 21 additions and 1 deletions

View File

@ -27,7 +27,27 @@ impl Into<ChronoNaiveTime> for Time {
impl Parse for Time {
fn parse(s: &str) -> Option<Time> {
unimplemented!()
use std::str::FromStr;
use regex::Regex;
use parse::time_parse_regex;
lazy_static! {
static ref R: Regex = Regex::new(time_parse_regex()).unwrap();
}
R.captures(s)
.and_then(|capts| {
let hour = capts.name("h").and_then(|o| FromStr::from_str(o).ok());
let minute = capts.name("m").and_then(|o| FromStr::from_str(o).ok()).unwrap_or(0);
let second = capts.name("s").and_then(|o| FromStr::from_str(o).ok()).unwrap_or(0);
if hour.is_none() {
debug!("No hour");
return None;
}
Some(Time::new(hour.unwrap(), minute, second))
})
}
}