pddl_parser/plan/
action.rs1use 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#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, PartialOrd)]
15pub enum Action {
16 Simple(SimpleAction),
18 Durative(DurativeAction),
20}
21
22impl Action {
23 pub fn name(&self) -> &str {
25 match self {
26 Self::Simple(action) => &action.name,
27 Self::Durative(action) => &action.name,
28 }
29 }
30
31 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 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}