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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
#![allow(dead_code)]
use std::any::Any;
use crate::{AnyOutput, Program, ProgramCollect, RenderResult, error::ProgramPanic};
#[derive(Default)]
pub struct ProgramHook<C>
where
C: ProgramCollect<Enum = C>,
{
/// Executes when the program starts running
pub begin: Option<fn()>,
/// Executes before the program dispatches
pub pre_dispatch: Option<fn(args: &Vec<String>)>,
/// Executes after the program dispatches
pub post_dispatch: Option<fn(entry: &C)>,
/// Executes before the type enters the chain
pub pre_chain: Option<fn(input: &C, raw: &dyn Any)>,
/// Executes after the chain processing for the type ends
pub post_chain: Option<fn(output: &AnyOutput<C>)>,
/// Executes before the type enters the renderer
pub pre_render: Option<fn(input: &C, raw: &dyn Any)>,
/// Executes after the type enters the renderer
pub post_render: Option<fn(result: &RenderResult)>,
/// Executes before the program ends
pub finish: Option<fn() -> i32>,
/// Executes when the program panics
pub exec_panic: Option<fn(&ProgramPanic)>,
/// Executes when the REPL starts (only available with `repl` feature)
#[cfg(feature = "repl")]
pub repl_on_begin: Option<fn()>,
/// Executes before reading the next REPL line (only available with `repl` feature)
#[cfg(feature = "repl")]
pub repl_pre_readline: Option<fn()>,
/// Executes after reading a REPL line (only available with `repl` feature)
#[cfg(feature = "repl")]
pub repl_post_readline: Option<fn(line: &str)>,
/// Executes when the REPL receives a render result (only available with `repl` feature)
#[cfg(feature = "repl")]
pub repl_on_receive_result: Option<fn(&RenderResult)>,
/// Executes when the REPL panics (only available with `repl` feature)
#[cfg(feature = "repl")]
pub repl_on_panic: Option<fn(&ProgramPanic)>,
}
impl<C> Program<C>
where
C: ProgramCollect<Enum = C>,
{
/// Adds a typed hook to the program. The hook will be called at the appropriate
/// lifecycle events.
pub fn with_hook(&mut self, hook: ProgramHook<C>) {
self.hooks.push(hook);
}
pub(crate) fn run_hook_on_begin(&self) {
if !self.user_context.run_hook {
return;
}
for hook in &self.hooks {
if let Some(begin) = hook.begin {
begin()
}
}
}
pub(crate) fn run_hook_pre_dispatch(&self, args: &Vec<String>) {
if !self.user_context.run_hook {
return;
}
for hook in &self.hooks {
if let Some(pre_dispatch) = hook.pre_dispatch {
pre_dispatch(args)
}
}
}
pub(crate) fn run_hook_post_dispatch(&self, entry: &C) {
if !self.user_context.run_hook {
return;
}
for hook in &self.hooks {
if let Some(post_dispatch) = hook.post_dispatch {
post_dispatch(entry)
}
}
}
pub(crate) fn run_hook_pre_chain(&self, input: &C, raw: &dyn Any) {
if !self.user_context.run_hook {
return;
}
for hook in &self.hooks {
if let Some(pre_chain) = hook.pre_chain {
pre_chain(input, raw)
}
}
}
pub(crate) fn run_hook_post_chain(&self, output: &AnyOutput<C>) {
if !self.user_context.run_hook {
return;
}
for hook in &self.hooks {
if let Some(post_chain) = hook.post_chain {
post_chain(output)
}
}
}
pub(crate) fn run_hook_pre_render(&self, input: &C, raw: &dyn Any) {
if !self.user_context.run_hook {
return;
}
for hook in &self.hooks {
if let Some(pre_render) = hook.pre_render {
pre_render(input, raw)
}
}
}
pub(crate) fn run_hook_post_render(&self, result: &RenderResult) {
if !self.user_context.run_hook {
return;
}
for hook in &self.hooks {
if let Some(post_render) = hook.post_render {
post_render(result)
}
}
}
#[allow(dead_code)]
pub(crate) fn run_hook_exec_panic(&self, panic_info: &ProgramPanic) {
if !self.user_context.run_hook {
return;
}
for hook in &self.hooks {
if let Some(exec_panic) = hook.exec_panic {
exec_panic(panic_info)
}
}
}
pub(crate) fn run_hook_finish(&self) -> i32 {
if !self.user_context.run_hook {
return 0;
}
let mut exit_code = 0;
for hook in &self.hooks {
if let Some(finish) = hook.finish {
exit_code = finish();
if exit_code != 0 {
return exit_code;
}
}
}
exit_code
}
/// Runs the REPL begin hooks (only available with `repl` feature)
#[cfg(feature = "repl")]
pub(crate) fn run_hook_repl_on_begin(&self) {
if !self.user_context.run_hook {
return;
}
for hook in &self.hooks {
if let Some(repl_on_begin) = hook.repl_on_begin {
repl_on_begin()
}
}
}
/// Runs the REPL pre-readline hooks (only available with `repl` feature)
#[cfg(feature = "repl")]
pub(crate) fn run_hook_repl_pre_readline(&self) {
if !self.user_context.run_hook {
return;
}
for hook in &self.hooks {
if let Some(repl_pre_readline) = hook.repl_pre_readline {
repl_pre_readline()
}
}
}
/// Runs the REPL post-readline hooks (only available with `repl` feature)
#[cfg(feature = "repl")]
pub(crate) fn run_hook_repl_post_readline(&self, line: &str) {
if !self.user_context.run_hook {
return;
}
for hook in &self.hooks {
if let Some(repl_post_readline) = hook.repl_post_readline {
repl_post_readline(line)
}
}
}
/// Runs the REPL receive result hooks (only available with `repl` feature)
#[cfg(feature = "repl")]
pub(crate) fn run_hook_repl_on_receive_result(&self, result: &RenderResult) {
if !self.user_context.run_hook {
return;
}
for hook in &self.hooks {
if let Some(repl_on_receive_result) = hook.repl_on_receive_result {
repl_on_receive_result(result)
}
}
}
/// Runs the REPL panic hooks (only available with `repl` feature)
#[cfg(feature = "repl")]
pub(crate) fn run_hook_repl_on_panic(&self, panic_info: &ProgramPanic) {
if !self.user_context.run_hook {
return;
}
for hook in &self.hooks {
if let Some(repl_on_panic) = hook.repl_on_panic {
repl_on_panic(panic_info)
}
}
}
}
impl<C> ProgramHook<C>
where
C: ProgramCollect<Enum = C>,
{
/// Creates a new empty hook set with no handlers.
pub fn empty() -> Self {
Self {
begin: None,
pre_dispatch: None,
post_dispatch: None,
pre_chain: None,
post_chain: None,
pre_render: None,
post_render: None,
finish: None,
exec_panic: None,
#[cfg(feature = "repl")]
repl_on_begin: None,
#[cfg(feature = "repl")]
repl_pre_readline: None,
#[cfg(feature = "repl")]
repl_post_readline: None,
#[cfg(feature = "repl")]
repl_on_receive_result: None,
#[cfg(feature = "repl")]
repl_on_panic: None,
}
}
/// Sets the handler for the `begin` event.
pub fn on_begin(mut self, handler: fn()) -> Self {
let _ = self.begin.insert(handler);
self
}
/// Sets the handler for the `pre_dispatch` event.
pub fn on_pre_dispatch(mut self, handler: fn(args: &Vec<String>)) -> Self {
let _ = self.pre_dispatch.insert(handler);
self
}
/// Sets the handler for the `post_dispatch` event.
pub fn on_post_dispatch(mut self, handler: fn(entry: &C)) -> Self {
let _ = self.post_dispatch.insert(handler);
self
}
/// Sets the handler for the `pre_chain` event.
pub fn on_pre_chain(mut self, handler: fn(input: &C, raw: &dyn Any)) -> Self {
let _ = self.pre_chain.insert(handler);
self
}
/// Sets the handler for the `post_chain` event.
pub fn on_post_chain(mut self, handler: fn(output: &AnyOutput<C>)) -> Self {
let _ = self.post_chain.insert(handler);
self
}
/// Sets the handler for the `pre_render` event.
pub fn on_pre_render(mut self, handler: fn(input: &C, raw: &dyn Any)) -> Self {
let _ = self.pre_render.insert(handler);
self
}
/// Sets the handler for the `post_render` event.
pub fn on_post_render(mut self, handler: fn(result: &RenderResult)) -> Self {
let _ = self.post_render.insert(handler);
self
}
/// Sets the handler for the `finish` event.
pub fn on_finish(mut self, handler: fn() -> i32) -> Self {
let _ = self.finish.insert(handler);
self
}
/// Sets the handler for the `exec_panic` event.
pub fn on_exec_panic(mut self, handler: fn(&ProgramPanic)) -> Self {
let _ = self.exec_panic.insert(handler);
self
}
/// Sets the handler for the REPL begin event (only available with `repl` feature).
#[cfg(feature = "repl")]
pub fn on_repl_begin(mut self, handler: fn()) -> Self {
let _ = self.repl_on_begin.insert(handler);
self
}
/// Sets the handler for the REPL pre-readline event (only available with `repl` feature).
/// This hook runs after `on_repl_begin` but before reading the next input line.
#[cfg(feature = "repl")]
pub fn on_repl_pre_readline(mut self, handler: fn()) -> Self {
let _ = self.repl_pre_readline.insert(handler);
self
}
/// Sets the handler for the REPL post-readline event (only available with `repl` feature).
/// This hook runs after reading a line of input and receives the line as a `&str`.
#[cfg(feature = "repl")]
pub fn on_repl_post_readline(mut self, handler: fn(line: &str)) -> Self {
let _ = self.repl_post_readline.insert(handler);
self
}
/// Sets the handler for the REPL receive result event (only available with `repl` feature).
#[cfg(feature = "repl")]
pub fn on_repl_receive_result(mut self, handler: fn(result: &RenderResult)) -> Self {
let _ = self.repl_on_receive_result.insert(handler);
self
}
/// Sets the handler for the REPL panic event (only available with `repl` feature).
#[cfg(feature = "repl")]
pub fn on_repl_panic(mut self, handler: fn(panic: &ProgramPanic)) -> Self {
let _ = self.repl_on_panic.insert(handler);
self
}
}
|