use crate::unicode_normalization_check::uniform_unicode_normalization;
use std::fs::File;
use std::io;
use std::io::BufRead;
use std::io::BufReader;
use std::path::Path;
use std::path::PathBuf;
use std::str::FromStr;
pub fn read_in_custom_list(file_path: &Path) -> Result<Vec<String>, String> {
let file_input: Vec<String> = match read_by_line(file_path.to_path_buf()) {
Ok(r) => r,
Err(e) => return Err(format!("Error reading word list file: {}", e)),
};
let mut word_list: Vec<String> = vec![];
for line in file_input {
if line.trim() != "" {
word_list.push(line.trim().to_string());
}
}
word_list.sort();
word_list.dedup();
if !uniform_unicode_normalization(&word_list) {
eprintln!(
"WARNING: Custom word list has multiple Unicode normalizations. Consider normalizing the Unicode of all words on the list before making a passphrase."
);
}
Ok(word_list)
}
fn read_by_line<T: FromStr>(file_path: PathBuf) -> io::Result<Vec<T>>
where
<T as std::str::FromStr>::Err: std::fmt::Debug,
{
let mut vec = Vec::new();
let f = File::open(file_path)?;
let file = BufReader::new(&f);
for line in file.lines() {
match line?.parse() {
Ok(l) => vec.push(l),
Err(e) => panic!("Error parsing line from file: {:?}", e),
}
}
Ok(vec)
}