diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-05-18 17:25:29 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-05-18 17:26:37 +0800 |
| commit | da5a750965dbec5a2c003faa8fb9f1dd110ccce8 (patch) | |
| tree | b793769ec40494c1ab056c65748c3393b5a849ed /mingling_core/src/program/hook.rs | |
| parent | ab7c5785fb290541ad4361c0d46241817c3ff5f9 (diff) | |
Implement REPL execution with hooks and argument splitting
Diffstat (limited to 'mingling_core/src/program/hook.rs')
| -rw-r--r-- | mingling_core/src/program/hook.rs | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/mingling_core/src/program/hook.rs b/mingling_core/src/program/hook.rs index 19f7baf..448949f 100644 --- a/mingling_core/src/program/hook.rs +++ b/mingling_core/src/program/hook.rs @@ -33,6 +33,18 @@ where /// 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 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> @@ -158,6 +170,48 @@ where } 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 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> @@ -176,6 +230,12 @@ where post_render: None, finish: None, exec_panic: None, + #[cfg(feature = "repl")] + repl_on_begin: None, + #[cfg(feature = "repl")] + repl_on_receive_result: None, + #[cfg(feature = "repl")] + repl_on_panic: None, } } @@ -232,4 +292,25 @@ where 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 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 + } } |
