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
use syn::Expr;
use super::*;
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum ActionExpr {
Process(Action<ProcessExpr>),
Initial(Action<InitialExpr>),
Err(Action<ErrExpr>),
}
impl ActionExpr {
pub fn get_move_type(&self) -> &MoveType {
match self {
Self::Process(expr) => &expr.move_type,
Self::Err(expr) => &expr.move_type,
Self::Initial(expr) => &expr.move_type,
}
}
pub fn get_apply_type(&self) -> &ApplyType {
match self {
Self::Process(expr) => &expr.apply_type,
Self::Err(expr) => &expr.apply_type,
Self::Initial(expr) => &expr.apply_type,
}
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct Action<E: InnerExpr> {
pub expr: E,
pub apply_type: ApplyType,
pub move_type: MoveType,
}
impl<E: InnerExpr> Action<E> {
pub fn new(expr: E, apply_type: ApplyType, move_type: MoveType) -> Self {
Self {
expr,
apply_type,
move_type,
}
}
}
impl<E: InnerExpr> InnerExpr for Action<E> {
fn replace_inner(&self, exprs: Vec<Expr>) -> Option<Self> {
self.expr.replace_inner(exprs).map(|expr| Self {
expr,
apply_type: self.apply_type.clone(),
move_type: self.move_type.clone(),
})
}
fn extract_inner(&self) -> Option<Vec<&Expr>> {
self.expr.extract_inner()
}
fn is_replaceable(&self) -> bool {
self.expr.is_replaceable()
}
}
impl InnerExpr for ActionExpr {
fn extract_inner(&self) -> Option<Vec<&Expr>> {
match self {
Self::Process(expr) => expr.extract_inner(),
Self::Err(expr) => expr.extract_inner(),
Self::Initial(expr) => expr.extract_inner(),
}
}
fn replace_inner(&self, exprs: Vec<Expr>) -> Option<Self> {
match self {
Self::Process(inner) => inner.replace_inner(exprs).map(Self::Process),
Self::Err(inner) => inner.replace_inner(exprs).map(Self::Err),
Self::Initial(inner) => inner.replace_inner(exprs).map(Self::Initial),
}
}
fn is_replaceable(&self) -> bool {
match self {
Self::Process(expr) => expr.is_replaceable(),
Self::Err(expr) => expr.is_replaceable(),
Self::Initial(expr) => expr.is_replaceable(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use syn::{parse_quote, Expr};
#[test]
fn it_tests_inner_expr_trait_impl_for_action() {
let expr: Expr = parse_quote! { |v| v + 1 };
let replace_expr: Expr = parse_quote! { |v| v + 2 };
for action_expr in vec![
ActionExpr::Process(Action::new(
ProcessExpr::Then(expr.clone()),
ApplyType::Instant,
MoveType::None,
)),
ActionExpr::Err(Action::new(
ErrExpr::Or(expr.clone()),
ApplyType::Instant,
MoveType::None,
)),
ActionExpr::Initial(Action::new(
InitialExpr(expr.clone()),
ApplyType::Instant,
MoveType::None,
)),
]
.into_iter()
{
assert_eq!(action_expr.extract_inner().clone(), Some(vec![&expr]));
assert_eq!(
action_expr
.replace_inner(vec![replace_expr.clone()])
.unwrap()
.extract_inner(),
Some(vec![&replace_expr])
)
}
}
}