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
use std::io;
pub trait ReadInput
where
Self: std::marker::Sized,
{
fn valid_input<F: Fn(&Self) -> bool>(test: F) -> Self {
Self::read_input(None, "That value does not pass please try again", |x| {
test(x)
})
}
fn simple_input() -> Self {
Self::read_input(None, "That value does not pass please try again", |_| true)
}
fn read_input<F: Fn(&Self) -> bool>(msg: Option<&str>, err: &str, test: F) -> Self;
}
impl ReadInput for String {
fn read_input<F: Fn(&Self) -> bool>(msg: Option<&str>, err: &str, test: F) -> Self {
if let Some(msg) = msg {
println!("{}", msg);
};
loop {
let mut input = String::new();
io::stdin()
.read_line(&mut input)
.expect("Failed to read line");
if test(&input) {
break input;
}
println!("{}", err);
continue;
}
}
}
macro_rules! impl_read_inputn {
($($t:ty),*) => {$(
impl ReadInput for $t {
fn read_input<F: Fn(&Self) -> bool>(msg: Option<&str>, err: &str, test: F) -> Self {
if let Some(msg) = msg {
println!("{}", msg);
};
loop {
let mut input = String::new();
io::stdin()
.read_line(&mut input)
.expect("Failed to read line");
if let Ok(num) = input.trim().parse() {
if test(&num) {
break num;
}
};
println!("{}", err);
continue;
}
}
}
)*}
}
impl_read_inputn! { i8, u8, i16, u16,f32, i32, u32,f64, i64, u64, i128, u128 }