1use rand::Rng;
7
8const NUMBER: &'static [u8] = b"0123456789";
9const SYMBOL: &'static [u8] = b"~!@#$%^&*()_+-={}[]|:;<>,.?/\"\\";
10const LETTER: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
11
12pub struct Custom {
14 length: usize,
16 kind: CharSetKind,
18}
19
20pub enum CharSetKind {
22 Number,
23 Letter,
24 Symbol,
25 NumberAndLetter,
26 NumberAndSymbol,
27 LetterAndSymbol,
28 NumberLetterAndSymbol,
29}
30
31impl Custom {
32 pub fn new(length: usize, kind: CharSetKind) -> Custom {
33 Custom { length, kind }
34 }
35
36 pub fn generate(&self) -> String {
45 let charset = match self.kind {
46 CharSetKind::Number => NUMBER.to_vec(),
47 CharSetKind::Letter => LETTER.to_vec(),
48 CharSetKind::Symbol => SYMBOL.to_vec(),
49 CharSetKind::NumberAndLetter => [NUMBER, LETTER].concat().to_vec(),
50 CharSetKind::NumberAndSymbol => [NUMBER, SYMBOL].concat().to_vec(),
51 CharSetKind::LetterAndSymbol => [LETTER, SYMBOL].concat().to_vec(),
52 CharSetKind::NumberLetterAndSymbol => [NUMBER, LETTER, SYMBOL].concat().to_vec(),
53 };
54
55 let mut rng = rand::thread_rng();
56
57 let value: String = (0..self.length)
58 .map(|_| {
59 let idx = rng.gen_range(0..charset.len());
60 charset[idx] as char
61 })
62 .collect();
63
64 value
65 }
66}
67