diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-04-06 20:19:43 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-04-06 20:19:43 +0800 |
| commit | 721b6f9f608977b938dbc9b847c9221370b5ee15 (patch) | |
| tree | 95483051d50d183357a097c41a678bad14c657c0 /examples/example-basic/src | |
| parent | e68e1230d4157c32592900372699411c7151b4e5 (diff) | |
Add workspace configuration and examples
Diffstat (limited to 'examples/example-basic/src')
| -rw-r--r-- | examples/example-basic/src/main.rs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/examples/example-basic/src/main.rs b/examples/example-basic/src/main.rs new file mode 100644 index 0000000..050bfd4 --- /dev/null +++ b/examples/example-basic/src/main.rs @@ -0,0 +1,51 @@ +//! `Mingling` Example - Basic +//! +//! # How to Run +//! ```bash +//! cargo run --manifest-path ./examples/example-basic/Cargo.toml -- hello World +//! ``` + +use mingling::{ + macros::{chain, dispatcher, gen_program, pack, r_println, renderer}, + marker::NextProcess, +}; + +// Define dispatcher `HelloCommand`, directing subcommand "hello" to `HelloEntry` +dispatcher!("hello", HelloCommand => HelloEntry); + +#[tokio::main] +async fn main() { + // Create program + let mut program = DefaultProgram::new(); + + // Add dispatcher `HelloCommand` + program.with_dispatcher(HelloCommand); + + // Run program + program.exec().await; +} + +// Register wrapper type `Hello`, setting inner to `String` +pack!(Hello = String); + +// Register chain to `DefaultProgram`, handling logic from `HelloEntry` +#[chain] +async fn parse_name(prev: HelloEntry) -> NextProcess { + // Extract string from `HelloEntry` as argument + let name = prev.first().cloned().unwrap_or_else(|| "World".to_string()); + + // Build `Hello` type and route to renderer + Hello::new(name).to_render() +} + +// Register renderer to `DefaultProgram`, handling rendering of `Hello` +#[renderer] +fn render_hello_who(prev: Hello) { + // Print message + r_println!("Hello, {}!", *prev); + + // Program ends here +} + +// Generate program, default is `DefaultProgram` +gen_program!(); |
