diff options
| author | Weicao-CatilGrass <1992414357@qq.com> | 2026-05-23 23:41:04 +0800 |
|---|---|---|
| committer | Weicao-CatilGrass <1992414357@qq.com> | 2026-05-23 23:49:34 +0800 |
| commit | 0a2ef958c0dca21d19e4ffc38ba5a7c4078e182a (patch) | |
| tree | c82fc4242ed393b132ba514eb434d722e7d9c387 /examples/example-hook/src | |
| parent | ccab1940c019dfbfb7dfcbbe4cb927258933755f (diff) | |
Rework examples and add entry macro for testing
Diffstat (limited to 'examples/example-hook/src')
| -rw-r--r-- | examples/example-hook/src/main.rs | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/examples/example-hook/src/main.rs b/examples/example-hook/src/main.rs new file mode 100644 index 0000000..373c76d --- /dev/null +++ b/examples/example-hook/src/main.rs @@ -0,0 +1,71 @@ +//! Example Hook +//! +//! > This example demonstrates how to use Mingling's hook system to obtain debugging information during program execution +//! +//! Run: +//! ```base +//! cargo run --manifest-path examples/example-hook/Cargo.toml --quiet -- greet Alice +//! ``` +//! +//! Output: +//! ```plaintext +//! [DEBUG] Program is begin +//! [DEBUG] Pre dispatch: ["greet", "Alice"] +//! [DEBUG] Post dispatch: EntryGreet +//! [DEBUG] Pre chain: EntryGreet +//! [DEBUG] Post chain: ResultName +//! [DEBUG] Pre render: ResultName +//! [DEBUG] Post render +//! [DEBUG] Program end +//! Hello, Alice! +//! ``` + +use mingling::{hook::ProgramHook, prelude::*}; + +dispatcher!("greet", CMDGreet => EntryGreet); + +fn main() { + let mut program = ThisProgram::new(); + + // --------- IMPORTANT --------- + program.with_hook( + ProgramHook::<ThisProgram>::empty() + .on_begin(|| println!("[DEBUG] Program is begin")) + .on_pre_dispatch(|args| println!("[DEBUG] Pre dispatch: {:?}", args)) + .on_post_dispatch(|c: &_| println!("[DEBUG] Post dispatch: {:?}", c)) + .on_pre_chain(|c: &_, _| { + println!("[DEBUG] Pre chain: {}", c); + }) + .on_post_chain(|any_output| println!("[DEBUG] Post chain: {}", any_output.member_id)) + .on_finish(|| { + println!("[DEBUG] Loop end"); + 0 // Override exit code + }) + .on_pre_render(|c: &_, _| println!("[DEBUG] Pre render: {}", c)) + .on_post_render(|_| println!("[DEBUG] Post render")), + ); + // --------- IMPORTANT --------- + + program.with_dispatcher(CMDGreet); + program.exec_and_exit(); +} + +pack!(ResultName = String); + +#[chain] +fn handle_greet(args: EntryGreet) -> Next { + let name: ResultName = args + .inner + .first() + .cloned() + .unwrap_or_else(|| "World".to_string()) + .into(); + name +} + +#[renderer] +fn render_name(name: ResultName) { + r_println!("Hello, {}!", *name); +} + +gen_program!(); |
