aboutsummaryrefslogtreecommitdiff
path: root/mingling/src/setups/exit_code.rs
diff options
context:
space:
mode:
authorWeicao-CatilGrass <1992414357@qq.com>2026-05-13 07:28:02 +0800
committerWeicao-CatilGrass <1992414357@qq.com>2026-05-13 07:28:02 +0800
commit95c6b979ca399671eed8bf9c72f53cfe5d46f431 (patch)
tree8225b39bee4ad3b7ebc8a36ab978d1329f667eb8 /mingling/src/setups/exit_code.rs
parentfef888b75b2544765aa06808c14490a2af827313 (diff)
Migrate exit code control to resource-based system
Diffstat (limited to 'mingling/src/setups/exit_code.rs')
-rw-r--r--mingling/src/setups/exit_code.rs40
1 files changed, 40 insertions, 0 deletions
diff --git a/mingling/src/setups/exit_code.rs b/mingling/src/setups/exit_code.rs
new file mode 100644
index 0000000..3267f85
--- /dev/null
+++ b/mingling/src/setups/exit_code.rs
@@ -0,0 +1,40 @@
+use std::marker::PhantomData;
+
+use mingling_core::{ProgramCollect, hook::ProgramHook, setup::ProgramSetup, this};
+
+use crate::res::ExitCode;
+
+/// 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<C> {
+ _collect: PhantomData<C>,
+}
+
+impl<C> Default for ExitCodeSetup<C>
+where
+ C: ProgramCollect<Enum = C> + 'static,
+{
+ fn default() -> Self {
+ Self {
+ _collect: Default::default(),
+ }
+ }
+}
+
+impl<C> ProgramSetup<C> for ExitCodeSetup<C>
+where
+ C: ProgramCollect<Enum = C> + 'static,
+{
+ fn setup(&mut self, program: &mut crate::Program<C>) {
+ // Insert resource
+ program.with_resource(ExitCode { exit_code: 0 });
+
+ // Insert hook to override exit code before program ends
+ program.with_hook(ProgramHook::empty().on_finish(|| {
+ let this = this::<C>().res_or_default();
+ *this
+ }));
+ }
+}