pddl_parser/plan/
simple_action.rs

1use std::fmt::Display;
2
3use nom::sequence::{delimited, pair};
4use nom::IResult;
5use serde::{Deserialize, Serialize};
6
7use crate::domain::parameter::Parameter;
8use crate::error::ParserError;
9use crate::lexer::{Token, TokenStream};
10use crate::tokens::id;
11
12/// Action is a named sequence of steps that can be performed by an agent.
13#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
14pub struct SimpleAction {
15    /// The name of the action.
16    pub name: String,
17    /// The parameters of the action.
18    #[serde(default)]
19    pub parameters: Vec<Parameter>,
20}
21
22impl SimpleAction {
23    /// Create a new action.
24    pub const fn new(name: String, parameters: Vec<Parameter>) -> Self {
25        Self { name, parameters }
26    }
27
28    /// Parse an action from a token stream.
29    pub fn parse(input: TokenStream) -> IResult<TokenStream, Self, ParserError> {
30        let (output, (name, parameters)) = delimited(
31            Token::OpenParen,
32            pair(id, Parameter::parse_parameters),
33            Token::CloseParen,
34        )(input)?;
35        Ok((output, Self::new(name, parameters)))
36    }
37}
38
39impl Display for SimpleAction {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        write!(
42            f,
43            "({} {})",
44            self.name,
45            self.parameters
46                .iter()
47                .map(ToString::to_string)
48                .collect::<Vec<_>>()
49                .join(" ")
50        )
51    }
52}