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
use serde::{Deserialize, Serialize};
use super::model::*;
#[derive(Serialize, Deserialize, Clone, Hash, Eq, PartialEq, Debug)]
pub enum Action {
Silent,
Labeled(LabeledAction),
}
const EMPTY_ARGUMENTS: [Value; 0] = [];
impl Action {
pub(crate) fn new(label: LabelIndex, arguments: Box<[Value]>) -> Self {
Action::Labeled(LabeledAction::new(label, arguments))
}
pub fn is_silent(&self) -> bool {
matches!(self, Action::Silent)
}
pub fn is_labeled(&self) -> bool {
matches!(self, Action::Labeled(_))
}
pub fn label_index(&self) -> Option<LabelIndex> {
match self {
Action::Silent => None,
Action::Labeled(labeled) => Some(labeled.label),
}
}
pub fn arguments(&self) -> &[Value] {
match self {
Action::Silent => &EMPTY_ARGUMENTS,
Action::Labeled(labeled) => labeled.arguments(),
}
}
}
#[derive(Serialize, Deserialize, Clone, Hash, Eq, PartialEq, Debug)]
pub struct LabeledAction {
pub(crate) label: LabelIndex,
pub(crate) arguments: Box<[Value]>,
}
impl LabeledAction {
pub(crate) fn new(label: LabelIndex, arguments: Box<[Value]>) -> Self {
LabeledAction { label, arguments }
}
pub fn new_with_network(network: &Network, label: &str, arguments: Box<[Value]>) -> Self {
LabeledAction {
label: network
.declarations
.action_labels
.get_index_of(label)
.unwrap(),
arguments,
}
}
pub fn label_index(&self) -> LabelIndex {
self.label
}
pub fn label<'n>(&self, network: &'n Network) -> Option<&'n String> {
network
.declarations
.action_labels
.get_index(self.label)
.map(|(action_name, _)| action_name)
}
pub fn arguments(&self) -> &[Value] {
&self.arguments
}
}
impl Into<Action> for LabeledAction {
fn into(self) -> Action {
Action::Labeled(self)
}
}