1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//! # Simple Password Generator
//!
//! A library for generating passwords

use rand::Rng;
use std::char;

static DEFAULT_LENGTH: u8 = 8;
static ALPHABET: [char; 26] = [
    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
    't', 'u', 'v', 'w', 'x', 'y', 'z',
];
static NUMBERS: [char; 10] = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
static SPECIAL_CHARS: [char; 11] = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '~'];

/// PasswordGenerator struct for generatoring passwords
pub struct PasswordGenerator {
    /// Array of lowercase chars used when generating a password.
    lowercase_char_set: [char; 26],
    /// Array of uppercase chars used when generating a password.
    uppercase_char_set: [char; 26],
    /// Array of number chars used when generating a password.
    number_set: [char; 10],
    /// Array of special chars used when generating a password.
    spec_char_set: [char; 11],
    /// Array of composition codes used when generating a password.
    composition_codes: [CompositionCodes; 4],
    /// Length of password to be generated.
    ///
    /// Default `8`
    length: u8,
    /// Bool used to exclude uppercase composition code when generating a password.
    ///
    /// Default `false`
    lowercase_only: bool,
    /// Bool used to exclude lowercase composition code when generating a password.
    ///
    /// Default `false`
    uppercase_only: bool,
    /// Bool used to exclude number composition code when generating a password.
    ///
    /// Default `false`
    exclude_numbers: bool,
    /// Bool used to special character composition code when generating a password.
    ///
    /// Default `false`
    exclude_special_chars: bool,
}

/// The primary character types used when generating the composition of the password
pub enum CompositionCodes {
    Lowercase,
    Uppercase,
    Number,
    SpecialCharacter,
}

impl CompositionCodes {
    /// Outputs all compositions code in a array
    pub fn all_to_array() -> [CompositionCodes; 4] {
        [
            CompositionCodes::Lowercase,
            CompositionCodes::Uppercase,
            CompositionCodes::Number,
            CompositionCodes::SpecialCharacter,
        ]
    }
}

impl PasswordGenerator {
    /// PasswordGenerator Constructor
    pub fn new() -> PasswordGenerator {
        let lowercase_char_set = ALPHABET;
        let uppercase_char_set = lowercase_char_set.map(|lower_char| {
            let uppered: Vec<char> = lower_char.to_uppercase().collect();
            uppered[0]
        });

        PasswordGenerator {
            lowercase_char_set,
            uppercase_char_set,
            number_set: NUMBERS,
            spec_char_set: SPECIAL_CHARS,
            composition_codes: CompositionCodes::all_to_array(),
            length: DEFAULT_LENGTH,
            lowercase_only: false,
            uppercase_only: false,
            exclude_numbers: false,
            exclude_special_chars: false,
        }
    }

    /// Builder function for setting password length
    ///
    /// # Examples
    ///
    /// ```
    /// use simple_password_generator::PasswordGenerator;
    ///
    /// let expected_length = 16;
    /// let result = PasswordGenerator::new().length(expected_length).generate();
    ///
    /// assert_eq!(expected_length as usize, result.chars().count());
    /// ```
    pub fn length(mut self, length: u8) -> Self {
        self.length = length;

        self
    }

    /// Builder function for setting lowercase characters only
    ///
    /// # Examples
    ///
    /// ```
    /// use simple_password_generator::PasswordGenerator;
    ///
    /// let test_password = PasswordGenerator::new().lowercase_only(true).generate();
    /// let mut contains_uppercase = false;
    ///
    /// for c in test_password.chars() {
    /// 	if c.is_alphabetic() {
    /// 		if c.is_uppercase() {
    /// 			contains_uppercase = true
    /// 		}
    /// 	}
    /// }
    ///
    /// assert_eq!(false, contains_uppercase);
    /// ```
    pub fn lowercase_only(mut self, lowercase_only: bool) -> Self {
        self.lowercase_only = lowercase_only;

        self
    }

    /// Builder function for setting uppercase characters only
    ///
    /// # Examples
    ///
    /// ```
    /// use simple_password_generator::PasswordGenerator;
    ///
    /// let test_password = PasswordGenerator::new().uppercase_only(true).generate();
    /// let mut contains_lowercase = false;
    ///
    /// for c in test_password.chars() {
    /// 	if c.is_alphabetic() {
    /// 		if c.is_lowercase() {
    /// 			contains_lowercase = true
    /// 		}
    /// 	}
    /// }
    ///
    /// assert_eq!(false, contains_lowercase);
    /// ```
    pub fn uppercase_only(mut self, uppercase_only: bool) -> Self {
        self.uppercase_only = uppercase_only;

        self
    }

    /// Builder function for excluding any numbers from password
    ///
    /// # Examples
    ///
    /// ```
    /// use simple_password_generator::PasswordGenerator;
    ///
    /// let test_password = PasswordGenerator::new().exclude_numbers(true).generate();
    /// let mut contains_numbers = false;
    ///
    /// for c in test_password.chars() {
    /// 	if c.is_numeric() {
    /// 		contains_numbers = true
    /// 	}
    /// }
    ///
    /// assert_eq!(false, contains_numbers);
    /// ```
    pub fn exclude_numbers(mut self, exclude_numbers: bool) -> Self {
        self.exclude_numbers = exclude_numbers;

        self
    }

    /// Builder function for excluding any special characters from password
    ///
    /// # Examples
    ///
    /// ```
    /// use simple_password_generator::PasswordGenerator;
    ///
    /// let test_password = PasswordGenerator::new()
    /// 	.exclude_special_chars(true)
    /// 	.generate();
    /// let spec_char_vec: Vec<char> = vec!['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '~'];
    /// let mut contains_special_chars = false;
    ///
    /// for c in test_password.chars() {
    /// 	if spec_char_vec.contains(&c) {
    /// 		contains_special_chars = true;
    /// 	}
    /// }
    ///
    /// assert_eq!(false, contains_special_chars);
    /// ```
    pub fn exclude_special_chars(mut self, exclude_special_chars: bool) -> Self {
        self.exclude_special_chars = exclude_special_chars;

        self
    }

    /// Returns a random lowercase char
    fn get_random_lowercase_char(&self) -> char {
        let mut rng = rand::thread_rng();
        let rnd_index = rng.gen_range(0..=self.lowercase_char_set.len() - 1);

        self.lowercase_char_set[rnd_index]
    }

    /// Returns a random uppercase char
    fn get_random_uppercase_char(&self) -> char {
        let mut rng = rand::thread_rng();
        let rnd_index = rng.gen_range(0..=self.uppercase_char_set.len() - 1);

        self.uppercase_char_set[rnd_index]
    }

    /// Returns a random number char
    fn get_random_number(&self) -> char {
        let mut rng = rand::thread_rng();
        let rnd_index = rng.gen_range(0..=self.number_set.len() - 1);

        self.number_set[rnd_index]
    }

    /// Returns a random special character char
    fn get_random_special_character(&self) -> char {
        let mut rng = rand::thread_rng();
        let rnd_index = rng.gen_range(0..=self.spec_char_set.len() - 1);

        self.spec_char_set[rnd_index]
    }

    /// Generates a random composition vec
    fn generate_random_composition(&self) -> Vec<&CompositionCodes> {
        let mut result: Vec<&CompositionCodes> = Vec::new();
        let mut rng = rand::thread_rng();
        let filtered_comp_codes = self
            .composition_codes
            .iter()
            .filter(|code| match code {
                CompositionCodes::Lowercase => {
                    if self.uppercase_only {
                        false
                    } else {
                        true
                    }
                }
                CompositionCodes::Uppercase => {
                    if self.lowercase_only {
                        false
                    } else {
                        true
                    }
                }
                CompositionCodes::Number => {
                    if self.exclude_numbers {
                        false
                    } else {
                        true
                    }
                }
                CompositionCodes::SpecialCharacter => {
                    if self.exclude_special_chars {
                        false
                    } else {
                        true
                    }
                }
            })
            .collect::<Vec<_>>();

        for _i in 0..self.length {
            let rnd_index = rng.gen_range(0..=filtered_comp_codes.len() - 1);
            let comp_char = filtered_comp_codes[rnd_index];

            result.push(comp_char)
        }

        result
    }

    /// Generates a random string following the input composition vec
    fn generate_random_string_from_composition(
        &self,
        composition: Vec<&CompositionCodes>,
    ) -> String {
        let mut password: Vec<char> = Vec::new();

        for code in composition {
            match code {
                CompositionCodes::Lowercase => {
                    let value = self.get_random_lowercase_char();
                    password.push(value);
                }
                CompositionCodes::Uppercase => {
                    let value = self.get_random_uppercase_char();
                    password.push(value);
                }
                CompositionCodes::Number => {
                    let value = self.get_random_number();
                    password.push(value);
                }
                CompositionCodes::SpecialCharacter => {
                    let value = self.get_random_special_character();
                    password.push(value);
                }
            }
        }

        password.into_iter().collect()
    }

    /// Generates a password
    ///
    /// # Examples
    ///
    /// ```
    /// use simple_password_generator::PasswordGenerator;
    ///
    /// let password = PasswordGenerator::new().generate();
    /// ```
    pub fn generate(&self) -> String {
        let comp_code = self.generate_random_composition();
        let password = self.generate_random_string_from_composition(comp_code);

        password
    }
}