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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
use std::{rc::Rc, sync::Arc};
use super::CommandContext;
use crate::{
errors::{CommandResultTrait, CommandSyntaxError},
result_consumer::ResultConsumer,
};
pub struct ContextChain<S, R> {
modifiers: Vec<Rc<CommandContext<S, R>>>,
executable: Rc<CommandContext<S, R>>,
next_stage_cache: Option<Rc<ContextChain<S, R>>>,
}
impl<S, R: CommandResultTrait> ContextChain<S, R> {
pub fn new(
modifiers: Vec<Rc<CommandContext<S, R>>>,
executable: Rc<CommandContext<S, R>>,
) -> Self {
if executable.command.is_none() {
panic!("Last command in chain must be executable");
}
Self {
modifiers,
executable,
next_stage_cache: None,
}
}
pub fn try_flatten(root_context: Rc<CommandContext<S, R>>) -> Option<Self> {
let mut modifiers = Vec::new();
let mut current = root_context;
loop {
let child = current.child.clone();
let Some(child) = child else {
// Last entry must be executable command
current.command.as_ref()?;
return Some(ContextChain::new(modifiers, current));
};
modifiers.push(current);
current = child;
}
}
pub fn run_modifier(
modifier: Rc<CommandContext<S, R>>,
source: Arc<S>,
result_consumer: &dyn ResultConsumer<S, R>,
forked_mode: bool,
) -> Result<Vec<Arc<S>>, CommandSyntaxError> {
let source_modifier = modifier.redirect_modifier();
let Some(source_modifier) = source_modifier else {
return Ok(vec![source]);
};
let context_to_use = Rc::new(modifier.copy_for(source));
let err = match (source_modifier)(&context_to_use) {
Ok(res) => return Ok(res),
Err(e) => e,
};
result_consumer.on_command_complete(context_to_use, false, 0);
if forked_mode {
return Ok(vec![]);
}
Err(err)
}
pub fn run_executable(
&self,
executable: Rc<CommandContext<S, R>>,
source: Arc<S>,
result_consumer: &dyn ResultConsumer<S, R>,
forked_mode: bool,
) -> Result<R, CommandSyntaxError> {
let context_to_use = Rc::new(executable.copy_for(source));
let Some(command) = &executable.command else {
unimplemented!();
};
let res = (command)(&context_to_use);
let err = match res {
Ok(res) => {
let Some(res) = res.as_i32() else {
// these are treated as exceptions, so they can bubble up without doing anything
// else
return Ok(res);
};
result_consumer.on_command_complete(context_to_use, true, res);
return if forked_mode {
Ok(R::new(1))
} else {
Ok(R::new(res))
};
}
Err(err) => err,
};
result_consumer.on_command_complete(context_to_use, false, 0);
if forked_mode { Ok(R::new(0)) } else { Err(err) }
}
pub fn execute_all(
&self,
source: Arc<S>,
result_consumer: &dyn ResultConsumer<S, R>,
) -> Result<R, CommandSyntaxError> {
if self.modifiers.is_empty() {
return self.run_executable(self.executable.clone(), source, result_consumer, false);
}
let mut forked_mode = false;
let mut current_sources = vec![source];
for modifier in &self.modifiers {
forked_mode |= modifier.is_forked();
let mut next_sources = Vec::new();
for source_to_run in current_sources {
match Self::run_modifier(
modifier.clone(),
source_to_run.clone(),
result_consumer,
forked_mode,
) {
Ok(res) => next_sources.extend(res),
Err(err) => return Err(err),
}
}
if next_sources.is_empty() {
return Ok(R::new(0));
}
current_sources = next_sources;
}
let mut summed = 0;
for execution_source in current_sources {
let res = self.run_executable(
self.executable.clone(),
execution_source,
result_consumer,
forked_mode,
)?;
match res.as_i32() {
Some(res) => summed += res,
None => return Ok(res),
}
}
Ok(R::new(summed))
}
pub fn stage(&self) -> Stage {
if self.modifiers.is_empty() {
Stage::Execute
} else {
Stage::Modify
}
}
pub fn top_context(&self) -> Rc<CommandContext<S, R>> {
self.modifiers
.first()
.cloned()
.unwrap_or_else(|| self.executable.clone())
}
pub fn next_stage(&mut self) -> Option<Rc<ContextChain<S, R>>> {
let modifier_count = self.modifiers.len();
if modifier_count == 0 {
return None;
}
if self.next_stage_cache.is_none() {
self.next_stage_cache = Some(Rc::new(ContextChain::new(
self.modifiers[1..].to_vec(),
self.executable.clone(),
)));
}
self.next_stage_cache.clone()
}
}
pub enum Stage {
Modify,
Execute,
}
|