From 6f3158896977a60c7c886c0960c8f9c0f2f0b2d9 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Wed, 15 Apr 2026 13:22:06 +0800 Subject: Add documentation for Dispatcher, Chain, and Renderer --- docs/pages/2-basic/4-chain.md | 71 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) (limited to 'docs/pages/2-basic/4-chain.md') diff --git a/docs/pages/2-basic/4-chain.md b/docs/pages/2-basic/4-chain.md index e9676c2..87a50f2 100644 --- a/docs/pages/2-basic/4-chain.md +++ b/docs/pages/2-basic/4-chain.md @@ -5,6 +5,77 @@ --- +## Intro + +Like `Dispatcher`, `Chain` is also a core concept in building the entire **Mingling** framework. It is used to receive a dispatch of one type and convert it into another type. + +```rust +dispatcher!("hello", + HelloCommand => HelloEntry); + +// Define intermediate type ParsedHello, internally a String +pack!(ParsedHello = String); + +// Define chain parse_hello (expands to ParseHello) +// Declare conversion from HelloEntry +#[chain] +async fn parse_hello(prev: HelloEntry) -> NextProcess { + // Take the inner reference of HelloEntry + let args = &*prev; + + // Extract the first argument, use default value "World" + // if it doesn't exist + let first = args.first().cloned().unwrap_or_else(|| "World".to_string()); + + // Pack the extracted argument into ParsedHello and + // dispatch to the next chain + ParsedHello::new(first).to_chain() +} +``` + +> **About NextProcess** +> +> `NextProcess` is a marker type in **Mingling**, from `mingling::marker`. +> +> It serves no functional purpose other than to simplify the declaration of chain functions. After the `chain!` macro expands, `NextProcess` will be replaced with `mingling::ChainProcess`. + +## Manual Impl + +> ⚠️ WARNING +> +> The following content is not yet fully implemented; currently, only the `chain!` macro is allowed for implementation. + +You can also manually implement the basic `Chain` for finer control. + +However, please note that within the `chain!` macro, a `register_type!` macro is executed. This macro does not expand to any content; it only informs the `gen_program` context that this type exists. + +```rust +dispatcher!("hello", + HelloCommand => HelloEntry); + +pack!(ParsedHello = String); + +struct ParseHello; +impl Chain for ParseHello { + type Previous = HelloEntry; + async fn proc(prev: Self::Previous) + -> ChainProcess + { + let args = &*prev; + let first = args + .first() + .cloned() + .unwrap_or_else(|| + "World".to_string() + ); + ParsedHello::new(first).to_chain() + } +} + +// Register HelloEntry to the context and +// assign an ID for it +register_type!(HelloEntry); +``` ## 💡 Next Page > **Basic Component** - Renderer [Go](./pages/2-basic/5-renderer) -- cgit