pddl_parser/plan/
action.rs

1use std::fmt::Display;
2
3use nom::branch::alt;
4use nom::combinator::map;
5use nom::IResult;
6use serde::{Deserialize, Serialize};
7
8use super::durative_action::DurativeAction;
9use super::simple_action::SimpleAction;
10use crate::error::ParserError;
11use crate::lexer::TokenStream;
12
13/// Enum to represent either an `Action` or a `DurativeAction`.
14#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, PartialOrd)]
15pub enum Action {
16    /// An Action wrapper around a simple action. See [`SimpleAction`](../simple_action/struct.SimpleAction.html).
17    Simple(SimpleAction),
18    /// An Action wrapper around a durative action. See [`DurativeAction`](../durative_action/struct.DurativeAction.html).
19    Durative(DurativeAction),
20}
21
22impl Action {
23    /// Get the name of the action. This is the same as the name of the simple or durative action.
24    pub fn name(&self) -> &str {
25        match self {
26            Self::Simple(action) => &action.name,
27            Self::Durative(action) => &action.name,
28        }
29    }
30
31    /// Get the parameters of the action. This is the same as the parameters of the simple or durative action.
32    pub fn parameters(&self) -> &[crate::domain::parameter::Parameter] {
33        match self {
34            Self::Simple(action) => &action.parameters,
35            Self::Durative(action) => &action.parameters,
36        }
37    }
38
39    /// Get the precondition of the action. This is the same as the precondition of the simple or durative action.
40    pub fn parse(input: TokenStream) -> IResult<TokenStream, Action, ParserError> {
41        log::debug!("BEGIN > parse_actions {:?}", input.span());
42        let (output, actions) = alt((
43            map(SimpleAction::parse, Action::Simple),
44            map(DurativeAction::parse, Action::Durative),
45        ))(input)?;
46        log::debug!("END < parse_actions {:?}", output.span());
47        Ok((output, actions))
48    }
49}
50
51impl Display for Action {
52    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53        match self {
54            Action::Simple(action) => write!(f, "{action}"),
55            Action::Durative(action) => write!(f, "{action}"),
56        }
57    }
58}