gen_random/
lib.rs

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
//! # Gen Random
//!
//! `gen_random` is a lib for generating random number by custom its length and kind.
//! It's very easy to generate different randon number, Number/Letter/Symbol or collection of them.

use rand::Rng;

const NUMBER: &'static [u8] = b"0123456789";
const SYMBOL: &'static [u8] = b"~!@#$%^&*()_+-={}[]|:;<>,.?/\"\\";
const LETTER: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

/// Custom length and kind of random number
pub struct Custom {
    /// Length of random number
    length: usize,
    /// Kind of random number
    kind: CharSetKind,
}

/// Kinds of random number
pub enum CharSetKind {
    Number,
    Letter,
    Symbol,
    NumberAndLetter,
    NumberAndSymbol,
    LetterAndSymbol,
    NumberLetterAndSymbol,
}

impl Custom {
    pub fn new(length: usize, kind: CharSetKind) -> Custom {
        Custom { length, kind }
    }

    /// Generate random number by custom its length and kind
    ///
    /// # Examples
    ///
    /// ```
    ///  let random = gen_random::Custom::new(5, gen_random::CharSetKind::Number).generate();
    ///  assert_eq!(random, "12345".to_string())
    /// ```
    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
    }
}