aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-05-24 17:06:54 +0800
committer魏曹先生 <1992414357@qq.com>2026-05-24 17:06:54 +0800
commit60e70f5320b2abdb38a2349c18e5bffcfea37ca7 (patch)
tree3402af0a2822255c1c3f9c77affe6da81c9d1279 /examples
parent11adad7db1b6202d5366527902c3f0a9fb90654f (diff)
Add implicit dispatcher macro with auto-derived names
Diffstat (limited to 'examples')
-rw-r--r--examples/example-general-renderer/src/main.rs6
-rw-r--r--examples/example-implicit-dispatcher/Cargo.lock76
-rw-r--r--examples/example-implicit-dispatcher/Cargo.toml8
-rw-r--r--examples/example-implicit-dispatcher/src/main.rs23
-rw-r--r--examples/example-repl-basic/src/main.rs27
5 files changed, 123 insertions, 17 deletions
diff --git a/examples/example-general-renderer/src/main.rs b/examples/example-general-renderer/src/main.rs
index 5f74815..3ba4433 100644
--- a/examples/example-general-renderer/src/main.rs
+++ b/examples/example-general-renderer/src/main.rs
@@ -21,13 +21,13 @@ use mingling::prelude::*;
use mingling::{Groupped, parser::Picker, setup::GeneralRendererSetup};
use serde::Serialize;
-dispatcher!("render", RenderCommand => RenderCommandEntry);
+dispatcher!("render", CMDRender => EntryRender);
fn main() {
let mut program = ThisProgram::new();
// Add `GeneralRendererSetup` to receive user input `--json` `--yaml` parameters
program.with_setup(GeneralRendererSetup);
- program.with_dispatcher(RenderCommand);
+ program.with_dispatcher(CMDRender);
program.exec();
}
@@ -53,7 +53,7 @@ struct Info {
// --------- IMPORTANT ---------
#[chain]
-fn parse_render(prev: RenderCommandEntry) -> Next {
+fn parse_render(prev: EntryRender) -> Next {
let (name, age) = Picker::new(prev.inner)
.pick::<String>(())
.pick::<i32>(())
diff --git a/examples/example-implicit-dispatcher/Cargo.lock b/examples/example-implicit-dispatcher/Cargo.lock
new file mode 100644
index 0000000..7bd56a7
--- /dev/null
+++ b/examples/example-implicit-dispatcher/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-implicit-dispatcher"
+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.1.9"
+dependencies = [
+ "mingling_core",
+ "mingling_macros",
+]
+
+[[package]]
+name = "mingling_core"
+version = "0.1.9"
+dependencies = [
+ "just_fmt",
+]
+
+[[package]]
+name = "mingling_macros"
+version = "0.1.9"
+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.117"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99"
+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-implicit-dispatcher/Cargo.toml b/examples/example-implicit-dispatcher/Cargo.toml
new file mode 100644
index 0000000..db6fdab
--- /dev/null
+++ b/examples/example-implicit-dispatcher/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "example-implicit-dispatcher"
+version = "0.1.0"
+edition = "2024"
+
+[dependencies.mingling]
+path = "../../mingling"
+features = ["extra_macros"]
diff --git a/examples/example-implicit-dispatcher/src/main.rs b/examples/example-implicit-dispatcher/src/main.rs
new file mode 100644
index 0000000..3dc7f83
--- /dev/null
+++ b/examples/example-implicit-dispatcher/src/main.rs
@@ -0,0 +1,23 @@
+//! Example Implicit Dispatcher
+//!
+//! > This example demonstrates how to use the implicit `dispatcher!` definition syntax enabled by `extra_macros`
+
+use mingling::prelude::*;
+
+// When using implicit syntax, the entry and dispatcher names will be automatically derived
+dispatcher!("remote.add" /*, CMDRemoteAdd => EntryRemoteAdd */);
+dispatcher!("remote.remove", CMDRemoteRemove => EntryRemoteRemove);
+
+fn main() {
+ let mut program = ThisProgram::new();
+
+ // --------- IMPORTANT ---------
+ program.with_dispatcher(CMDRemoteAdd);
+ // ^^^^^^^^^^^^\_ CMDRemoteAdd is implicitly created
+ // --------- IMPORTANT ---------
+
+ program.with_dispatcher(CMDRemoteRemove);
+ program.exec_and_exit();
+}
+
+gen_program!();
diff --git a/examples/example-repl-basic/src/main.rs b/examples/example-repl-basic/src/main.rs
index f02c2f8..f2c871e 100644
--- a/examples/example-repl-basic/src/main.rs
+++ b/examples/example-repl-basic/src/main.rs
@@ -8,11 +8,10 @@
//! ```
use mingling::{
- REPL,
hook::ProgramHook,
prelude::*,
setup::{BasicREPLOutputSetup, BasicREPLPromptSetup, BasicREPLReadlineSetup},
- this,
+ this, REPL,
};
use std::{env::current_dir, path::PathBuf};
@@ -37,10 +36,10 @@ fn main() {
program.with_resource(ResCurrentDir::default());
// Dispatchers
- program.with_dispatcher(ChangeDirectoryCommand);
- program.with_dispatcher(ListCommand);
- program.with_dispatcher(ExitCommand);
- program.with_dispatcher(ClearCommand);
+ program.with_dispatcher(CMDCd);
+ program.with_dispatcher(CMDLs);
+ program.with_dispatcher(CMDExit);
+ program.with_dispatcher(CMDClear);
// Setups
// Enable basic std::io::stdin().read_line(&mut input)
@@ -78,10 +77,10 @@ fn main() {
pack!(ErrorDirectoryNotExist = PathBuf);
// Create commands: cd ls exit
-dispatcher!("cd", ChangeDirectoryCommand => ChangeDirectoryEntry);
-dispatcher!("ls", ListCommand => ListEntry);
-dispatcher!("exit", ExitCommand => ExitEntry);
-dispatcher!("clear", ClearCommand => ClearEntry);
+dispatcher!("cd", CMDCd => EntryCd);
+dispatcher!("ls", CMDLs => EntryLs);
+dispatcher!("exit", CMDExit => EntryExit);
+dispatcher!("clear", CMDClear => EntryClear);
// Define data needed for the cd command's execution phase
pack!(StateChangeDirectory = String);
@@ -91,7 +90,7 @@ pack!(ResultList = Vec<String>);
// Parse cd command arguments
#[chain]
-fn parse_cd_args(prev: ChangeDirectoryEntry) -> Next {
+fn parse_cd_args(prev: EntryCd) -> Next {
let join = prev.pick(()).unpack();
StateChangeDirectory::new(join)
}
@@ -115,7 +114,7 @@ fn handle_cd(prev: StateChangeDirectory, current_dir: &mut ResCurrentDir) -> Nex
// Get directory contents via the CurrentDir resource
#[chain]
-fn handle_ls(_prev: ListEntry, current_dir: &ResCurrentDir) -> Next {
+fn handle_ls(_prev: EntryLs, current_dir: &ResCurrentDir) -> Next {
let dir = &current_dir.dir;
let entries: Vec<String> = std::fs::read_dir(dir)
.into_iter()
@@ -145,7 +144,7 @@ fn render_list(list: ResultList) {
// Handle exit command event
#[chain]
fn handle_exit(
- _prev: ExitEntry,
+ _prev: EntryExit,
repl: &mut REPL, // Import REPL resource, registered in `exec_repl`, usable directly
) {
// Set the REPL exit flag; REPL will exit after this loop iteration
@@ -154,7 +153,7 @@ fn handle_exit(
// Handle clear command event
#[chain]
-fn handle_clear(_prev: ClearEntry) {
+fn handle_clear(_prev: EntryClear) {
// Clear the terminal screen
print!("\x1B[2J\x1B[1;1H");
}