aboutsummaryrefslogtreecommitdiff
path: root/examples/example-setup/src/main.rs
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-08-17 02:29:30 +0800
committer魏曹先生 <1992414357@qq.com>2026-08-17 02:39:56 +0800
commit6980fbf2f9fb4c599d8dc6ff549a8b3288eb24e9 (patch)
treebc7049698955d7a40706ea691fd5bab526a6b5e5 /examples/example-setup/src/main.rs
parent4c191b2b46a67a0dda6e9a867b40f45156af4f9c (diff)
refactor!: remove dynamic dispatcher registration API
Dispatchers are now always registered at compile time, removing the `with_dispatcher` / `with_dispatchers` methods and the `PathfinderConfig` API. The `dispatch_tree` feature now only controls the matching strategy (trie vs linear list).
Diffstat (limited to 'examples/example-setup/src/main.rs')
-rw-r--r--examples/example-setup/src/main.rs67
1 files changed, 55 insertions, 12 deletions
diff --git a/examples/example-setup/src/main.rs b/examples/example-setup/src/main.rs
index bf610ca..b497054 100644
--- a/examples/example-setup/src/main.rs
+++ b/examples/example-setup/src/main.rs
@@ -1,8 +1,28 @@
//! Example Setup
//!
-//! > This example demonstrates how to build a custom Setup for modular management of project components
+//! > This example demonstrates how to build a custom Setup that encapsulates a
+//! > group of related resources and registers them with `with_resource`.
use mingling::{Program, macros::program_setup, prelude::*};
+use std::io::Write;
+
+// A group of related resources — here, the demo app's identity.
+// Resource types are plain structs: any `Default + Clone + Send + Sync` type
+// can be used as a resource, and it is identified by its type.
+#[derive(Default, Clone)]
+struct ResAppName {
+ name: String,
+}
+
+#[derive(Default, Clone)]
+struct ResAppVersion {
+ version: String,
+}
+
+#[derive(Default, Clone)]
+struct ResGreetingPrefix {
+ prefix: String,
+}
fn main() {
let mut program = ThisProgram::new();
@@ -17,21 +37,44 @@ fn main() {
// --------- IMPORTANT ---------
// Define `CustomSetup` (inferred from `custom_setup`)
-// Package part of the program construction logic into this type for modular management
+// Package part of the program construction logic into this type for modular
+// management — e.g. register a group of related resources here.
#[program_setup]
fn custom_setup(program: &mut Program<ThisProgram>) {
- program.with_dispatcher(CMD1);
- program.with_dispatcher(CMD2);
- program.with_dispatcher(CMD3);
- program.with_dispatcher(CMD4);
- program.with_dispatcher(CMD5);
+ program.with_resource(ResAppName {
+ name: "mingling".to_string(),
+ });
+ program.with_resource(ResAppVersion {
+ version: "0.5.0".to_string(),
+ });
+ program.with_resource(ResGreetingPrefix {
+ prefix: "Hello".to_string(),
+ });
}
// --------- IMPORTANT ---------
-dispatcher!("1", CMD1 => Entry1);
-dispatcher!("2", CMD2 => Entry2);
-dispatcher!("3", CMD3 => Entry3);
-dispatcher!("4", CMD4 => Entry4);
-dispatcher!("5", CMD5 => Entry5);
+dispatcher!("greet", CMDGreet => EntryGreet);
+
+pack!(ResultGreeting = String);
+
+/// Chain: reads the `ResAppName` and `ResAppVersion` resources.
+#[chain]
+fn handle_greet(args: EntryGreet, app: &ResAppName, version: &ResAppVersion) -> Next {
+ let who = args
+ .inner
+ .first()
+ .cloned()
+ .unwrap_or_else(|| "World".to_string());
+ let greeting: ResultGreeting = format!("{} from {} v{}", who, app.name, version.version).into();
+ greeting.into()
+}
+
+/// Renderer: injects the `ResGreetingPrefix` resource to decorate the output.
+#[renderer]
+fn render_greet(greeting: ResultGreeting, prefix: &ResGreetingPrefix) -> RenderResult {
+ let mut render_result = RenderResult::new();
+ writeln!(render_result, "{}, {}!", prefix.prefix, *greeting).ok();
+ render_result
+}
gen_program!();