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
//! Complex but easy ways to read user input
//!
//! <br>
//!
//! ### Functionality in this crate is defined by types that implement `TryRead`.
//!
//! <br>
//! <br>
//!
//! # Types that implement `TryRead`:
//!
//! This is basically a list of all default functionality, if you want to know more about one of these types, the header name is the same as the module which contains the type
//!
//! <br>
//!
//! ### Basics
//!
//! ```
//! impl TryRead for () // requests any string from the user
//! impl TryRead for NonEmptyInput // requests a non-empty string from the user
//! impl TryRead for NonWhitespaceInput // requests a non-whitespace string from the user
//! impl TryRead for BoolInput // requests a true/false/t/f string from the user
//! impl TryRead for YesNoInput // requests a yes/no/y/n string from the user
//! impl TryRead for CharInput // requests a single-char string from the user
//! // requests a number from the user that can be converted to a specific type:
//! impl TryRead for U8Input, U16Input, U32Input, U64Input, U128Input, USizeInput
//! impl TryRead for I8Input, I16Input, I32Input, I64Input, I128Input, ISizeInput
//! impl TryRead for F32Input
//! impl TryRead for F64Input
//! ```
//!
//! <br>
//!
//! ### Input Validations
//!
//! These allow you to easily add custom logic to specific reads
//!
//! ```
//! // requests a string from the user which passes the programmed validation:
//! impl<F> TryRead for SimpleValidate<F> where F: Fn(&str) -> Result<(), String>
//! // similar to `SimpleValidate`, but also transforms the output:
//! impl<F, O> TryRead for TransformValidate<F, O> where F: Fn(String) -> Result<O, String>, O: Display
//! ```
//!
//! <br>
//!
//! ### List Constraints
//!
//! These allow you to specify which inputs are allowed. Example: `read!(["a", "b", "c"])`
//!
//! NOTE: The default value for these types denotes the index of the default option
//!
//! ```
//! // requests a string from the user that matches any of the names from the `InputOption`s:
//! impl<Data> TryRead for &[InputOption<Data>]
//! impl<Data> TryRead for &[InputOption<Data>; N]
//! impl<Data> TryRead for [InputOption<Data>; N]
//! // requests a string from the user that matches any value in the list:
//! impl<T: Display> TryRead for &[T]
//! impl<T: Display> TryRead for [T; N]
//! impl<T: Display> TryRead for Vec<T>
//! impl<T: Display> TryRead for VecDeque<T>
//! impl<T: Display> TryRead for LinkedList<T>
//! ```
//!
//! <br>
//!
//! ### Range Constraints
//!
//! These allow you to take a number within a specified range. Example: `read!(1. ..= 100.)`, `read!(10..)`, etc
//!
//! ```
//! impl<T> TryRead for Range<T> where T: Display + FromStr + PartialOrd<T>, <T as FromStr>::Err: Display
//! impl<T> TryRead for RangeInclusive<T> where T: Display + FromStr + PartialOrd<T>, <T as FromStr>::Err: Display
//! impl<T> TryRead for RangeTo<T> where T: Display + FromStr + PartialOrd<T>, <T as FromStr>::Err: Display
//! impl<T> TryRead for RangeFrom<T> where T: Display + FromStr + PartialOrd<T>, <T as FromStr>::Err: Display
//! impl<T> TryRead for RangeToInclusive<T> where T: Display + FromStr + PartialOrd<T>, <T as FromStr>::Err: Display
//! ```
//!
//! <br>
//! <br>
//!
//! # Macro Syntax
//!
//! Prompt macros: `prompt!("message to user"; [default_value] input_type)`
//!
//! Read macros: `read!([default_value] input_type)`
//!
//! All components are optional (except the message in prompts) and all are expressions.
//!
//! Some examples:
//! ```
//! read!([2] 1..=10); // take a number from 1 to 10, with 2 as the default
//! prompt!(messages[i]; UsizeInput); // request a positive integer for the current prompt
//! prompt!("continue?"; [true] YesNoInput); // request a yes/no input with yes being the default
//! ```
//!
//! <br>
//!
//! The input type is what determines the functionality of the input. It is another expression, and the type of the resulting value is what determines which impl of `TryRead` is used. For example, if you have `read!(1..10)` then the impl for `Range<i32>` is used. Also, when you have something like `read!(UsizeInput)`, you are creating a new `UsizeInput` value and passing it to the macro.
//!
//! Some input types have special syntax that can be substituted for the input_type component, they are:
//!
//! ```
//! // this:
//! read!()
//! // is this:
//! read!(())
//!
//! // this:
//! read!(= 1, 2, 3)
//! // is this:
//! read!([1, 2, 3])
//!
//! // this:
//! read!(=
//! "1_bulletin"; "1_display_name"; ["1_alt_name_1", ...]; 1_data,
//! "2_bulletin"; "2_display_name"; ["2_alt_name_1", ...]; 2_data,
//! ...
//! )
//! // is this:
//! read!([
//! InputOption::new("1_bulletin", vec!("1_display_name", "1_alt_name_1", ...), 1_data),
//! InputOption::new("2_bulletin", vec!("2_display_name", "2_alt_name_1", ...), 2_data),
//! ...
//! ])
//! ```
//!
//! <br>
//!
//! And of course, you can combine this special input type syntax with everything else: `prompt!("Enter a color: "; ["red"] = "red", "green", "blue")`
//!
//! <br>
//!
//! If you have ideas for more functionality (including things you've found to be useful yourself), feel free to open an issue / pull request
//!
//! <br>
//! <br>
use ;
/// Contains implementations for `()`, `UsizeInput`, `NonEmptyInput`, etc
/// Contains implementations for `SimpleValidate` and `TransformValidate`
/// Contains implementations for `&[T]`, `[T; N]`, `Vec<T>`, `read!(= a, b, c)`, etc
/// Contains implementations for `Range<T>`, `RangeFrom<T>`, etc
/// Easy way to use existing functionality. If you want to extend functionality instead, you can do `use smart_read::*;`
// ================================ Macros ================================ //
/// ## Reads a line of text, a number, etc
/// Same as `read!()`, but returns a result
/// Same as `read!()`, but also prints a prompt
/// Same as `prompt!()`, but returns a result
};
=> ;
=> ;
}
// ================================ TYPES ================================ //
/// Just `Result<T, Box<dyn Error>>`, mostly for internal use
pub type BoxResult<T> = ;
/// This is what powers the whole crate. Any type that implements this can be used with the macros
// ================================ FUNCTIONS ================================ //
/// Utility function, mostly for internal use
/// Tiny utility function, clears the terminal output, but you should probably use the [ClearScreen](https://crates.io/crates/clearscreen) crate instead
/// Waits for the user to press enter, prints "Press enter to continue "
///
/// This is basically a wrapper for `prompt!("Press enter to continue ")`