codephrases 0.0.1

Generate human memorizable codephrases
Documentation
//! `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);
        }
    }
}