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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//! This library uses the authors from Anarchist Library to generate a random name
//!
//! The intention here is to have a random name for situations you need to spin up some cloud compute resources and don't have a name in mind. Typically, for throwaway purposes.
//!
//! # Examples
//!
//! It's possible to simply generate a random name
//!
//! ```
//! use anarchist_readable_name_generator_lib::readable_name;
//!
//! assert!(readable_name().len() > 0)
//! ```
//!
//! You can also pass a seed or change the separator to give you predictability or minor customization.
//!
//! ```
//! use anarchist_readable_name_generator_lib::readable_name_custom;
//! use rand::prelude::*;
//! use rand_pcg::Pcg64;
//!
//! let rng = Pcg64::seed_from_u64(2);
//! assert_eq!(
//!    readable_name_custom("+", rng),
//!    "engrossing+cazarabet"
//! );
//! ```

#![warn(
    rust_2018_idioms,
    unused,
    rust_2021_compatibility,
    nonstandard_style,
    future_incompatible,
    missing_copy_implementations,
    missing_debug_implementations,
    missing_docs
)]

mod names;

use names::ADJECTIVES;
use names::NAMES;
use rand::prelude::*;
use rand::seq::SliceRandom;

/// Generate a readable name with some customization
///
/// # Examples
///
/// ```
/// use anarchist_readable_name_generator_lib::readable_name_custom;
/// use rand::prelude::*;
/// use rand_pcg::Pcg64;
///
/// let rng = Pcg64::seed_from_u64(2);
/// assert_eq!(
///    readable_name_custom("+", rng),
///    "engrossing+cazarabet"
/// );
/// ```
#[must_use]
pub fn readable_name_custom<R: Rng>(seperator: &str, mut rng: R) -> String {
    format!(
        "{}{}{}",
        ADJECTIVES
            .choose(&mut rng)
            .expect("This should never fail, our list is predefined"),
        seperator,
        NAMES
            .choose(&mut rng)
            .expect("This should never fail, our list is predefined")
    )
}

/// Generate a readable name with some customization
///
/// # Examples
///
/// ```
/// use anarchist_readable_name_generator_lib::readable_name;
///
/// assert!(readable_name().len() > 0)
/// ```
#[must_use]
pub fn readable_name() -> String {
    let rng = thread_rng();
    readable_name_custom("_", rng)
}

#[cfg(test)]
mod test_readable_name_custom {
    use super::readable_name_custom;

    use rand::prelude::*;
    use rand_pcg::Pcg64;

    #[test]
    fn it_generates_a_name_with_a_custom_separator() {
        let rng = thread_rng();
        let split = readable_name_custom("-", rng)
            .split('-')
            .map(String::from)
            .collect::<Vec<_>>();
        assert!(!split.get(0).unwrap().is_empty());
        assert!(!split.get(1).unwrap().is_empty());
        assert_eq!(split.len(), 2);
    }

    #[test]
    fn it_can_be_made_predictable_with_a_known_seed() {
        let rng_1 = Pcg64::seed_from_u64(2);
        let rng_2 = Pcg64::seed_from_u64(2);
        assert_eq!(
            readable_name_custom("_", rng_1),
            readable_name_custom("_", rng_2)
        );
    }
}

#[cfg(doctest)]
mod test_readme {
    macro_rules! external_doc_test {
        ($x:expr) => {
            #[doc = $x]
            extern "C" {}
        };
    }

    external_doc_test!(include_str!("../README.md"));
}