Add documentation.
This commit is contained in:
parent
cf8d03cece
commit
4059e5002d
@ -9,9 +9,9 @@ path = "src/lib.rs"
|
|||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "names"
|
name = "names"
|
||||||
|
doc = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
# pending a release which includes https://github.com/kbknapp/clap-rs/pull/257
|
# pending a release which includes https://github.com/kbknapp/clap-rs/pull/257
|
||||||
clap = { git = "https://github.com/kbknapp/clap-rs", rev = "40104af" }
|
clap = { git = "https://github.com/kbknapp/clap-rs", rev = "40104af" }
|
||||||
rand = "0.3.0"
|
rand = "0.3.0"
|
||||||
|
|
||||||
|
95
src/lib.rs
95
src/lib.rs
@ -1,3 +1,65 @@
|
|||||||
|
//! This crate provides a generate that constructs random name strings suitable
|
||||||
|
//! for use in container instances, project names, application instances, etc.
|
||||||
|
//!
|
||||||
|
//! # Usage
|
||||||
|
//!
|
||||||
|
//! This crate is [on crates.io](https://crates.io/crates/names) and can be
|
||||||
|
//! used by adding `names` to your dependencies in your project's `Cargo.toml`
|
||||||
|
//! file:
|
||||||
|
//!
|
||||||
|
//! ```toml
|
||||||
|
//! [dependencies]
|
||||||
|
//! names = "0.1.0"
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
//! and this to your crate root:
|
||||||
|
//!
|
||||||
|
//! ```
|
||||||
|
//! extern crate names;
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
//! # Example: painless defaults
|
||||||
|
//!
|
||||||
|
//! The easiest way to get started is to use the default `Generator` to return
|
||||||
|
//! a name:
|
||||||
|
//!
|
||||||
|
//! ```
|
||||||
|
//! use names::Generator;
|
||||||
|
//!
|
||||||
|
//! let generator = Generator::default();
|
||||||
|
//! println!("Your project is: {}", generator.name());
|
||||||
|
//! // #=> "rusty-nail"
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
//! If more randomness is required, you can generate a name with a trailing
|
||||||
|
//! 4-digit number:
|
||||||
|
//!
|
||||||
|
//! ```
|
||||||
|
//! use names::Generator;
|
||||||
|
//!
|
||||||
|
//! let generator = Generator::default();
|
||||||
|
//! println!("Your project is: {}", generator.name_with_number());
|
||||||
|
//! // #=> "pushy-pencil-5602"
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
//! # Example: with custom dictionaries
|
||||||
|
//!
|
||||||
|
//! If you would rather supply your own custom adjective and noun word lists,
|
||||||
|
//! you can provide your own by supplying 2 `Dictionary` structs. For example,
|
||||||
|
//! this returns only one result:
|
||||||
|
//!
|
||||||
|
//! ```
|
||||||
|
//! use names::{Dictionary, Generator};
|
||||||
|
//!
|
||||||
|
//! let adjectives = &["imaginary"];
|
||||||
|
//! let nouns = &["roll"];
|
||||||
|
//! let generator = Generator::new(
|
||||||
|
//! Dictionary::new(adjectives),
|
||||||
|
//! Dictionary::new(nouns));
|
||||||
|
//!
|
||||||
|
//! assert_eq!("imaginary-roll", generator.name());
|
||||||
|
//! ```
|
||||||
|
|
||||||
extern crate rand;
|
extern crate rand;
|
||||||
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
@ -5,6 +67,7 @@ use rand::Rng;
|
|||||||
mod adjectives;
|
mod adjectives;
|
||||||
mod nouns;
|
mod nouns;
|
||||||
|
|
||||||
|
/// A `Dictionary` is collection of words.
|
||||||
pub struct Dictionary<'a> {
|
pub struct Dictionary<'a> {
|
||||||
words: &'a [&'a str],
|
words: &'a [&'a str],
|
||||||
}
|
}
|
||||||
@ -19,16 +82,48 @@ impl<'a> Dictionary<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A random name generator which combines an adjective, a noun, and an
|
||||||
|
/// optional number.
|
||||||
|
///
|
||||||
|
/// A `Generator` takes a `Dictionary` of adjectives and a `Dictionary` of
|
||||||
|
/// nouns.
|
||||||
pub struct Generator<'a> {
|
pub struct Generator<'a> {
|
||||||
adjectives: Dictionary<'a>,
|
adjectives: Dictionary<'a>,
|
||||||
nouns: Dictionary<'a>,
|
nouns: Dictionary<'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Generator<'a> {
|
impl<'a> Generator<'a> {
|
||||||
|
/// Constructs a new `Generator<'a>`.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use names::{Dictionary, Generator};
|
||||||
|
///
|
||||||
|
/// let adjective_words = &["sassy"];
|
||||||
|
/// let noun_words = &["clocks"];
|
||||||
|
///
|
||||||
|
/// let adjectives = Dictionary::new(adjective_words);
|
||||||
|
/// let nouns = Dictionary::new(noun_words);
|
||||||
|
///
|
||||||
|
/// let generator = Generator::new(adjectives, nouns);
|
||||||
|
///
|
||||||
|
/// assert_eq!("sassy-clocks", generator.name());
|
||||||
|
/// ```
|
||||||
pub fn new(adjectives: Dictionary<'a>, nouns: Dictionary<'a>) -> Generator<'a> {
|
pub fn new(adjectives: Dictionary<'a>, nouns: Dictionary<'a>) -> Generator<'a> {
|
||||||
Generator { adjectives: adjectives, nouns: nouns }
|
Generator { adjectives: adjectives, nouns: nouns }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Construct and returns a default `Generator<'a>` containing a large
|
||||||
|
/// collection of adjectives and nouns.
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use names::Generator;
|
||||||
|
///
|
||||||
|
/// let generator = Generator::default();
|
||||||
|
///
|
||||||
|
/// println!("My new name is: {}", generator.name());
|
||||||
|
/// ```
|
||||||
pub fn default() -> Generator<'a> {
|
pub fn default() -> Generator<'a> {
|
||||||
Generator::new(
|
Generator::new(
|
||||||
Dictionary::new(adjectives::LIST),
|
Dictionary::new(adjectives::LIST),
|
||||||
|
Loading…
Reference in New Issue
Block a user