From b75773a1177c6fe006bcd129b481ff42ecc3ec0e Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Tue, 23 Jun 2026 01:41:46 +0800 Subject: Add IO error example with type alias in group macro --- examples/example-outside-type/src/main.rs | 23 ++++++++++++++++++++++- examples/test-examples.toml | 5 +++++ mingling/src/example_docs.rs | 23 ++++++++++++++++++++++- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/examples/example-outside-type/src/main.rs b/examples/example-outside-type/src/main.rs index 6cdd672..7cf4d40 100644 --- a/examples/example-outside-type/src/main.rs +++ b/examples/example-outside-type/src/main.rs @@ -8,18 +8,26 @@ //! ```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 +//! cargo run --manifest-path examples/example-outside-type/Cargo.toml --quiet -- error //! ``` //! //! Output: //! ```plaintext //! Parsed number: 42 //! Parse error: invalid digit found in string +//! IO_ERROR: Error //! ``` use mingling::{macros::group, prelude::*}; -use std::num::ParseIntError; +use std::{io::ErrorKind::Other, num::ParseIntError}; dispatcher!("parse"); +dispatcher!("error"); + +#[chain] +fn handle_entry_error(_args: EntryError) -> Next { + std::io::Error::new(Other, "Error").to_render() +} // --------- IMPORTANT --------- // You can directly use the `group!` macro to define outside types as types @@ -28,6 +36,10 @@ dispatcher!("parse"); // / // vvvvvvvvvvvvv group!(ParseIntError); +group!(ErrorIo = std::io::Error); +// ^^^^^^^^^^^^^^^^^^^^^^^^ +// \_____________ For types whose names may cause ambiguity, +// you can use this syntax to create an alias simultaneously // --------- IMPORTANT --------- pack!(ParsedNumber = i32); @@ -62,9 +74,18 @@ fn render_parse_error(err: ParseIntError) { r_println!("Parse error: {}", err); } +/// Renderer for IO errors — using `std::io::Error` registered as `ErrorIo`. +// ________ Must use alias `ErrorIo` here, not bare `std::io::Error` +// / +#[renderer] // vvvvvvv +fn render_error_io(err: ErrorIo) { + r_println!("IO_ERROR: {}", err.to_string()); +} + fn main() { let mut program = ThisProgram::new(); program.with_dispatcher(CMDParse); + program.with_dispatcher(CMDError); program.exec_and_exit(); } diff --git a/examples/test-examples.toml b/examples/test-examples.toml index 038221d..9c25781 100644 --- a/examples/test-examples.toml +++ b/examples/test-examples.toml @@ -8,6 +8,11 @@ command = "parse hello" expect.exit-code = 0 expect.result = "Parse error: invalid digit found in string" +[[test.example-outside-type]] +command = "error" +expect.exit-code = 0 +expect.result = "IO_ERROR: Error" + [[test.example-lazy-resources]] command = "none" expect.exit-code = 0 diff --git a/mingling/src/example_docs.rs b/mingling/src/example_docs.rs index caa12ae..3171c22 100644 --- a/mingling/src/example_docs.rs +++ b/mingling/src/example_docs.rs @@ -1572,12 +1572,14 @@ pub mod example_lazy_resources {} /// ```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 +/// cargo run --manifest-path examples/example-outside-type/Cargo.toml --quiet -- error /// ``` /// /// Output: /// ```plaintext /// Parsed number: 42 /// Parse error: invalid digit found in string +/// IO_ERROR: Error /// ``` /// /// Source code (./Cargo.toml) @@ -1599,9 +1601,15 @@ pub mod example_lazy_resources {} /// Source code (./src/main.rs) /// ```ignore /// use mingling::{macros::group, prelude::*}; -/// use std::num::ParseIntError; +/// use std::{io::ErrorKind::Other, num::ParseIntError}; /// /// dispatcher!("parse"); +/// dispatcher!("error"); +/// +/// #[chain] +/// fn handle_entry_error(_args: EntryError) -> Next { +/// std::io::Error::new(Other, "Error").to_render() +/// } /// /// // --------- IMPORTANT --------- /// // You can directly use the `group!` macro to define outside types as types @@ -1610,6 +1618,10 @@ pub mod example_lazy_resources {} /// // / /// // vvvvvvvvvvvvv /// group!(ParseIntError); +/// group!(ErrorIo = std::io::Error); +/// // ^^^^^^^^^^^^^^^^^^^^^^^^ +/// // \_____________ For types whose names may cause ambiguity, +/// // you can use this syntax to create an alias simultaneously /// // --------- IMPORTANT --------- /// /// pack!(ParsedNumber = i32); @@ -1644,9 +1656,18 @@ pub mod example_lazy_resources {} /// r_println!("Parse error: {}", err); /// } /// +/// /// Renderer for IO errors — using `std::io::Error` registered as `ErrorIo`. +/// // ________ Must use alias `ErrorIo` here, not bare `std::io::Error` +/// // / +/// #[renderer] // vvvvvvv +/// fn render_error_io(err: ErrorIo) { +/// r_println!("IO_ERROR: {}", err.to_string()); +/// } +/// /// fn main() { /// let mut program = ThisProgram::new(); /// program.with_dispatcher(CMDParse); +/// program.with_dispatcher(CMDError); /// program.exec_and_exit(); /// } /// -- cgit