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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
//! Full Example - Todo List
//!
//! This example introduces how to use Mingling's features to build a complete CLI program
//!
//! > HAHA:
//! >
//! > This is truly a cliché example, as common as `Hello World`!
use mingling::{
LazyInit, LazyRes,
macros::route,
prelude::*,
res::ResExitCode,
setup::{ExitCodeSetup, HelpFlagSetup, StructuralRendererSetup},
};
use std::io::Write;
mod help;
pub use help::*;
mod todolist;
pub use todolist::*;
#[derive(Default, Clone)]
pub struct ResProgramFlags {
pub all: bool,
}
// Define dispatchers
dispatcher!("add");
dispatcher!("list");
dispatcher!("complete");
dispatcher!("clean");
// Define states
pack!(StateAddTodo = String);
pack!(StateCompleteTodo = i32);
pack!(StateListTodo = bool);
// Define errors
pack!(ErrorNoTaskDescriptionProvided = ());
pack!(ErrorNoIndexProvided = ());
pack!(ErrorIndexOutOfBounds = ());
fn main() {
let mut program = ThisProgram::new();
// Setups
program.with_setup(ExitCodeSetup::default());
program.with_setup(StructuralRendererSetup);
program.with_setup(HelpFlagSetup::new(["--help", "-h"]));
// Flags
let all = program.pick_global_flag(["-A", "--all"]);
// Resources
program.with_resource(
// Load on use
ResTodoList::lazy_init(read_todo_list)
// Write on drop
.with_on_drop(write_todo_list),
);
program.with_resource(ResProgramFlags { all });
// Dispatchers
program.with_dispatcher(CMDAdd);
program.with_dispatcher(CMDComplete);
program.with_dispatcher(CMDList);
program.with_dispatcher(CMDClean);
program.exec_and_exit();
}
// Command `add`
#[chain]
fn handle_add(args: EntryAdd) -> Next {
let task: String = route! {
args
.pick_or_route((), ErrorNoTaskDescriptionProvided::new(()))
.unpack()
};
StateAddTodo::new(task).to_chain()
}
#[chain]
fn handle_state_add_todo(
state: StateAddTodo,
// Inject ResTodoList resource
todolist: &mut LazyRes<ResTodoList>,
) -> Next {
let todolist = todolist.get_mut();
// Unpack state and read description
let description = state.inner;
todolist.items.push(Todo {
item: description,
completed: false,
});
// Clone todolist to render
let todolist = todolist.clone();
todolist.to_render()
}
// Command `list`
#[chain]
fn handle_list(_args: EntryList, todolist: &mut LazyRes<ResTodoList>) -> Next {
let todolist = todolist.get_mut().clone();
todolist.to_render()
}
// Command `complete`
#[chain]
fn handle_complete(args: EntryComplete) -> Next {
let index: i32 = route! {
args.pick_or_route((), ErrorNoIndexProvided::new(())).unpack()
};
StateCompleteTodo::new(index).to_chain()
}
#[chain]
fn handle_state_complete_todo(
state: StateCompleteTodo,
todolist: &mut LazyRes<ResTodoList>,
) -> Next {
let todolist = todolist.get_mut();
let index = state.inner as usize;
if index < todolist.items.len() {
todolist.items[index].completed = true;
todolist.clone().to_render()
} else {
ErrorIndexOutOfBounds::new(()).to_render()
}
}
// Command `clean`
#[chain]
fn handle_clean(
_args: EntryClean,
todolist: &mut LazyRes<ResTodoList>,
program_flags: &ResProgramFlags,
) -> Next {
let todolist = todolist.get_mut();
if program_flags.all {
todolist.items.clear();
} else {
todolist.items.retain(|item| !item.completed);
}
if todolist.items.is_empty() {
empty_result!()
} else {
todolist.clone().to_render()
}
}
/// Renders error when no task description is provided.
#[renderer]
pub fn render_error_no_task_description_provided(
_err: ErrorNoTaskDescriptionProvided,
// ExitCode
ec: &mut ResExitCode,
) -> RenderResult {
let mut render_result = RenderResult::new();
writeln!(render_result, "No task description provided!").ok();
writeln!(render_result).ok();
writeln!(render_result, "Use `todolist add <desc>` to add a task").ok();
ec.exit_code = 1;
render_result
}
/// Renders error when no index is provided.
#[renderer]
pub fn render_error_no_index_provided(
_err: ErrorNoIndexProvided,
// ExitCode
ec: &mut ResExitCode,
) -> RenderResult {
let mut render_result = RenderResult::new();
writeln!(render_result, "No index provided!").ok();
writeln!(render_result).ok();
writeln!(render_result, "Use `todolist list` to query indexes").ok();
ec.exit_code = 2;
render_result
}
/// Renders error when index is out of bounds.
#[renderer]
pub fn render_error_index_out_of_bounds(
_err: ErrorIndexOutOfBounds,
// ExitCode
ec: &mut ResExitCode,
) -> RenderResult {
let mut render_result = RenderResult::new();
writeln!(render_result, "Index out of bounds!").ok();
ec.exit_code = 3;
render_result
}
gen_program!();
|