From 2eba732e17cf71dcf8a54b428a1c5e344582c2c2 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Sat, 9 May 2026 17:37:37 +0800 Subject: Add exit code control and hook lifecycle features --- mingling_core/src/program/hook.rs | 21 ++++++++----- mingling_core/src/program/setup.rs | 4 +++ .../src/program/setup/exit_code_control.rs | 36 ++++++++++++++++++++++ 3 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 mingling_core/src/program/setup/exit_code_control.rs (limited to 'mingling_core/src/program') diff --git a/mingling_core/src/program/hook.rs b/mingling_core/src/program/hook.rs index d422e12..edbf996 100644 --- a/mingling_core/src/program/hook.rs +++ b/mingling_core/src/program/hook.rs @@ -56,7 +56,18 @@ pub struct ProgramAnonymousHook { pub post_render: Option, /// Executes before the program ends - pub finish: Option, + pub finish: Option i32>, +} + +impl Program +where + C: ProgramCollect, +{ + /// Adds an anonymous hook to the program. The hook will be called at the appropriate + /// lifecycle events, but receives string representations instead of typed references. + pub fn with_hook_anonymous(&mut self, hook: ProgramAnonymousHook) { + self.anonymous_hooks.push(hook); + } } impl Program @@ -69,12 +80,6 @@ where self.hooks.push(hook); } - /// Adds an anonymous hook to the program. The hook will be called at the appropriate - /// lifecycle events, but receives string representations instead of typed references. - pub fn with_hook_anonymous(&mut self, hook: ProgramAnonymousHook) { - self.anonymous_hooks.push(hook); - } - pub(crate) fn run_hook_on_begin(&self) { if !self.user_context.run_hook { return; @@ -342,7 +347,7 @@ impl ProgramAnonymousHook { } /// Sets the handler for the `finish` event. - pub fn on_finish(mut self, handler: fn()) -> Self { + pub fn on_finish(mut self, handler: fn() -> i32) -> Self { let _ = self.finish.insert(handler); self } diff --git a/mingling_core/src/program/setup.rs b/mingling_core/src/program/setup.rs index 28aa49b..289dfee 100644 --- a/mingling_core/src/program/setup.rs +++ b/mingling_core/src/program/setup.rs @@ -3,6 +3,10 @@ use crate::{ProgramCollect, program::Program}; mod basic; pub use basic::*; +#[doc(hidden)] +pub mod exit_code_control; +pub use exit_code_control::ExitCodeSetup; + #[cfg(feature = "general_renderer")] mod general_renderer; diff --git a/mingling_core/src/program/setup/exit_code_control.rs b/mingling_core/src/program/setup/exit_code_control.rs new file mode 100644 index 0000000..0844a24 --- /dev/null +++ b/mingling_core/src/program/setup/exit_code_control.rs @@ -0,0 +1,36 @@ +use std::sync::atomic::{AtomicI32, Ordering}; + +use crate::{ProgramCollect, hook::ProgramAnonymousHook, setup::ProgramSetup}; + +static EXIT_CODE: AtomicI32 = AtomicI32::new(0); + +/// Provides the ability to control the program's exit code, which is returned when the program ends. +/// +/// - Use `mingling::update_exit_code` to update the exit code. +/// - Use `mingling::current_exit_code` to query the current exit code. +pub struct ExitCodeSetup; + +impl ProgramSetup for ExitCodeSetup +where + C: ProgramCollect, +{ + fn setup(&mut self, program: &mut crate::Program) { + program + .with_hook_anonymous(ProgramAnonymousHook::empty().on_finish(|| current_exit_code())); + } +} + +/// Updates the program's exit code. +/// +/// This function sets the value that will be returned when the program exits. +/// The new code will take effect immediately and be used when the program finishes. +pub fn update_exit_code(code: i32) { + EXIT_CODE.store(code, Ordering::SeqCst); +} + +/// Returns the current exit code. +/// +/// This function queries the value that will be returned when the program exits. +pub fn current_exit_code() -> i32 { + EXIT_CODE.load(Ordering::SeqCst) +} -- cgit