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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
// Doc Not Optimize
#![allow(clippy::borrowed_box)]
#![allow(clippy::too_many_lines)]
use crate::{
AnyOutput, ChainProcess, NextProcess, Program, ProgramCollect, RenderResult,
error::ProgramInternalExecuteError, hook::ProgramControls,
};
#[doc(hidden)]
pub mod error;
#[might_be_async::func]
pub fn exec<C>(program: &'static Program<C>) -> Result<RenderResult, ProgramInternalExecuteError>
where
C: ProgramCollect<Enum = C> + Send + Sync,
{
let mut args = program.args.clone();
might_be_async::invoke!(exec_with_args(program, &mut args))
}
#[might_be_async::func]
pub fn exec_with_args<C>(
program: &'static Program<C>,
args: &mut Vec<String>,
) -> Result<RenderResult, ProgramInternalExecuteError>
where
C: ProgramCollect<Enum = C> + Send + Sync,
{
// Exit code
let mut exit_code: i32 = 0;
macro_rules! control {
($hook_call:expr, $current:expr) => {
let __ccc = $hook_call;
if let Some(r) =
handle_program_control(program, __ccc, Some(&mut $current), &mut exit_code)
{
return Ok(r);
}
};
($hook_call:expr) => {
let __ccc = $hook_call;
if let Some(r) = handle_program_control(program, __ccc, None, &mut exit_code) {
return Ok(r);
}
};
}
// Current
let mut current = C::build_entry_fallback(vec![]);
// Run hooks
control!(
program.run_hook_pre_dispatch(&mut crate::hook::HookPreDispatchInfo {
arguments: &mut *args,
}),
current
);
// Dispatch args
let mut current = C::dispatch_args(args)?;
// Run hook
control!(
program.run_hook_post_dispatch(&crate::hook::HookPostDispatchInfo {
entry: ¤t.member_id,
}),
current
);
let mut stop_next = false;
// If the program has Help enabled, skip actual logic and jump to Help
if program.user_context.help {
let mut render_result = render_help::<C>(program, current);
// Run hook
control!(program.run_hook_finish(&crate::hook::HookFinishInfo {}));
render_result.exit_code = exit_code;
return Ok(render_result);
}
loop {
let final_exec = stop_next;
current = {
// If a chain exists, execute as a chain
if C::has_chain(¤t) {
// Run hook
control!(
program.run_hook_pre_chain(&crate::hook::HookPreChainInfo {
input: ¤t.member_id,
raw: current.inner.as_ref(),
}),
current
);
match might_be_async::invoke!(C::do_chain(current)) {
ChainProcess::Ok((any, NextProcess::Renderer)) => {
{
let mut render_result = render::<C>(program, any);
// Run hook
control!(program.run_hook_finish(&crate::hook::HookFinishInfo {}));
render_result.exit_code = exit_code;
return Ok(render_result);
};
}
ChainProcess::Ok((mut any, NextProcess::Chain)) => {
// Run hook
control!(
program.run_hook_post_chain(&crate::hook::HookPostChainInfo {
output: &any
}),
any
);
any
}
ChainProcess::Err(e) => {
// Run hook
control!(
program.run_hook_finish(&crate::hook::HookFinishInfo {}),
&mut C::build_empty_result()
);
return Err(e.into());
}
}
}
// If no chain exists, attempt to render
else if C::has_renderer(¤t) {
// Run hook
control!(
program.run_hook_pre_render(&crate::hook::HookPreRenderInfo {
input: ¤t.member_id,
raw: current.inner.as_ref(),
}),
current
);
let mut render_result = render::<C>(program, current);
// Run hooks
control!(
program.run_hook_post_render(&crate::hook::HookPostRenderInfo {
result: &render_result,
})
);
control!(program.run_hook_finish(&crate::hook::HookFinishInfo {}));
render_result.exit_code = exit_code;
return Ok(render_result);
}
// No renderer exists
else {
stop_next = true;
C::build_renderer_not_found(current.member_id)
}
};
if final_exec && stop_next {
break;
}
}
let mut render_result = RenderResult::default();
// Run hook
control!(
program.run_hook_finish(&crate::hook::HookFinishInfo {}),
current
);
render_result.exit_code = exit_code;
Ok(render_result)
}
#[inline]
pub(crate) fn handle_program_control<C: ProgramCollect<Enum = C>>(
program: &Program<C>,
controls: ProgramControls<C>,
mut current: Option<&mut AnyOutput<C>>,
exit_code: &mut i32,
) -> Option<RenderResult> {
for unit in controls {
match unit {
super::hook::ProgramControlUnit::OverrideExitCode(c) => *exit_code = c,
super::hook::ProgramControlUnit::RouteToChain(any_output) => {
if let Some(ref mut current) = current {
**current = any_output;
}
}
super::hook::ProgramControlUnit::RouteToRender(any_output) => {
// Note: Hooks triggered by ProgramControl will not trigger ProgramControl again
// Pre render
let _ = program.run_hook_pre_render(&crate::hook::HookPreRenderInfo {
input: &any_output.member_id,
raw: any_output.inner.as_ref(),
});
let mut r = render::<C>(program, any_output);
r.exit_code = *exit_code;
// Post render
program.run_hook_post_render(&crate::hook::HookPostRenderInfo { result: &r });
return Some(r);
}
super::hook::ProgramControlUnit::RouteToHelp(any_output) => {
let mut r = render_help::<C>(program, any_output);
r.exit_code = *exit_code;
return Some(r);
}
}
}
None
}
#[inline]
#[allow(unused_variables)]
fn render<C: ProgramCollect<Enum = C>>(program: &Program<C>, any: AnyOutput<C>) -> RenderResult {
#[cfg(not(feature = "structural_renderer"))]
{
C::render(any)
}
#[cfg(feature = "structural_renderer")]
{
#[allow(unreachable_patterns)]
match program.structural_renderer_name {
super::StructuralRendererSetting::Disable => C::render(any),
_ => C::structural_render(any, &program.structural_renderer_name).unwrap(),
}
}
}
#[inline]
#[allow(unused_variables)]
fn render_help<C: ProgramCollect<Enum = C>>(
program: &Program<C>,
entry: AnyOutput<C>,
) -> RenderResult {
#[cfg(not(feature = "structural_renderer"))]
{
C::render_help(entry)
}
#[cfg(feature = "structural_renderer")]
{
#[allow(unreachable_patterns)]
match program.structural_renderer_name {
super::StructuralRendererSetting::Disable => C::render_help(entry),
_ => RenderResult::default(),
}
}
}
|