Code cleanup and more docs/tests
This commit is contained in:
parent
1c185c22a8
commit
ed76fb2786
@ -1,10 +1,8 @@
|
||||
extern crate libroller;
|
||||
//use libroller::config::Config;
|
||||
use libroller::roll::Roll;
|
||||
use std::env;
|
||||
|
||||
fn main() {
|
||||
// let roll = Config::new(&mut env::args());
|
||||
let rolls = Roll::from_args(&mut env::args()).unwrap_or_else(|err| {
|
||||
eprintln!("Error occured:{}", err);
|
||||
std::process::exit(1)
|
||||
|
27
src/roll.rs
27
src/roll.rs
@ -24,7 +24,9 @@ impl Roll {
|
||||
/// Returns the value stored in the number field of a roll.
|
||||
/// Allows the fields of `Roll` to be private (and unchangable)
|
||||
/// while still allowing access to act on their internal value
|
||||
/// ##EXAMPLE
|
||||
/// ```
|
||||
/// use libroller::Roll;
|
||||
/// let x = Roll::new(2,6);
|
||||
/// assert!(x.number() == 2);
|
||||
/// ```
|
||||
@ -34,6 +36,12 @@ impl Roll {
|
||||
/// Returns the value stored in the die field of a roll.
|
||||
/// Allows the fields of `Roll` to be private (and unchangable)
|
||||
/// 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 {
|
||||
self.die
|
||||
}
|
||||
@ -41,6 +49,11 @@ impl Roll {
|
||||
/// a Vector ensures the ability to work on exactly the same
|
||||
/// die rolls easily and accurately, since calling 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> {
|
||||
self.collect()
|
||||
}
|
||||
@ -48,6 +61,11 @@ impl Roll {
|
||||
/// Used on a `Roll` to return only the final value, ignoring
|
||||
/// the values used to add up to that result. Allows `.total()`
|
||||
/// syntax when used. Consumes the `Roll`.
|
||||
/// ##EXAMPLE
|
||||
/// ```
|
||||
/// use libroller::Roll;
|
||||
/// let x = Roll::new(2,6).total();
|
||||
/// ```
|
||||
pub fn total(self) -> usize {
|
||||
self.sum()
|
||||
}
|
||||
@ -56,6 +74,11 @@ impl Roll {
|
||||
/// failing that it returns an error to be handled.
|
||||
/// Keeps taking args until less than 2 remain so it returns a
|
||||
/// `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> {
|
||||
//first arg is the path to the executable so skip it
|
||||
args.next();
|
||||
@ -96,8 +119,8 @@ impl Iterator for Roll {
|
||||
|
||||
/// 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
|
||||
///```
|
||||
/// ```
|
||||
/// `println!("{}", Roll::new(2,6));`
|
||||
/// and have `2d6` be returned.
|
||||
impl fmt::Display for Roll {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}d{}", self.number, self.die)
|
||||
|
Loading…
x
Reference in New Issue
Block a user