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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
<h1 align="center">Setup</h1>
<p align="center">
Mingling's Basic Components
</p>
---
## Usage
`Setup` is used to organize and package the initialization process of a `Program`, making the project easier to manage. It is defined as follows:
```rust
struct MySetup;
impl ProgramSetup<ThisProgram, ThisProgram>
for MySetup
{
fn setup(
&mut self,
program: &mut Program<ThisProgram, ThisProgram>
) {
// Your setup logic
}
}
```
For example:
```rust
use std::{env::current_dir, path::PathBuf};
use mingling::{
Program,
macros::{dispatcher, gen_program, renderer},
setup::ProgramSetup,
};
// Global state
static OUTPUT_PATH: std::sync::OnceLock<PathBuf>
= std::sync::OnceLock::new();
fn main() {
let mut program = ThisProgram::new();
program.with_setup(MySetup);
program.exec();
}
// Define two Dispatchers using `dispatcher!`
dispatcher!("member.add",
AddMemberCommand => AddMemberEntry);
dispatcher!("member.rm",
RemoveMemberCommand => RemoveMemberEntry);
struct MySetup;
impl ProgramSetup<ThisProgram, ThisProgram> for MySetup {
fn setup(
&mut self, program: &mut Program<ThisProgram, ThisProgram>
) {
// Register Dispatchers
program.with_dispatcher(AddMemberCommand);
program.with_dispatcher(RemoveMemberCommand);
// Initialize global output once
OUTPUT_PATH.get_or_init(|| current_dir().unwrap());
// Pick the "--quiet" or "-q" flag
program.global_flag(["--quiet", "-q"], |p| {
// Disable render output
p.stdout_setting.render_output = false;
});
// Pick the "--output" or "-O" flag, write to output
program.global_argument(["--output", "-O"], |_, v| {
let _ = OUTPUT_PATH.set(PathBuf::from(v));
});
}
}
// Define empty renderer types to give the two types type IDs
#[renderer]
fn phantom_renderer_add_member(
_prev: AddMemberEntry
) {}
#[renderer]
fn phantom_renderer_remove_member(
_prev: RemoveMemberEntry
) {}
gen_program!();
```
## Simplified Syntax
If you find the above declaration method too **verbose**, you can use the `program_setup!` macro to simplify it. The format is:
```rust
#[program_setup]
fn my_setup(
program: &mut Program<ThisProgram, ThisProgram>
) {
// Your setup logic
}
```
For example:
```rust
use std::{env::current_dir, path::PathBuf};
use mingling::{
Program,
macros::{
dispatcher,
gen_program,
program_setup,
renderer
},
};
static OUTPUT_PATH: std::sync::OnceLock<PathBuf>
= std::sync::OnceLock::new();
#[tokio::main]
async fn main() {
let mut program = ThisProgram::new();
program.with_setup(MySetup);
program.exec().await;
}
dispatcher!("member.add",
AddMemberCommand => AddMemberEntry);
dispatcher!("member.rm",
RemoveMemberCommand => RemoveMemberEntry);
#[program_setup]
fn my_setup(
program: &mut Program<ThisProgram, ThisProgram>
) {
program.with_dispatcher(AddMemberCommand);
program.with_dispatcher(RemoveMemberCommand);
OUTPUT_PATH.get_or_init(|| current_dir().unwrap());
program.global_flag(["--quiet", "-q"], |p| {
p.stdout_setting.render_output = false;
});
program.global_argument(["--output", "-O"], |_, v| {
let _ = OUTPUT_PATH.set(PathBuf::from(v));
});
}
#[renderer]
fn phantom_renderer_add_member(
_prev: AddMemberEntry
) {}
#[renderer]
fn phantom_renderer_remove_member(
_prev: RemoveMemberEntry
) {}
gen_program!();
```
## 💡 Next Page
> **Basic Component** - Dispatcher [Go](./pages/2-basic/3-dispatcher)
|