use rpassword::read_password;
use std::io::{self, Write};
pub struct Password {
message: String,
}
impl Password {
pub(crate) fn new() -> Self {
Password {
message: String::new(),
}
}
pub fn message(mut self, msg: &str) -> Self {
self.message = msg.to_string();
self
}
pub fn get_password(&self) -> String {
loop {
print!("{}", self.message);
io::stdout().flush().unwrap();
match read_password() {
Ok(value) => return value,
Err(e) => {
eprintln!("Error reading password: {}", e);
continue;
}
};
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_password_prompt() {
let password = Password::new().message("Enter your password");
assert_eq!(password.message, "Enter your password");
}
}