Code cleanup and more docs/tests

This commit is contained in:
Beefki 2017-11-05 09:47:08 -06:00
parent 1c185c22a8
commit ed76fb2786
2 changed files with 32 additions and 11 deletions

View File

@ -1,10 +1,8 @@
extern crate libroller; extern crate libroller;
//use libroller::config::Config;
use libroller::roll::Roll; use libroller::roll::Roll;
use std::env; use std::env;
fn main() { fn main() {
// let roll = Config::new(&mut env::args());
let rolls = Roll::from_args(&mut env::args()).unwrap_or_else(|err| { let rolls = Roll::from_args(&mut env::args()).unwrap_or_else(|err| {
eprintln!("Error occured:{}", err); eprintln!("Error occured:{}", err);
std::process::exit(1) std::process::exit(1)

View File

@ -24,7 +24,9 @@ impl Roll {
/// Returns the value stored in the number field of a roll. /// Returns the value stored in the number field of a roll.
/// Allows the fields of `Roll` to be private (and unchangable) /// Allows the fields of `Roll` to be private (and unchangable)
/// while still allowing access to act on their internal value /// while still allowing access to act on their internal value
/// ##EXAMPLE
/// ``` /// ```
/// use libroller::Roll;
/// let x = Roll::new(2,6); /// let x = Roll::new(2,6);
/// assert!(x.number() == 2); /// assert!(x.number() == 2);
/// ``` /// ```
@ -34,6 +36,12 @@ impl Roll {
/// Returns the value stored in the die field of a roll. /// Returns the value stored in the die field of a roll.
/// Allows the fields of `Roll` to be private (and unchangable) /// Allows the fields of `Roll` to be private (and unchangable)
/// while still allowing access to act on their internal value /// while still allowing access to act on their internal value
/// ##EXAMPLE
/// ```
/// use libroller::Roll;
/// let x = Roll::new(2,6);
/// assert!(x.die() == 6);
/// ```
pub fn die(&self) -> usize { pub fn die(&self) -> usize {
self.die self.die
} }
@ -41,6 +49,11 @@ impl Roll {
/// a Vector ensures the ability to work on exactly the same /// a Vector ensures the ability to work on exactly the same
/// die rolls easily and accurately, since calling the `Roll` /// die rolls easily and accurately, since calling the `Roll`
/// iterator again will produce new results. Consumes the `Roll` /// iterator again will produce new results. Consumes the `Roll`
/// ##EXAMPLE
/// ```
/// use libroller::Roll;
/// let x = Roll::new(2,6).rolls();
/// ```
pub fn rolls(self) -> Vec<usize> { pub fn rolls(self) -> Vec<usize> {
self.collect() self.collect()
} }
@ -48,6 +61,11 @@ impl Roll {
/// Used on a `Roll` to return only the final value, ignoring /// Used on a `Roll` to return only the final value, ignoring
/// the values used to add up to that result. Allows `.total()` /// the values used to add up to that result. Allows `.total()`
/// syntax when used. Consumes the `Roll`. /// syntax when used. Consumes the `Roll`.
/// ##EXAMPLE
/// ```
/// use libroller::Roll;
/// let x = Roll::new(2,6).total();
/// ```
pub fn total(self) -> usize { pub fn total(self) -> usize {
self.sum() self.sum()
} }
@ -56,6 +74,11 @@ impl Roll {
/// failing that it returns an error to be handled. /// failing that it returns an error to be handled.
/// Keeps taking args until less than 2 remain so it returns a /// Keeps taking args until less than 2 remain so it returns a
/// `Vec<Roll>` /// `Vec<Roll>`
/// ##EXAMPLE
/// ```
/// use libroller::Roll;
/// let x = Roll::from_args(&mut std::env::args()).unwrap_or(vec![]);
/// ```
pub fn from_args(args: &mut std::env::Args) -> Result<Vec<Roll>, ParseIntError> { pub fn from_args(args: &mut std::env::Args) -> Result<Vec<Roll>, ParseIntError> {
//first arg is the path to the executable so skip it //first arg is the path to the executable so skip it
args.next(); args.next();
@ -96,8 +119,8 @@ impl Iterator for Roll {
/// Defines a `Display` format for the `Roll` type. Makes it much easier /// Defines a `Display` format for the `Roll` type. Makes it much easier
///to use the `Roll` type as you should be able to just use ///to use the `Roll` type as you should be able to just use
///``` /// `println!("{}", Roll::new(2,6));`
/// ``` /// and have `2d6` be returned.
impl fmt::Display for Roll { impl fmt::Display for Roll {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}d{}", self.number, self.die) write!(f, "{}d{}", self.number, self.die)