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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
//! Fast designing menus for your Rust CLI programs.
//!
//! > Caution: This library is not completely stable yet.
//! Many changes may occur depending on versions. I am still looking for a sustainable design
//! of the library.
//!
//! This crate provides a library with structs and traits to easily build menus.
//! It includes type-checking from the user input, and a formatting customization.
//!
//! This crate is useful if you use [structopt](https://docs.rs/structopt/)
//! or [clap](https://docs.rs/clap/) crates beside it, so you can get the matches safely, and
//! build a menu on your own after.
//!
//! It can also be used as a mode selection, for a game for example.
//!
//! ## Note
//!
//! If you want to use the derive Menu macro,
//! you must use the [ezmenu](https://docs.rs/ezmenu/) crate instead.
//! This crate may however contain features that are not available on the ezmenu crate.
//!
//! # Value-menus
//!
//! The first type of menu this library provides is a [value-menu](crate::menu::ValueMenu).
//! These menus are used to retrieve data values from the user by iterating on the next outputs.
//! At each iteration, it prompts the user a value, parses it and prompts until it is correct,
//! then returns it.
//!
//! ## Example
//!
//! Here is an example of how to use this menu:
//!
//! ```
//! use ezmenulib::prelude::*;
//!
//! let mut my_menu = ValueMenu::from([
//! Field::Value(ValueField::from("Give your name")),
//! Field::Value(ValueField::from("Give a number")),
//! ])
//! .title("Hello there!");
//!
//! let name: String = my_menu.next_output().unwrap();
//! let number: i32 = my_menu.next_output().unwrap();
//!
//! println!("values provided: name={}, number={}", name, number);
//! ```
//!
//! This sample code prints the standard menu like above:
//!
//! ```text
//! Hello there!
//! --> Give your name
//! >> Ahmad
//!
//! --> Give a number
//! >> 1000
//!
//! values provided: name=Ahmad, number=1000
//! ```
//!
//! ## Format it as you wish
//!
//! You can apply several formatting rules on a menu or on a field specifically.
//! You can edit:
//! * the chip: `"--> "` by default.
//! * the prefix: `">> "` by default.
//! * insert a new line before prefix and user input: `true` by default.
//! * display default values or not: `true` by default.
//! These parameters are defined in the [`ValueFieldFormatting`](crate::field::ValueFieldFormatting) struct.
//!
//! ### Example
//!
//! For a custom format on a field and a main formatting rule on a menu, you can build this with:
//! ```rust
//! use ezmenulib::prelude::*;
//!
//! let mut license = ValueMenu::from([
//! Field::Value(ValueField::from("Authors")),
//! Field::Value(ValueField::from("Project name")
//! .fmt(ValueFieldFormatting::chip("--- "))),
//! Field::Value(ValueField::from("Date")),
//! ])
//! .fmt(ValueFieldFormatting::chip("==> "));
//!
//! // ...
//! ```
//!
//! The custom `"==> "` chip will be applied on every field except those with custom formatting rules,
//! In this case, it will format the text like above:
//!
//! ```text
//! ==> Authors
//! >> ...
//!
//! --- Project name
//! >> ...
//!
//! ==> Date
//! >> ...
//! ```
//!
//! ## Skip fields with default values
//!
//! You can provide a default input value to a field with the `ValueField::default` method:
//! ```rust
//! # use ezmenulib::field::ValueField;
//! # fn get_field() -> ValueField<'static> {
//! ValueField::from("Date").default_value("2022")
//! # }
//! ```
//!
//! If the user provided an incorrect input, the program will not re-ask a value to the user,
//! but will directly return the default value instead.
//!
//! By default, the default value is visible by the user, like above:
//!
//! ```text
//! --> Date (default: 2022)
//! ```
//!
//! If you want to hide it, you can do so
//! with formatting rules:
//!
//! ```rust
//! # use ezmenulib::prelude::*;
//! # fn get_field() -> ValueField<'static> {
//! ValueField::from("Date")
//! .fmt(ValueFieldFormatting::default(false))
//! # }
//! ```
//!
//! ## Use custom value types
//!
//! If the user has to provide a value which corresponds to your specific type,
//! you only need to implement the `FromStr` trait on that type.
//! The error type only needs to implement `Debug` trait, for error displaying purposes.
//!
//! If the error is infallible, you can use simple data types such as unit `()`
//! or `std::convert::Infallible`.
//!
//! ### Example
//!
//! ```rust
//! use std::str::FromStr;
//! use ezmenulib::field::ValueField;
//!
//! enum Type {
//! MIT,
//! BSD,
//! GPL,
//! }
//!
//! impl FromStr for Type {
//! type Err = String;
//!
//! fn from_str(s: &str) -> Result<Self, Self::Err> {
//! match s.to_lowercase().as_str() {
//! "mit" => Ok(Self::MIT),
//! "gpl" => Ok(Self::GPL),
//! "bsd" => Ok(Self::BSD),
//! s => Err(format!("unknown license type: {}", s)),
//! }
//! }
//! }
//!
//! let license_type: Type = ValueField::from("Give the license type")
//! .build_init()
//! .unwrap();
//! ```
//!
//! ## Provided custom value types
//!
//! The EZMenu library already provides custom value types to handle user input.
//! Check out the [`customs`]
//! module to see all available custom value types.
//!
//! For instance, the [`MenuBool`](crate::customs::MenuBool)
//! is used to override the boolean parsing method, allowing "yes" or "no" as inputs.
//!
//! The [`MenuVec<T>`](crate::customs::MenuVec) type allows the user
//! to enter many values separated by spaces and collect them into a `Vec<T>`.
//! Of course, `T` must implement `FromStr` trait.
//!
//! # Selectable menus
//!
//! Beside the value-menus, there is also the [selectable menus](crate::menu::SelectMenu).
//! These menus, unlike value-menus, displays the list of possible values to the user,
//! to let him select one among them.
//!
//! ## Example
//!
//! ```
//! use std::str::FromStr;
//! use ezmenulib::prelude::*;
//!
//! enum Type {
//! MIT,
//! GPL,
//! BSD,
//! }
//!
//! impl FromStr for Type {
//! type Err = MenuError;
//!
//! fn from_str(s: &str) -> MenuResult<Self> {
//! match s.to_lowercase().as_str() {
//! "mit" => Ok(Self::MIT),
//! "gpl" => Ok(Self::GPL),
//! "bsd" => Ok(Self::BSD),
//! s => Err(MenuError::from(format!("unknown license type: {}", s))),
//! }
//! }
//! }
//!
//! let license_type: Type = SelectMenu::from([
//! SelectField::from("MIT"),
//! SelectField::from("GPL"),
//! SelectField::from("BSD"),
//! ])
//! .title(SelectTitle::from("Choose a license type"))
//! .default(0)
//! .next_output()
//! .unwrap();
//! ```
//!
//! This code prints the output like above:
//! ```text
//! Choose a license type:
//! 1 - MIT (default)
//! 2 - GPL
//! 3 - BSD
//! ```
//! > Note that the `:` character right next to the title is on purpose
//! (check the [`SelectTitle`](crate::menu::SelectTitle) for more information).
//!
//! You can also use this menu on primitive types or types already implementing `FromStr` trait.
//! The menu accepts an index or the literal value as input.
//!
//! ## Formatting rules
//!
//! Like the [`ValueMenu`](crate::menu::ValueMenu), you can edit many formatting rules
//! to stylish the menu as you want.
//!
//! ### The menu format
//!
//! The selective menu itself has two editable formatting rules.
//! Like [`ValueFieldFormatting`](crate::field::ValueFieldFormatting), it contains a
//! `chip` and a `prefix`:
//! ```text
//! X<chip><message>
//! X<chip><message>
//! ...
//! <prefix>
//! ```
//!
//! The default chip is `" - "`, and the default prefix is `">> "`.
//!
//! ### The title format
//!
//! The selective has also its own title format.
//! Because the title can be seen as a field of a value-menu, it has its own instance of
//! `ValueFieldFormatting` struct.
//!
//! This is useful for sub-menu management, where the formatting rules of the title inherits from the
//! formatting rules of the parent menu, for more convenience.
//!
//! ## Skip the menu with a default field value
//!
//! The user can skip the selectable menu if it has a default value provided.
//! To do so, you must use the [`SelectMenu::default`](crate::menu::SelectMenu::default) method.
//!
//! This will mark the indexed field as `"(default)"`.
//!
//! ## Sub-menus
//!
//! You can set a selectable menu as a field of a value-menu.
//! This is really useful if you want to design sub-menu. The selectable field format
//! will inherit from the formatting rules of the value-menu.
//!
//! ### Example
//!
//! ```
//! use ezmenulib::prelude::*;
//!
//! let mut license = ValueMenu::from([
//! Field::Value(ValueField::from("Authors")),
//! Field::Value(ValueField::from("Project name")),
//! Field::Select(SelectMenu::from([
//! SelectField::from("MIT"),
//! SelectField::from("GPL"),
//! SelectField::from("BSD"),
//! ])
//! .title(SelectTitle::from("License type"))
//! .default(0)),
//! ])
//! .title("Describe the project license");
//! ```
/// Module used to import common structs, to build menus with their fields.
use VarError;
use Error;
use ;
use ;
/// The error type used by the menu builder.
/// The main result type used in the EZMenu library.
pub type MenuResult<T> = ;