pddl_parser/plan/
plan.rs

1use nom::multi::many0;
2use serde::{Deserialize, Serialize};
3
4use super::action::Action;
5use crate::error::ParserError;
6use crate::lexer::TokenStream;
7
8/// A plan is a sequence of actions.
9///
10/// The order of the actions is important. Plan is a wrapper around a `Vec<Action>` that implements `IntoIterator` and `FromIterator<Action>`. This might change in the future.
11#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, PartialOrd)]
12pub struct Plan(pub Vec<Action>);
13
14impl Plan {
15    /// Parse a plan from a token stream.
16    ///
17    /// The plan must be a sequence of actions. The parser will fail if there are any tokens left after the plan.
18    ///
19    /// # Errors
20    ///
21    /// The parser will fail if there are any tokens left after the plan. It will also fail if the plan is empty or if any of the actions are invalid.
22    pub fn parse(input: TokenStream) -> Result<Self, ParserError> {
23        let (output, items) = many0(Action::parse)(input)?;
24        if !output.is_empty() {
25            log::error!("Plan parser failed: {:?}", output.peek_n(10));
26            return Err(ParserError::ExpectedEndOfInput);
27        }
28        Ok(Plan(items))
29    }
30
31    /// Get an iterator over the actions in the plan.
32    pub fn actions(&self) -> impl Iterator<Item = &Action> {
33        self.0.iter()
34    }
35}
36
37impl IntoIterator for Plan {
38    type IntoIter = std::vec::IntoIter<Self::Item>;
39    type Item = Action;
40
41    fn into_iter(self) -> Self::IntoIter {
42        self.0.into_iter()
43    }
44}
45
46impl FromIterator<Action> for Plan {
47    fn from_iter<T: IntoIterator<Item = Action>>(iter: T) -> Self {
48        Self(iter.into_iter().collect())
49    }
50}