diff options
Diffstat (limited to 'examples/example-outside-type')
| -rw-r--r-- | examples/example-outside-type/Cargo.lock | 76 | ||||
| -rw-r--r-- | examples/example-outside-type/Cargo.toml | 12 | ||||
| -rw-r--r-- | examples/example-outside-type/page.toml | 10 | ||||
| -rw-r--r-- | examples/example-outside-type/src/main.rs | 71 |
4 files changed, 169 insertions, 0 deletions
diff --git a/examples/example-outside-type/Cargo.lock b/examples/example-outside-type/Cargo.lock new file mode 100644 index 0000000..1ca10d7 --- /dev/null +++ b/examples/example-outside-type/Cargo.lock @@ -0,0 +1,76 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "example-outside-type" +version = "0.1.0" +dependencies = [ + "mingling", +] + +[[package]] +name = "just_fmt" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5454cda0d57db59778608d7a47bff5b16c6705598265869fb052b657f66cf05e" + +[[package]] +name = "mingling" +version = "0.2.0" +dependencies = [ + "mingling_core", + "mingling_macros", +] + +[[package]] +name = "mingling_core" +version = "0.2.0" +dependencies = [ + "just_fmt", +] + +[[package]] +name = "mingling_macros" +version = "0.2.0" +dependencies = [ + "just_fmt", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.106" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "2.0.118" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9ae57f904213ebb649ce6895b8a66c66f0203b9319718f69a5612a065b1422" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" diff --git a/examples/example-outside-type/Cargo.toml b/examples/example-outside-type/Cargo.toml new file mode 100644 index 0000000..e2ca5ba --- /dev/null +++ b/examples/example-outside-type/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "example-outside-type" +version = "0.1.0" +edition = "2024" + +[dependencies.mingling] +path = "../../mingling" +features = [ + "extra_macros", +] + +[workspace] diff --git a/examples/example-outside-type/page.toml b/examples/example-outside-type/page.toml new file mode 100644 index 0000000..41e543f --- /dev/null +++ b/examples/example-outside-type/page.toml @@ -0,0 +1,10 @@ +[example] +id = "example-outside-type" +name = "Outside Type" +icon = "🆕" +category = "advanced" +desc = """ +Demonstrates how to use the `group!()` macro to convert an external type into a type recognizable by Mingling +""" +tags = ["group!", "extra_macros"] +files = ["src/main.rs", "Cargo.toml"] 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!(); |
