diff options
| author | Weicao-CatilGrass <1992414357@qq.com> | 2026-05-13 07:28:02 +0800 |
|---|---|---|
| committer | Weicao-CatilGrass <1992414357@qq.com> | 2026-05-13 07:28:02 +0800 |
| commit | 95c6b979ca399671eed8bf9c72f53cfe5d46f431 (patch) | |
| tree | 8225b39bee4ad3b7ebc8a36ab978d1329f667eb8 /examples/example-exit-code | |
| parent | fef888b75b2544765aa06808c14490a2af827313 (diff) | |
Migrate exit code control to resource-based system
Diffstat (limited to 'examples/example-exit-code')
| -rw-r--r-- | examples/example-exit-code/Cargo.lock | 105 | ||||
| -rw-r--r-- | examples/example-exit-code/Cargo.toml | 7 | ||||
| -rw-r--r-- | examples/example-exit-code/src/main.rs | 42 |
3 files changed, 154 insertions, 0 deletions
diff --git a/examples/example-exit-code/Cargo.lock b/examples/example-exit-code/Cargo.lock new file mode 100644 index 0000000..efe8f6e --- /dev/null +++ b/examples/example-exit-code/Cargo.lock @@ -0,0 +1,105 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "example-exit-code" +version = "0.1.0" +dependencies = [ + "mingling", +] + +[[package]] +name = "just_fmt" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5454cda0d57db59778608d7a47bff5b16c6705598265869fb052b657f66cf05e" + +[[package]] +name = "mingling" +version = "0.1.8" +dependencies = [ + "mingling_core", + "mingling_macros", +] + +[[package]] +name = "mingling_core" +version = "0.1.8" +dependencies = [ + "just_fmt", + "once_cell", + "thiserror", +] + +[[package]] +name = "mingling_macros" +version = "0.1.8" +dependencies = [ + "just_fmt", + "once_cell", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "2.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4288b5bcbc7920c07a1149a35cf9590a2aa808e0bc1eafaade0b80947865fbc4" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebc4ee7f67670e9b64d05fa4253e753e016c6c95ff35b89b7941d6b856dec1d5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" diff --git a/examples/example-exit-code/Cargo.toml b/examples/example-exit-code/Cargo.toml new file mode 100644 index 0000000..a6c9c7c --- /dev/null +++ b/examples/example-exit-code/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "example-exit-code" +version = "0.1.0" +edition = "2024" + +[dependencies] +mingling = { path = "../../mingling" } diff --git a/examples/example-exit-code/src/main.rs b/examples/example-exit-code/src/main.rs new file mode 100644 index 0000000..f138fb3 --- /dev/null +++ b/examples/example-exit-code/src/main.rs @@ -0,0 +1,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!(); |
