Skip to main content

pddl_core/parsers/structure/
constants_def.rs

1//! Provides parsers for constant definitions (`<constants-def>`).
2
3use crate::parsers::error::R;
4use crate::parsers::sexpr::Node;
5use crate::parsers::{parse_name, prefix, typed_list};
6use crate::types::Constants;
7
8/// Parses a constant definition (`<constants-def>`), i.e. `(:constants <typed list (name)>)`.
9pub(crate) fn parse_constants_def(node: &Node) -> R<Constants> {
10    prefix(node, ":constants", |args| {
11        typed_list(args, parse_name).map(Constants::new)
12    })
13}
14
15crate::parsers::impl_parser!(Constants => parse_constants_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 = "(:constants B P D - physob)";
27        check!(
28            Constants::from_str(input).is_value(Constants::new(TypedList::from_iter([
29                Name::from("B").to_typed("physob"),
30                Name::from("P").to_typed("physob"),
31                Name::from("D").to_typed("physob"),
32            ])))
33        );
34    }
35
36    #[test]
37    fn doc_example_0() {
38        let input = "(:constants B P D - physob)";
39        check!(
40            Constants::from_str(input).is_value(Constants::new(TypedList::from_iter([
41                Name::from("B").to_typed("physob"),
42                Name::from("P").to_typed("physob"),
43                Name::from("D").to_typed("physob"),
44            ])))
45        );
46    }
47}