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
use super::*;
impl Promptable for bool {
fn prompt<S: AsRef<str>>(msg: S) -> Result<Self> {
prompt_bool(msg)
}
fn prompt_opt<S: AsRef<str>>(msg: S) -> Result<Option<Self>> {
prompt_bool_opt(msg)
}
fn prompt_default<S: AsRef<str>>(msg: S, default: Self) -> Result<Self> {
let msg = if default {
format!("{} (Y/n)", msg.as_ref())
} else {
format!("{} (y/N)", msg.as_ref())
};
Ok(prompt_bool_opt(msg)?.unwrap_or(default))
}
}
fn prompt_bool<S: AsRef<str>>(msg: S) -> Result<bool> {
Prompter::new().prompt_then(msg, |s| match &*s.to_lowercase() {
"true" | "yes" | "y" => Ok(true),
"false" | "no" | "n" => Ok(false),
s => Err(format!("Could not parse {} as bool.", s)),
})
}
fn prompt_bool_opt<S: AsRef<str>>(msg: S) -> Result<Option<bool>> {
Prompter::new().prompt_then(msg, |s| match &*s.to_lowercase().trim() {
"" => Ok(None),
"true" | "yes" | "y" => Ok(Some(true)),
"false" | "no" | "n" => Ok(Some(false)),
s => Err(format!("Could not parse {} as bool.", s)),
})
}