aboutsummaryrefslogtreecommitdiff
path: root/examples/example-exit-code/src/main.rs
blob: f138fb3c200cabcfea33a85fb5c6c8b4812cac6a (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
36
37
38
39
40
41
42
//! `Mingling` Example - Exit Code
//!
//! This example demonstrates how to modify the program's exit code using `ExitCodeSetup`.
//! By default, the program exits with code 0. This example shows:
//! 1. Using `dispatcher!` to define an error command,
//! 2. Using `chain!` to handle errors and set a custom exit code via `ProgramExitCode`,
//! 3. Using `renderer!` to print an error message.
//!
//! # How to Run
//! ```bash
//! cargo run --manifest-path ./examples/example-exit-code/Cargo.toml -- error
//! ```

use mingling::{
    macros::{chain, dispatcher, gen_program, pack, r_println, renderer},
    res::ExitCode,
    setup::ExitCodeSetup,
    this,
};

fn main() {
    let mut program = ThisProgram::new();
    program.with_dispatcher(ErrorCommand);
    program.with_setup(ExitCodeSetup::<ThisProgram>::default());
    program.exec();
}

dispatcher!("error", ErrorCommand => ErrorEntry);
pack!(ResultError = ());

#[chain]
fn handle_error_entry(_prev: ErrorEntry) -> NextProcess {
    this::<ThisProgram>().modify_res(|r: &mut ExitCode| r.exit_code = 1);
    return ResultError::default();
}

#[renderer]
fn render_error(_prev: ResultError) {
    r_println!("Error!");
}

gen_program!();