custom_tryread/
custom_tryread.rs1use smart_read::*;
2
3
4
5struct PasswordInput {
6 pub min_len: usize,
7 pub min_digits: usize,
8}
9
10fn main() {
12 let input = read!(PasswordInput {min_len: 10, min_digits: 1});
13 println!("You entered: \"{input}\"");
14}
15
16
17
18impl TryRead for PasswordInput {
19 type Output = String;
20 type Default = (); fn try_read_line(self, prompt: Option<String>, default: Option<Self::Default>) -> smart_read::BoxResult<Self::Output> {
22 if default.is_some() {return DefaultNotAllowedError::new_box_result();}
23 let prompt = prompt.unwrap_or_else(
24 || format!("Enter a password (must have {}+ characters and have {}+ digits): ", self.min_len, self.min_digits)
25 );
26 loop {
27
28 print!("{prompt}");
29 let password = read_stdin()?;
30
31 if password.len() < 10 {
32 println!("Invalid, password must have at least 10 characters");
33 continue;
34 }
35 if password.chars().filter(|c| c.is_digit(10)).count() < 1 {
36 println!("Invalid, password must have at least 1 digit");
37 continue;
38 }
39
40 return Ok(password)
41
42 }
43 }
44}