diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-06-20 01:30:10 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-06-20 01:30:10 +0800 |
| commit | d3177c386d739a3dc0e06890ad8f14713ef44ee0 (patch) | |
| tree | 98193b025812ee13b1f0f0bb38e1d58fd2558f3d /examples/example-outside-type/src/main.rs | |
| parent | 9d491352d161ee629cc47459537344ba0ea4bb35 (diff) | |
Add `group!` macro for registering external types
Diffstat (limited to 'examples/example-outside-type/src/main.rs')
| -rw-r--r-- | examples/example-outside-type/src/main.rs | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/examples/example-outside-type/src/main.rs b/examples/example-outside-type/src/main.rs new file mode 100644 index 0000000..6cdd672 --- /dev/null +++ b/examples/example-outside-type/src/main.rs @@ -0,0 +1,71 @@ +//! Example: Using the `group!()` Macro to Register Outside Types +//! +//! This example demonstrates how to use the `group!()` macro to make outside +//! types (from `std` or other crates) recognizable by the Mingling framework, +//! without modifying the original type definition. +//! +//! Run: +//! ```bash +//! cargo run --manifest-path examples/example-outside-type/Cargo.toml --quiet -- parse 42 +//! cargo run --manifest-path examples/example-outside-type/Cargo.toml --quiet -- parse hello +//! ``` +//! +//! Output: +//! ```plaintext +//! Parsed number: 42 +//! Parse error: invalid digit found in string +//! ``` + +use mingling::{macros::group, prelude::*}; +use std::num::ParseIntError; + +dispatcher!("parse"); + +// --------- IMPORTANT --------- +// You can directly use the `group!` macro to define outside types as types +// recognizable by Mingling +// _____________ from std::num::ParseIntError +// / +// vvvvvvvvvvvvv +group!(ParseIntError); +// --------- IMPORTANT --------- + +pack!(ParsedNumber = i32); + +/// Parse the first argument as an `i32` +/// +/// On success, routes to `render_number`. +/// On failure, routes to `render_parse_error` via the registered outside type. +#[chain] +fn parse_number(args: EntryParse) -> Next { + let input = args.inner.first().cloned().unwrap_or_default(); + match input.parse::<i32>() { + Ok(num) => ParsedNumber::new(num).to_chain(), + Err(e) => e.to_chain(), + } +} + +/// Renderer for successful parse — displays the parsed integer. +// _____________ Using std::num::ParseIntError as a chain input +// / +#[renderer] // vvvvvvvvvvvv +fn render_number(num: ParsedNumber) { + r_println!("Parsed number: {}", *num); +} + +/// Renderer for parse errors — using the outside `ParseIntError` type. +/// +/// The `ParseIntError` type is registered via `group!` above, so it implements +/// `Groupped<ThisProgram>` and can be used directly in a `#[renderer]` function. +#[renderer] +fn render_parse_error(err: ParseIntError) { + r_println!("Parse error: {}", err); +} + +fn main() { + let mut program = ThisProgram::new(); + program.with_dispatcher(CMDParse); + program.exec_and_exit(); +} + +gen_program!(); |
