zen_console_input/
password.rs1use rpassword::read_password;
4use std::io::{self, Write};
5
6pub struct Password {
8 message: String,
9}
10
11impl Password {
12 pub(crate) fn new() -> Self {
13 Password {
14 message: String::new(),
15 }
16 }
17
18 pub fn message(mut self, msg: &str) -> Self {
20 self.message = msg.to_string();
21 self
22 }
23
24 pub fn get_password(&self) -> String {
26 loop {
27 print!("{}", self.message);
28 io::stdout().flush().unwrap();
29 match read_password() {
30 Ok(value) => return value,
31 Err(e) => {
32 eprintln!("Error reading password: {}", e);
33 continue;
34 }
35 };
36 }
37 }
38}
39
40#[cfg(test)]
41mod tests {
42 use super::*;
43
44 #[test]
45 fn test_password_prompt() {
46 let password = Password::new().message("Enter your password");
47 assert_eq!(password.message, "Enter your password");
48 }
49}