Skip to main content

pddl_core/parsers/expressions/
f_comp.rs

1//! Provides parsers for fluent comparisons (`<f-comp>`).
2
3use crate::parsers::error::R;
4use crate::parsers::sexpr::Node;
5use crate::parsers::{parens, parse_binary_comp, parse_f_exp};
6use crate::types::FluentComparison;
7
8/// Parses a fluent comparison (`<f-comp>`), i.e. `(<binary-comp> <f-exp> <f-exp>)`.
9pub(crate) fn parse_f_comp(node: &Node) -> R<FluentComparison> {
10    parens(node, |args| {
11        let comparison = parse_binary_comp(args.next()?)?;
12        let lhs = parse_f_exp(args.next()?)?;
13        let rhs = parse_f_exp(args.next()?)?;
14        Ok(FluentComparison::from((comparison, lhs, rhs)))
15    })
16}
17
18crate::parsers::impl_parser!(FluentComparison => parse_f_comp);
19
20#[cfg(test)]
21mod tests {
22    use crate::parsers::UnwrapValue;
23    use assert2::check;
24
25    use crate::*;
26
27    #[test]
28    fn test_parse() {
29        check!(
30            FluentComparison::from_str("(= (+ 1.23 2.34) (+ 1.23 2.34))").is_value(
31                FluentComparison::new(
32                    BinaryComparison::Equal,
33                    FluentExpression::new_binary_op(
34                        BinaryOp::Addition,
35                        FluentExpression::new_number(1.23),
36                        FluentExpression::new_number(2.34),
37                    ),
38                    FluentExpression::new_binary_op(
39                        BinaryOp::Addition,
40                        FluentExpression::new_number(1.23),
41                        FluentExpression::new_number(2.34),
42                    )
43                )
44            )
45        );
46    }
47
48    #[test]
49    fn doc_example_0() {
50        check!(
51            FluentComparison::from_str("(= (+ 1.23 2.34) (+ 1.23 2.34))").is_value(
52                FluentComparison::new(
53                    BinaryComparison::Equal,
54                    FluentExpression::new_binary_op(
55                        BinaryOp::Addition,
56                        FluentExpression::new_number(1.23),
57                        FluentExpression::new_number(2.34),
58                    ),
59                    FluentExpression::new_binary_op(
60                        BinaryOp::Addition,
61                        FluentExpression::new_number(1.23),
62                        FluentExpression::new_number(2.34),
63                    )
64                )
65            )
66        );
67    }
68}