-
Notifications
You must be signed in to change notification settings - Fork 1
/
time_specifier.rs
45 lines (38 loc) · 1.35 KB
/
time_specifier.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//! Provides parsers for assignment operations.
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::combinator::map;
use crate::parsers::{ParseResult, Span};
use crate::types::time_specifier::names;
use crate::types::TimeSpecifier;
/// Parses an assignment operation, i.e. `increase | decrease`.
///
/// ## Example
/// ```
/// # use pddl::parsers::{parse_time_specifier, preamble::*};
/// # use pddl::{TimeSpecifier};
/// assert!(parse_time_specifier("start").is_value(TimeSpecifier::Start));
/// assert!(parse_time_specifier("end").is_value(TimeSpecifier::End));
///```
pub fn parse_time_specifier<'a, T: Into<Span<'a>>>(input: T) -> ParseResult<'a, TimeSpecifier> {
map(alt((tag(names::START), tag(names::END))), |x: Span| {
TimeSpecifier::try_from(*x.fragment()).expect("unhandled variant")
})(input.into())
}
impl crate::parsers::Parser for TimeSpecifier {
type Item = TimeSpecifier;
/// See [`parse_time_specifier`].
fn parse<'a, S: Into<Span<'a>>>(input: S) -> ParseResult<'a, Self::Item> {
parse_time_specifier(input)
}
}
#[cfg(test)]
mod tests {
use crate::parsers::UnwrapValue;
use crate::{Parser, TimeSpecifier};
#[test]
fn test_parse() {
assert!(TimeSpecifier::parse("start").is_value(TimeSpecifier::Start));
assert!(TimeSpecifier::parse("end").is_value(TimeSpecifier::End));
}
}