-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpassword.rs
30 lines (26 loc) · 884 Bytes
/
password.rs
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
use requestty::Question;
fn is_valid(password: &str, _: &requestty::Answers) -> bool {
password.contains(|c: char| c.is_ascii_digit()) && password.contains(char::is_alphabetic)
}
fn letter_and_numbers(password: &str, ans: &requestty::Answers) -> Result<(), String> {
if is_valid(password, ans) {
Ok(())
} else {
Err("Password needs to have at least 1 letter and 1 number.".to_owned())
}
}
fn main() {
let questions = vec![
Question::password("password1")
.message("Enter a password")
.validate(letter_and_numbers)
.build(),
Question::password("password2")
.message("Enter a masked password")
.mask('*')
.validate_on_key(is_valid)
.validate(letter_and_numbers)
.build(),
];
println!("{:#?}", requestty::prompt(questions));
}