diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2017-11-11 02:57:40 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2017-11-11 03:04:52 +0100 |
commit | 55678c5a6fba270f6bb050377ebc9bad19873e36 (patch) | |
tree | 92f66c7d5e9679fdd254597e47f750107cfe90bf | |
parent | 8f46938f9947c3f487bd0ba6c02a19c0de672a6d (diff) |
Add TimeType::parse()
-rw-r--r-- | src/error.rs | 7 | ||||
-rw-r--r-- | src/timetype.rs | 9 |
2 files changed, 15 insertions, 1 deletions
diff --git a/src/error.rs b/src/error.rs index f9f1efc..653b368 100644 --- a/src/error.rs +++ b/src/error.rs @@ -9,7 +9,7 @@ error_chain! { } foreign_links { - NomError(::nom::IError); + NomError(::nom::simple_errors::Err); } errors { @@ -59,6 +59,11 @@ error_chain! { display("Cannot compare Month to non-Moment TimeType: {:?}", tt_rep) } + UnknownParserError { + description("Unknown parser error") + display("Unknown parser error") + } + } } diff --git a/src/timetype.rs b/src/timetype.rs index e8df4c8..8a33aa0 100644 --- a/src/timetype.rs +++ b/src/timetype.rs @@ -435,6 +435,15 @@ impl TimeType { pub fn calculate(self) -> Result<TimeType> { do_calculate(self) } + + pub fn parse(s: &str) -> Result<TimeType> { + use parser; + match parser::timetype(s.as_bytes()) { + ::nom::IResult::Done(_, o) => Ok(o.into()), + ::nom::IResult::Error(e) => Err(KE::from(e)), + ::nom::IResult::Incomplete(_) => Err(KEK::UnknownParserError.into()), + } + } } fn do_calculate(tt: TimeType) -> Result<TimeType> { |