aboutsummaryrefslogtreecommitdiff
path: root/mingling
diff options
context:
space:
mode:
authorWeicao-CatilGrass <1992414357@qq.com>2026-05-13 07:34:45 +0800
committerWeicao-CatilGrass <1992414357@qq.com>2026-05-13 07:40:00 +0800
commit2588933ebc500f439ebef075fef1064b0c5f0bcb (patch)
tree595e55d7aa634bf7f730acaab7bcf243191c519a /mingling
parent566b43cc15fc8a0bc7f962e05908486dbf53a7d0 (diff)
Add convenient `update_exit_code` and `exit_code` helper functions
Diffstat (limited to 'mingling')
-rw-r--r--mingling/src/example_docs.rs5
-rw-r--r--mingling/src/res/exit_code.rs24
2 files changed, 26 insertions, 3 deletions
diff --git a/mingling/src/example_docs.rs b/mingling/src/example_docs.rs
index 09e84a8..1669344 100644
--- a/mingling/src/example_docs.rs
+++ b/mingling/src/example_docs.rs
@@ -371,9 +371,8 @@ pub mod example_dispatch_tree {}
/// ```ignore
/// use mingling::{
/// macros::{chain, dispatcher, gen_program, pack, r_println, renderer},
-/// res::ExitCode,
+/// res::update_exit_code,
/// setup::ExitCodeSetup,
-/// this,
/// };
///
/// fn main() {
@@ -388,7 +387,7 @@ pub mod example_dispatch_tree {}
///
/// #[chain]
/// fn handle_error_entry(_prev: ErrorEntry) -> NextProcess {
-/// this::<ThisProgram>().modify_res(|r: &mut ExitCode| r.exit_code = 1);
+/// update_exit_code::<ThisProgram>(1);
/// return ResultError::default();
/// }
///
diff --git a/mingling/src/res/exit_code.rs b/mingling/src/res/exit_code.rs
index 388a6f1..f4d4238 100644
--- a/mingling/src/res/exit_code.rs
+++ b/mingling/src/res/exit_code.rs
@@ -1,4 +1,28 @@
+use mingling_core::{ProgramCollect, this};
+
+/// Represents a program exit code.
#[derive(Debug, Default, Clone, Copy)]
pub struct ExitCode {
+ /// The numeric exit code value.
pub exit_code: i32,
}
+
+/// Updates the globally stored exit code for the given `ProgramCollect` type.
+pub fn update_exit_code<C>(exit_code: i32)
+where
+ C: ProgramCollect<Enum = C> + 'static,
+{
+ this::<C>().modify_res(|e: &mut ExitCode| e.exit_code = exit_code);
+}
+
+/// Retrieves the globally stored exit code for the given `ProgramCollect` type.
+/// Returns `0` if no exit code has been set.
+pub fn exit_code<C>() -> i32
+where
+ C: ProgramCollect<Enum = C> + 'static,
+{
+ match this::<C>().res::<ExitCode>() {
+ Some(e) => e.exit_code,
+ None => 0,
+ }
+}