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
//! Contains effects via the [`Effects`] type.
use crate::types::CEffect;
use std::ops::Deref;
/// An effect. Occurs e.g. in a [`ActionDefinition`](crate::types::ActionDefinition).
///
/// This represents the `(and ...)` variant of the PDDL definition,
/// modeling cases of zero, one or many effects.
///
/// ## Usage
/// Used by [`ActionDefinition`](crate::ActionDefinition).
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Effects(Vec<CEffect>);
impl Effects {
/// Constructs a new instance from the value.
pub fn new(effect: CEffect) -> Self {
Self(vec![effect])
}
/// Constructs a new instance from the provided vector of values.
pub fn new_and(effects: Vec<CEffect>) -> Self {
Self(effects)
}
/// Returns `true` if the list contains no elements.
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
/// Returns the number of elements in the list, also referred to
/// as its 'length'.
pub fn len(&self) -> usize {
self.0.len()
}
/// Returns an iterator over the list.
///
/// The iterator yields all items from start to end.
pub fn iter(&self) -> std::slice::Iter<CEffect> {
self.0.iter()
}
/// Get the only element of this list if the list has
/// exactly one element. Returns [`None`] in all other cases.
pub fn try_get_single(self) -> Option<CEffect> {
if self.len() == 1 {
self.into_iter().next()
} else {
None
}
}
}
impl IntoIterator for Effects {
type Item = CEffect;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl Deref for Effects {
type Target = [CEffect];
fn deref(&self) -> &Self::Target {
self.0.as_slice()
}
}
impl AsRef<[CEffect]> for Effects {
fn as_ref(&self) -> &[CEffect] {
self.0.as_slice()
}
}
impl From<CEffect> for Effects {
fn from(value: CEffect) -> Self {
Effects::new(value)
}
}
impl From<Vec<CEffect>> for Effects {
fn from(value: Vec<CEffect>) -> Self {
Effects::new_and(value)
}
}
impl FromIterator<CEffect> for Effects {
fn from_iter<T: IntoIterator<Item = CEffect>>(iter: T) -> Self {
Effects::new_and(iter.into_iter().collect())
}
}
impl TryInto<CEffect> for Effects {
type Error = ();
fn try_into(self) -> Result<CEffect, Self::Error> {
self.try_get_single().ok_or(())
}
}