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
use rand::seq::SliceRandom;
use rand::Rng;


const LOWERCASE_ALPHABET: &[u8] = b"abcdefghijklmnopqrstuvwxyz";
const LOWERCASE_ALPHNUMERIC: &[u8] = b"abcdefghijklmnopqrstuvwxyz0123456789";


// todo: remove `TRandom: 'a` when lifetime reform is implemented.
pub struct RandomCharacter<'a, TRandom: 'a> {
    random:   &'a mut TRandom,
    alphabet: &'a [char],
}

impl<TRandom> Iterator for RandomCharacter<'_, TRandom>
where
    TRandom: Rng,
{
    type Item = char;

    fn next(&mut self) -> Option<char> {
        self.alphabet.choose(&mut self.random).map(|x| *x as char)
    }
}


pub struct RandomAlphabet<'a, TRandom: 'a> {
    random: &'a mut TRandom,
}

impl<TRandom> Iterator for RandomAlphabet<'_, TRandom>
where
    TRandom: Rng,
{
    type Item = char;

    fn next(&mut self) -> Option<char> {
        LOWERCASE_ALPHABET.choose(&mut self.random).map(|x| *x as char)
    }
}


pub struct RandomAlphanumeric<'a, TRandom: 'a> {
    random: &'a mut TRandom,
}

impl<TRandom> Iterator for RandomAlphanumeric<'_, TRandom>
where
    TRandom: Rng,
{
    type Item = char;

    fn next(&mut self) -> Option<char> {
        LOWERCASE_ALPHNUMERIC.choose(&mut self.random).map(|x| *x as char)
    }
}


pub fn string(alphabet: &[char], length: usize) -> String {
    RandomCharacter {
        alphabet: alphabet,
        random:   &mut rand::thread_rng(),
    }
    .take(length)
    .collect()
}

pub fn alpha_string(length: usize) -> String {
    RandomAlphabet {
        random: &mut rand::thread_rng(),
    }
    .take(length)
    .collect()
}

pub fn alphanumeric_string(length: usize) -> String {
    RandomAlphanumeric {
        random: &mut rand::thread_rng(),
    }
    .take(length)
    .collect()
}

pub fn vec(length: usize) -> Vec<u8> {
    let mut buffer = vec![0; length];
    let mut random = rand::thread_rng();

    random.fill(&mut buffer[..]);
    buffer
}

// todo: use const generics.

pub fn array_16() -> [u8; 16] {
    rand::random::<[u8; 16]>()
}

pub fn array_32() -> [u8; 32] {
    rand::random::<[u8; 32]>()
}