diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/example-outside-type/src/main.rs | 23 | ||||
| -rw-r--r-- | examples/test-examples.toml | 5 |
2 files changed, 27 insertions, 1 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 |
