use rand::Rng;
const NUMBER: &'static [u8] = b"0123456789";
const SYMBOL: &'static [u8] = b"~!@#$%^&*()_+-={}[]|:;<>,.?/\"\\";
const LETTER: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
pub struct Custom {
length: usize,
kind: CharSetKind,
}
pub enum CharSetKind {
Number,
Letter,
Symbol,
NumberAndLetter,
NumberAndSymbol,
LetterAndSymbol,
NumberLetterAndSymbol,
}
impl Custom {
pub fn new(length: usize, kind: CharSetKind) -> Custom {
Custom { length, kind }
}
pub fn generate(&self) -> String {
let charset = match self.kind {
CharSetKind::Number => NUMBER.to_vec(),
CharSetKind::Letter => LETTER.to_vec(),
CharSetKind::Symbol => SYMBOL.to_vec(),
CharSetKind::NumberAndLetter => [NUMBER, LETTER].concat().to_vec(),
CharSetKind::NumberAndSymbol => [NUMBER, SYMBOL].concat().to_vec(),
CharSetKind::LetterAndSymbol => [LETTER, SYMBOL].concat().to_vec(),
CharSetKind::NumberLetterAndSymbol => [NUMBER, LETTER, SYMBOL].concat().to_vec(),
};
let mut rng = rand::thread_rng();
let value: String = (0..self.length)
.map(|_| {
let idx = rng.gen_range(0..charset.len());
charset[idx] as char
})
.collect();
value
}
}