1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
use crate::Next;
use crate::ThisProgram;
use mingling::ProgramCollect;
use mingling::macros::metadata;
use mingling::prelude::*;
use std::io::Write;
// Implicit dispatcher form — creates `CMDHello` / `EntryHello` in this module
dispatcher!("hello");
// Creates `CMDDescription` / `EntryDescription` in this module
dispatcher!("desc", CMDDescription => EntryDescription);
/// The metadata type attached to an entry (`DataType`).
#[derive(Debug, PartialEq, Eq)]
pub struct Description {
pub desc: String,
}
/// Attach a `Description` to `EntryHello`.
///
/// - `BindType` = `EntryHello` (the enum variant / entry type)
/// - `DataType` = `Description` (the function's return type)
#[metadata(EntryHello)]
pub fn hello_desc() -> Description {
Description {
desc: "okay".to_string(),
}
}
pack!(ResultName = String);
pack!(DescResult = String);
/// Chain for `hello` — reads the name and produces a `ResultName`.
#[chain]
pub fn handle_hello(args: EntryHello) -> Next {
let name: ResultName = args
.inner
.first()
.cloned()
.unwrap_or_else(|| "World".to_string())
.into();
name.into()
}
/// Chain for `desc` — looks up the metadata bound to `EntryHello`.
#[chain]
pub fn handle_desc(_args: EntryDescription) -> Next {
// --------- IMPORTANT ---------
let msg = match ThisProgram::get_metadata::<Description>(ThisProgram::EntryHello) {
Some(d) => format!("EntryHello desc = {}", d.desc),
None => "EntryHello has no description".to_string(),
};
// --------- IMPORTANT ---------
DescResult::new(msg).to_render()
}
/// Renders the greeting message with the provided name.
#[renderer]
pub fn render_name(name: ResultName) -> RenderResult {
let mut render_result = RenderResult::new();
writeln!(render_result, "Hello, {}!", *name).ok();
render_result
}
/// Renders the metadata query result.
#[renderer]
pub fn render_desc(msg: DescResult) -> RenderResult {
let mut render_result = RenderResult::new();
writeln!(render_result, "{}", *msg).ok();
render_result
}
|