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
use std::io;
use std::io::Write;
use std::str::FromStr;
const DEFAULT_ERR: &str = "That value does not pass please try again";
struct PromptMsg<'a> {
msg: &'a str,
repeat: bool,
}
impl<'a> PromptMsg<'a> {
fn new() -> Self {
Self {
msg: "",
repeat: false,
}
}
fn from_str(s: &'a str) -> Self {
Self {
msg: s,
repeat: false,
}
}
fn repeat_from_str(s: &'a str) -> Self {
Self {
msg: s,
repeat: true,
}
}
}
type TestFunc<T> = Fn(&T) -> bool;
pub struct InputBuilder<'a, T: FromStr> {
msg: PromptMsg<'a>,
err: &'a str,
default: Option<T>,
test: Vec<(Box<TestFunc<T>>, Option<&'a str>)>,
err_match: Box<dyn Fn(&T::Err) -> Option<String>>,
}
impl<'a, T: FromStr> InputBuilder<'a, T> {
pub fn new() -> InputBuilder<'a, T> {
InputBuilder {
msg: PromptMsg::new(),
err: DEFAULT_ERR,
default: None,
test: Vec::new(),
err_match: Box::new(|_| None),
}
}
pub fn msg(self, msg: &'a str) -> Self {
InputBuilder {
msg: PromptMsg::from_str(msg),
..self
}
}
pub fn repeat_msg(self, msg: &'a str) -> Self {
InputBuilder {
msg: PromptMsg::repeat_from_str(msg),
..self
}
}
pub fn err(self, err: &'a str) -> Self {
InputBuilder { err, ..self }
}
pub fn default(self, default: T) -> Self {
InputBuilder {
default: Some(default),
..self
}
}
fn test<F: 'static + Fn(&T) -> bool>(self, test: F, err: Option<&'a str>) -> Self {
InputBuilder {
test: {
let mut x = self.test;
x.push((Box::new(test), err));
x
},
..self
}
}
pub fn add_test<F: 'static + Fn(&T) -> bool>(self, test: F) -> Self {
self.test(test, None)
}
pub fn add_err_test<F: 'static + Fn(&T) -> bool>(self, test: F, err: &'a str) -> Self {
self.test(test, Some(err))
}
pub fn clear_tests(self) -> Self {
InputBuilder {
test: Vec::new(),
..self
}
}
pub fn err_match<F: 'static + Fn(&T::Err) -> Option<String>>(self, err_match: F) -> Self {
InputBuilder {
err_match: Box::new(err_match),
..self
}
}
pub fn get(self) -> T {
read_input::<T>(
&self.msg,
self.err,
self.default,
&self.test,
&*self.err_match,
)
}
}
pub fn input_new<'a, T: FromStr>() -> InputBuilder<'a, T> {
InputBuilder::new()
}
pub fn valid_input<T: FromStr>(test: impl Fn(&T) -> bool + 'static) -> T {
input_new::<T>().add_test(test).get()
}
pub fn simple_input<T: FromStr>() -> T {
input_new().get()
}
fn read_input<'a, T: FromStr>(
prompt: &PromptMsg<'a>,
err: &str,
default: Option<T>,
test: &[(Box<TestFunc<T>>, Option<&'a str>)],
err_pass: &dyn Fn(&T::Err) -> Option<String>,
) -> T {
print!("{}", prompt.msg);
io::stdout().flush().expect("could not flush output");
let mut input = String::new();
io::stdin()
.read_line(&mut input)
.expect("Failed to read line");
if input.trim().is_empty() {
if let Some(x) = default {
return x;
}
};
loop {
match T::from_str(&input.trim()) {
Ok(value) => {
let mut test_err = None;
let passes_test = test.iter().all(|f| {
if f.0(&value) {
true
} else {
test_err = Some(f.1.unwrap_or(err));
false
}
});
if passes_test {
return value;
} else {
println!("{}", test_err.unwrap_or(err));
}
}
Err(error) => {
println!("{}", err_pass(&error).unwrap_or_else(|| err.to_string()));
}
}
if prompt.repeat {
print!("{}", prompt.msg);
io::stdout().flush().expect("could not flush output");
};
input.clear();
io::stdin()
.read_line(&mut input)
.expect("Failed to read line");
}
}