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
//! `codephrases`
//!
//! Generate easy to memorize codephrases for sharing authentication. The word lists are based off
//! the room name generator for [Jitsi
//! Meet](https://github.com/jitsi/js-utils/blob/2639462719185599bc54e15233f9154e2d016a82/random/roomNameGenerator.js).
//!
//! Note that this library has insufficient entropy for generating passphrases. You probably want
//! diceware for that.
//!
//! ## Example
//!
//! ```
//! use codephrases::random_codephrase;
//!
//! let phrase = random_codephrase();
//! // "PinkContradictionsYellFast"
//! ```
use rand::seq::SliceRandom;
use rand::Rng;

mod adjectives;
mod adverbs;
mod nouns;
mod verbs;

/// Generate a random codephrase.
pub fn random_codephrase() -> String {
    let mut rng = rand::thread_rng();
    random_codephrase_from_rng(&mut rng)
}

/// Generate a random codephrase using the specified RNG.
pub fn random_codephrase_from_rng<R: ?Sized>(rng: &mut R) -> String
where
    R: Rng,
{
    let mut out = String::new();
    out.push_str(adjectives::ADJECTIVES.choose(rng).unwrap());
    out.push_str(nouns::NOUNS.choose(rng).unwrap());
    out.push_str(verbs::VERBS.choose(rng).unwrap());
    out.push_str(adverbs::ADVERBS.choose(rng).unwrap());
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn check_random_codephrase() {
        let out = random_codephrase();
        assert!(out.len() > 10);
    }

    #[test]
    fn check_random_codephrase_from_rng() {
        let mut rng = rand::thread_rng();
        for _ in 0..10000 {
            let out = random_codephrase_from_rng(&mut rng);
            assert!(out.len() > 10);
        }
    }
}