aboutsummaryrefslogtreecommitdiff
path: root/mingling/src/res/exit_code.rs
blob: b483139361ab43503c7a35c959c875cf48831be5 (plain) (blame)
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
use mingling_core::{ProgramCollect, this};

/// Represents a program exit code.
///
/// This struct should be used together with `ExitCodeSetup`:
/// ```ignore
/// program.with_setup(ExitCodeSetup::default());
/// ```
/// The exit code is stored globally per `ProgramCollect` type and can be
/// retrieved via [`exit_code()`] or updated via [`update_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,
    }
}