-
Notifications
You must be signed in to change notification settings - Fork 1
/
duration_constraint.rs
138 lines (126 loc) · 4.36 KB
/
duration_constraint.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//! Provides parsers for duration constraints.
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::combinator::map;
use crate::parsers::{parse_simple_duration_constraint, ParseResult, Span};
use crate::parsers::{prefix_expr, space_separated_list1};
use crate::types::DurationConstraint;
/// Parses a duration constraint.
///
/// ## Example
/// ```
/// # use pddl::parsers::{parse_duration_constraint, preamble::*};
/// # use pddl::{DOp, DurationConstraint, DurationValue, FunctionType, SimpleDurationConstraint, TimeSpecifier};
/// let input = "()";
/// assert!(parse_duration_constraint(input).is_value(None));
///
/// let input = "(= ?duration 5)";
/// assert!(parse_duration_constraint(input).is_value(
/// Some(DurationConstraint::new(
/// SimpleDurationConstraint::Op(
/// DOp::Equal,
/// DurationValue::Number(5.into())
/// )
/// ))
/// ));
///
/// let input = "(at end (<= ?duration 1.23))";
/// assert!(parse_duration_constraint(input).is_value(
/// Some(DurationConstraint::new(
/// SimpleDurationConstraint::new_at(
/// TimeSpecifier::End,
/// SimpleDurationConstraint::Op(
/// DOp::LessThanOrEqual,
/// DurationValue::Number(1.23.into())
/// )
/// )
/// ))
/// ));
///
/// let input = "(and (at end (<= ?duration 1.23)) (>= ?duration 1.0))";
/// assert!(parse_duration_constraint(input).is_value(
/// Some(DurationConstraint::new_all([
/// SimpleDurationConstraint::new_at(
/// TimeSpecifier::End,
/// SimpleDurationConstraint::Op(
/// DOp::LessThanOrEqual,
/// DurationValue::Number(1.23.into())
/// )
/// ),
/// SimpleDurationConstraint::new_op(
/// DOp::GreaterOrEqual,
/// DurationValue::Number(1.0.into())
/// )
/// ]))
/// ));
///```
pub fn parse_duration_constraint<'a, T: Into<Span<'a>>>(
input: T,
) -> ParseResult<'a, Option<DurationConstraint>> {
let none = map(tag("()"), |_| None);
let simple = map(parse_simple_duration_constraint, |c| {
Some(DurationConstraint::from(c))
});
// : duration-inequalities
let and = map(
prefix_expr(
"and",
space_separated_list1(parse_simple_duration_constraint),
),
|cs| Some(DurationConstraint::from_iter(cs)),
);
alt((none, simple, and))(input.into())
}
impl crate::parsers::Parser for DurationConstraint {
type Item = Option<DurationConstraint>;
/// See [`parse_duration_constraint`].
fn parse<'a, S: Into<Span<'a>>>(input: S) -> ParseResult<'a, Self::Item> {
parse_duration_constraint(input)
}
}
#[cfg(test)]
mod tests {
use crate::parsers::UnwrapValue;
use crate::{
DOp, DurationConstraint, DurationValue, Parser, SimpleDurationConstraint, TimeSpecifier,
};
#[test]
fn test_parse() {
let input = "()";
assert!(DurationConstraint::parse(input).is_value(None));
let input = "(= ?duration 5)";
assert!(
DurationConstraint::parse(input).is_value(Some(DurationConstraint::new(
SimpleDurationConstraint::Op(DOp::Equal, DurationValue::Number(5.into()))
)))
);
let input = "(at end (<= ?duration 1.23))";
assert!(
DurationConstraint::parse(input).is_value(Some(DurationConstraint::new(
SimpleDurationConstraint::new_at(
TimeSpecifier::End,
SimpleDurationConstraint::Op(
DOp::LessThanOrEqual,
DurationValue::Number(1.23.into())
)
)
)))
);
let input = "(and (at end (<= ?duration 1.23)) (>= ?duration 1.0))";
assert!(
DurationConstraint::parse(input).is_value(Some(DurationConstraint::new_all([
SimpleDurationConstraint::new_at(
TimeSpecifier::End,
SimpleDurationConstraint::Op(
DOp::LessThanOrEqual,
DurationValue::Number(1.23.into())
)
),
SimpleDurationConstraint::new_op(
DOp::GreaterOrEqual,
DurationValue::Number(1.0.into())
)
])))
);
}
}