use tracing::debug;
#[derive(Debug)]
pub struct PasswordList {
passwords: Vec<String>,
}
impl PasswordList {
pub async fn collect(
cached_correct: Option<&str>,
download_password: Option<&str>,
nzb_meta_password: Option<&str>,
global_file: Option<&std::path::Path>,
try_empty: bool,
) -> Self {
let mut seen = std::collections::HashSet::new();
let mut passwords = Vec::new();
for pw in [cached_correct, download_password, nzb_meta_password]
.into_iter()
.flatten()
{
if seen.insert(pw) {
passwords.push(pw.to_string());
}
}
if let Some(path) = global_file
&& let Ok(file_content) = tokio::fs::read_to_string(path).await
{
for line in file_content.lines() {
let pw = line.trim();
if !pw.is_empty() && !passwords.iter().any(|p| p == pw) {
passwords.push(pw.to_string());
}
}
}
if try_empty && !passwords.iter().any(|p| p.is_empty()) {
passwords.push(String::new());
}
debug!(
"collected {} unique passwords for extraction",
passwords.len()
);
Self { passwords }
}
pub fn iter(&self) -> impl Iterator<Item = &String> {
self.passwords.iter()
}
pub fn is_empty(&self) -> bool {
self.passwords.is_empty()
}
pub fn len(&self) -> usize {
self.passwords.len()
}
}