1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//! Provide assignment operations via the [`AssignOp`] type.
use std::borrow::Borrow;
use std::fmt::{Display, Formatter};
/// An assignment operation.
///
/// ## Usage
/// Used by [`PEffect`](crate::PEffect), [`TimedEffect`](crate::TimedEffect) and
/// [`FAssignDa`](crate::FAssignDa).
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum AssignOp {
/// An assign effect assigns the value of a numeric variable to the given amount.
/// ```pddl
/// (assign (battery-level ?r) 10)
/// ```
///
/// It is possible to use another numeric variable as the assign value, for example:
/// ```pddl
/// (assign (battery-level ?r) (max-charge ?r))
/// ```
Assign,
/// A scale up effect increases the value of the numeric variable by the given scale factor.
/// This resembles the C-style operator `+=`.
///
/// ```pddl
/// (scale-up (battery-level ?r) 2)
/// ```
///
/// The scale factor can be another numeric variable.
/// ```pddl
/// (scale-up (battery-level ?r) (charge-rate ?r))
/// ```
ScaleUp,
/// A scale down effect decreases the value of the numeric variable by the given scale factor.
/// This resembles the C-style operator `-=`.
///
/// ```pddl
/// (scale-down (battery-level ?r) 2)
/// ```
///
/// The scale factor can be another numeric variable.
/// ```pddl
/// (scale-down (battery-level ?r) (consumption-rate ?r))
/// ```
ScaleDown,
/// An increase effect increases the value of a numeric variable by the given amount.
/// This resembles the C-style operator `*=`.
///
/// ```pddl
/// (increase (battery-level ?r) 10)
/// ```
///
/// It is possible to use another numeric variable as the increase value for example.
/// ```pddl
/// (increase (battery-level ?r) (charge-available - ?solarpanel))
/// ```
Increase,
/// A decrease effect decreases the value of a numeric variable by the given amount.
/// This resembles the C-style operator `/=`.
///
/// ```pddl
/// (decrease (battery-level ?r) 10)
/// ```
///
/// It is possible to use another numeric variable as the decrease value for example.
/// ```pddl
/// (decrease (battery-level ?r) (power-needed-for-work - ?task))
/// ```
Decrease,
}
pub mod names {
/// [`CHANGE`] is a deprecated name for [`ASSIGN`].
pub const CHANGE: &str = "change";
pub const ASSIGN: &str = "assign";
pub const SCALE_UP: &str = "scale-up";
pub const SCALE_DOWN: &str = "scale-down";
pub const INCREASE: &str = "increase";
pub const DECREASE: &str = "decrease";
}
impl AssignOp {
pub fn as_str(&self) -> &'static str {
match self {
AssignOp::Assign => names::ASSIGN,
AssignOp::ScaleUp => names::SCALE_UP,
AssignOp::ScaleDown => names::SCALE_DOWN,
AssignOp::Increase => names::INCREASE,
AssignOp::Decrease => names::DECREASE,
}
}
}
impl TryFrom<&str> for AssignOp {
type Error = ParseError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
names::CHANGE => Ok(AssignOp::Assign),
names::ASSIGN => Ok(AssignOp::Assign),
names::SCALE_UP => Ok(AssignOp::ScaleUp),
names::SCALE_DOWN => Ok(AssignOp::ScaleDown),
names::INCREASE => Ok(AssignOp::Increase),
names::DECREASE => Ok(AssignOp::Decrease),
_ => Err(ParseError::InvalidOperation),
}
}
}
#[derive(Debug, Clone, thiserror::Error)]
pub enum ParseError {
#[error("Invalid operation")]
InvalidOperation,
}
impl Borrow<str> for AssignOp {
fn borrow(&self) -> &str {
self.as_str()
}
}
impl AsRef<str> for AssignOp {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl Display for AssignOp {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_ref())
}
}
impl PartialEq<AssignOp> for &AssignOp {
#[inline(always)]
fn eq(&self, other: &AssignOp) -> bool {
other.eq(*self)
}
}
impl PartialEq<&AssignOp> for AssignOp {
#[inline(always)]
fn eq(&self, other: &&AssignOp) -> bool {
self.eq(*other)
}
}
impl PartialEq<String> for AssignOp {
#[inline(always)]
fn eq(&self, other: &String) -> bool {
other.eq(self.as_ref())
}
}
impl PartialEq<&str> for AssignOp {
#[inline(always)]
fn eq(&self, other: &&str) -> bool {
(*other).eq(self.as_ref())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn str_equals_works() {
assert_eq!(AssignOp::Assign, "assign");
assert_eq!(AssignOp::ScaleUp, "scale-up");
assert_eq!(AssignOp::ScaleDown, "scale-down");
assert_eq!(AssignOp::Increase, "increase");
assert_eq!(AssignOp::Decrease, "decrease");
}
}