1use std::fs::File;
2use std::{io, env};
3use std::io::BufRead;
4use std::path::Path;
5use rand::Rng;
6use std::fmt::Write;
7use inflector::Inflector;
8
9pub fn generate_password(words:Vec<String>, min_letter_count:u8, min_word_count:u8, initial_capital:bool, use_caps:bool, use_spaces:bool ) ->String{
16
17 let mut char_count = 0;
18 let mut rng = rand::thread_rng();
19 let mut chosen_words = vec![];
20 while char_count < min_letter_count as usize || chosen_words.len()<min_word_count as usize {
21 let n: usize = rng.gen();
22 let pos = n % words.len();
23 chosen_words.push(words.get(pos).unwrap());
24 char_count += chosen_words.last().unwrap().len();
25 }
26 let mut chosen_iter = chosen_words.iter();
27 let mut res = String::new();
28 if initial_capital || use_caps {
29 write!(res, "{}", chosen_iter.next().unwrap().to_title_case()).expect("Unable to write data");
30 } else {
31 write!(res, "{}", chosen_iter.next().unwrap()).expect("Unable to write data");
32 }
33 while let Some(word) = chosen_iter.next() {
34 if use_spaces {
35 write!(res, " ").expect("Unable to write data");
36 }
37 if use_caps {
38 write!(res, "{}", word.to_title_case()).expect("Unable to write data");
39 } else {
40 write!(res, "{}", word).expect("Unable to write data");
41 }
42 }
43 res
44}
45pub fn get_word_list(file:File, filter:bool) -> Vec<String> {
49 let file_reader = io::BufReader::new(file);
50 let mut words = Vec::with_capacity(10_000);
51
52 for l in file_reader.lines() {
53 let line = l.unwrap();
54 if !filter {
55 words.push(line);
56 } else {
57 if !line.contains(|c:char|!c.is_ascii_lowercase()) {
58 words.push(line);
59 }
60 }
61 }
62 words
63}
64
65pub fn get_default_filename() -> Option<String> {
71
72 let word_list_in_config = env::var("XDG_CONFIG_HOME")
73 .unwrap_or(env::var("HOME")
74 .unwrap_or("~/".to_string())+".config")
75 + "wordpass/words.txt";
76
77 if Path::exists(Path::new(&word_list_in_config)) {
78 return Some(word_list_in_config);
79 }
80 let default_dictionary = "/usr/share/dict/words".to_string();
81 if Path::exists(Path::new(&default_dictionary)) {
82 return Some(default_dictionary);
83 }
84 None
85}
86