Skip to main content

pddl_core/parsers/structure/
functions_def.rs

1//! Provides parsers for function definitions (`<functions-def>`).
2
3use crate::parsers::error::R;
4use crate::parsers::sexpr::Node;
5use crate::parsers::{function_typed_list, parse_atomic_function_skeleton, prefix};
6use crate::types::Functions;
7
8/// Parses a function definition (`<functions-def>`), i.e. `(:functions <function typed list (atomic function skeleton)>)`.
9pub(crate) fn parse_functions_def(node: &Node) -> R<Functions> {
10    prefix(node, ":functions", |args| {
11        function_typed_list(args, parse_atomic_function_skeleton).map(Functions::new)
12    })
13}
14
15crate::parsers::impl_parser!(Functions => parse_functions_def);
16
17#[cfg(test)]
18mod tests {
19    use crate::parsers::UnwrapValue;
20    use assert2::check;
21
22    use crate::*;
23
24    #[test]
25    fn test_parse() {
26        let input = "(:functions (battery-amount ?r - rover))";
27        check!(
28            Functions::from_str(input).is_value(Functions::from_iter([FunctionTyped::number(
29                AtomicFunctionSkeleton::new(
30                    FunctionSymbol::string("battery-amount"),
31                    TypedList::from_iter([Typed::new(
32                        Variable::from("r"),
33                        Type::Exactly("rover".into())
34                    )])
35                )
36            )]))
37        );
38    }
39
40    #[test]
41    fn doc_example_0() {
42        let input = "(:functions (battery-amount ?r - rover))";
43        check!(
44            Functions::from_str(input).is_value(Functions::from_iter([FunctionTyped::number(
45                AtomicFunctionSkeleton::new(
46                    FunctionSymbol::string("battery-amount"),
47                    TypedList::from_iter([Typed::new(
48                        Variable::from("r"),
49                        Type::Exactly("rover".into())
50                    )])
51                )
52            )]))
53        );
54    }
55}