pddl/parsers/
constants_def.rs

1//! Provides parsers for constant definitions.
2
3use nom::combinator::map;
4
5use crate::parsers::{parse_name, prefix_expr, typed_list, ParseResult, Span};
6use crate::types::Constants;
7
8/// Parses constant definitions, i.e. `(:constants <typed list (name)>)`.
9///
10/// ## Example
11/// ```
12/// # use pddl::parsers::{parse_constants_def, preamble::*};
13/// # use pddl::{Variable, AtomicFormulaSkeleton, Predicate, PredicateDefinitions, Constants, Name, ToTyped, TypedList};
14/// let input = "(:constants B P D - physob)";
15/// assert!(parse_constants_def(input).is_value(
16///     Constants::new(TypedList::from_iter([
17///         Name::from("B").to_typed("physob"),
18///         Name::from("P").to_typed("physob"),
19///         Name::from("D").to_typed("physob"),
20///     ]))
21/// ));
22/// ```
23pub fn parse_constants_def<'a, T: Into<Span<'a>>>(input: T) -> ParseResult<'a, Constants> {
24    map(prefix_expr(":constants", typed_list(parse_name)), |vec| {
25        Constants::new(vec)
26    })(input.into())
27}
28
29impl crate::parsers::Parser for Constants {
30    type Item = Constants;
31
32    /// See [`parse_constants_def`].
33    fn parse<'a, S: Into<Span<'a>>>(input: S) -> ParseResult<'a, Self::Item> {
34        parse_constants_def(input)
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use crate::parsers::UnwrapValue;
41    use crate::{Constants, Name, Parser, ToTyped, TypedList};
42
43    #[test]
44    fn test_parse() {
45        let input = "(:constants B P D - physob)";
46        assert!(
47            Constants::parse(input).is_value(Constants::new(TypedList::from_iter([
48                Name::from("B").to_typed("physob"),
49                Name::from("P").to_typed("physob"),
50                Name::from("D").to_typed("physob"),
51            ])))
52        );
53    }
54}