ex_cli/zip/
manager.rs

1use crate::error::{MyError, MyResult};
2use std::collections::hash_set::Iter;
3use std::collections::HashSet;
4use std::path::Path;
5
6pub struct PasswordManager {
7    config: Option<String>,
8    passwords: HashSet<String>,
9}
10
11impl PasswordManager {
12    pub fn new(config: &Option<String>) -> Self {
13        let config = config.clone();
14        let passwords = HashSet::new();
15        Self { config, passwords }
16    }
17
18    pub fn config(&self) -> Option<&String> {
19        self.config.as_ref()
20    }
21
22    pub fn passwords(&self) -> Iter<'_, String> {
23        self.passwords.iter()
24    }
25
26    pub fn prompt(&mut self, path: &Path) -> MyResult<String> {
27        let prompt = format!("Password for {}? ", path.display());
28        let password = rpassword::prompt_password(prompt)
29            .map_err(|_| MyError::Text(String::from("")))?;
30        self.passwords.insert(password.clone());
31        Ok(password)
32    }
33}