adjective-adjective-animal/src/lib.rs

116 lines
3.1 KiB
Rust
Raw Normal View History

2018-08-07 00:08:48 -04:00
//! This crate provides a generate that constructs suitably random and reasonably unique human
//! readable (and fairly adorable) ids, ala GiphyCat.
2015-09-21 14:49:30 -04:00
//!
//! The name `Generator` implements the `Iterator` trait so it can be used with
//! adapters, consumers, and in loops.
//!
2015-09-21 14:49:30 -04:00
//! # Usage
//!
2018-08-07 00:08:48 -04:00
//! This crate is [on crates.io](https://crates.io/crates/adjective_adjective_animal) and can be
//! used by adding `adjective_adjective_animal` to your dependencies in your project's `Cargo.toml`
2015-09-21 14:49:30 -04:00
//! file:
//!
//! ```toml
//! [dependencies]
2018-08-07 00:08:48 -04:00
//! adjective_adjective_animal = "0.9.0"
2015-09-21 14:49:30 -04:00
//! ```
//!
//! and this to your crate root:
//!
//! ```
2018-08-07 00:08:48 -04:00
//! extern crate adjective_adjective_animal;
2015-09-21 14:49:30 -04:00
//! ```
//!
//! # Example: painless defaults
//!
//! The easiest way to get started is to use the default `Generator` to return
//! a name:
//!
//! ```
2018-08-07 00:08:48 -04:00
//! use adjective_adjective_animal::Generator;
2015-09-21 14:49:30 -04:00
//!
//! let mut generator = Generator::default();
//! println!("Your project is: {}", generator.next().unwrap());
2018-08-07 00:08:48 -04:00
//! // #=> "Your project is: IndustrialSecretiveSwan"
2015-09-21 14:49:30 -04:00
//! ```
//!
//! # Example: with custom dictionaries
//!
2018-08-07 00:08:48 -04:00
//! If you would rather supply your own custom adjective and animal word lists,
//! you can provide your own by supplying 2 string slices. For example,
2015-09-21 14:49:30 -04:00
//! this returns only one result:
//!
//! ```
2018-08-07 00:08:48 -04:00
//! use adjective_adjective_animal::Generator;
2015-09-21 14:49:30 -04:00
//!
2018-08-07 00:08:48 -04:00
//! let adjectives = &["Imaginary"];
//! let animals = &["Bear"];
//! let mut generator = Generator::new(adjectives, animals);
2015-09-21 14:49:30 -04:00
//!
2018-08-07 00:08:48 -04:00
//! assert_eq!("ImaginaryImaginaryBear", generator.next().unwrap());
2015-09-21 14:49:30 -04:00
//! ```
2015-09-19 16:17:59 -04:00
extern crate rand;
use rand::Rng;
pub const ADJECTIVES: &'static [&'static str] =
&include!(concat!(env!("OUT_DIR"), "/adjectives.rs"));
2018-08-07 00:08:48 -04:00
pub const ANIMALS: &'static [&'static str] = &include!(concat!(env!("OUT_DIR"), "/animals.rs"));
2018-08-07 00:08:48 -04:00
/// A random name generator which combines an adjective, a animal, and an
/// optional number
2015-09-21 14:49:30 -04:00
///
2018-08-07 00:08:48 -04:00
/// A `Generator` takes a slice of adjective and animal words strings and has
/// a naming strategy (with or without a number appended).
2015-09-19 16:17:59 -04:00
pub struct Generator<'a> {
adjectives: &'a [&'a str],
2018-08-07 00:08:48 -04:00
animals: &'a [&'a str],
2015-09-19 16:17:59 -04:00
}
impl<'a> Generator<'a> {
/// Constructs a new `Generator<'a>`
2015-09-21 14:49:30 -04:00
///
/// # Examples
///
/// ```
2018-08-07 00:08:48 -04:00
/// use adjective_adjective_animal::Generator;
2015-09-21 14:49:30 -04:00
///
2018-08-07 00:08:48 -04:00
/// let adjectives = &["Sassy"];
/// let animals = &["Fox"];
2015-09-21 14:49:30 -04:00
///
2018-08-07 00:08:48 -04:00
/// let mut generator = Generator::new(adjectives, animals);
2015-09-21 14:49:30 -04:00
///
2018-08-07 00:08:48 -04:00
/// assert_eq!("SassySassyFox", generator.next().unwrap());
2015-09-21 14:49:30 -04:00
/// ```
2018-08-07 00:08:48 -04:00
pub fn new(adjectives: &'a [&'a str], animals: &'a [&'a str]) -> Self {
Generator {
adjectives: adjectives,
2018-08-07 00:08:48 -04:00
animals: animals,
}
2015-09-19 16:17:59 -04:00
}
fn rand_adj(&self) -> &str {
rand::thread_rng().choose(self.adjectives).unwrap()
2015-09-19 16:17:59 -04:00
}
2018-08-07 00:08:48 -04:00
fn rand_animal(&self) -> &str {
rand::thread_rng().choose(self.animals).unwrap()
}
}
impl<'a> Default for Generator<'a> {
fn default() -> Self {
2018-08-07 00:08:48 -04:00
Generator::new(ADJECTIVES, ANIMALS)
}
}
impl<'a> Iterator for Generator<'a> {
type Item = String;
fn next(&mut self) -> Option<String> {
2018-08-07 00:08:48 -04:00
Some(format!("{}{}{}", self.rand_adj(), self.rand_adj(), self.rand_animal()))
2015-09-19 16:17:59 -04:00
}
}