summaryrefslogtreecommitdiffstats
path: root/libimagtimeui
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2016-05-28 20:19:09 +0200
committerMatthias Beyer <mail@beyermatthias.de>2016-05-28 20:53:30 +0200
commitfbb576d51a6a0dc71de45fce17fdd0409400ca54 (patch)
treea909900b0147a305fb25acceab421e0c69872738 /libimagtimeui
parente25ab854ee541c785e37730f0ba300022bb87059 (diff)
Impl Parse::parse for Time
Diffstat (limited to 'libimagtimeui')
-rw-r--r--libimagtimeui/src/time.rs22
1 files changed, 21 insertions, 1 deletions
diff --git a/libimagtimeui/src/time.rs b/libimagtimeui/src/time.rs
index 7da49971..be12dfa0 100644
--- a/libimagtimeui/src/time.rs
+++ b/libimagtimeui/src/time.rs
@@ -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))
+ })
}
}