pddl/types/
time_specifier.rs

1//! Contains the [`TimeSpecifier`] type.
2
3use std::fmt::{Display, Formatter};
4
5/// A time specifier used in e.g. [`TimedGD::At`](crate::types::TimedGD::At) and [`TimedEffect`](crate::types::TimedEffect).
6///
7/// ## Usage
8/// Used by [`TimedGD`](crate::TimedGD) and [`TimedEffect`](crate::TimedEffect).
9#[derive(Debug, Copy, Clone, Eq, PartialEq)]
10pub enum TimeSpecifier {
11    /// The condition or effect holds or applies at the beginning of a plan.
12    Start,
13    /// The condition or effect must hold or apply at the end of a plan.
14    End,
15}
16
17pub mod names {
18    pub const START: &str = "start";
19    pub const END: &str = "end";
20}
21
22impl Display for TimeSpecifier {
23    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
24        match self {
25            TimeSpecifier::Start => write!(f, "{}", names::START),
26            TimeSpecifier::End => write!(f, "{}", names::END),
27        }
28    }
29}
30
31impl TryFrom<&str> for TimeSpecifier {
32    type Error = ParseError;
33
34    fn try_from(value: &str) -> Result<Self, Self::Error> {
35        match value {
36            names::START => Ok(Self::Start),
37            names::END => Ok(Self::End),
38            _ => Err(ParseError::InvalidSpecifier),
39        }
40    }
41}
42
43#[derive(Debug, Clone, thiserror::Error)]
44pub enum ParseError {
45    #[error("Invalid specifier")]
46    InvalidSpecifier,
47}