diff --git a/src/bin/main.rs b/src/bin/main.rs index c8fbb1e..472202f 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -1,27 +1,27 @@ extern crate libroller; -use libroller::config::Config; +//use libroller::config::Config; +use libroller::roll::Roll; use std::env; fn main() { - let roll = Config::new(&mut env::args()); - for rolls in &roll.unwrap_or_else(|e| {println!("Application error: {}", e); - std::process::exit(1)}) - { - let x = rolls.to_roll().unwrap_or_else(|e| { println!("Application error: {}", e); - std::process::exit(1); - }); - if x.number() > 1 && x.number() < 26 { +// 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) + }); + for roll in rolls { + if roll.number() > 1 && roll.number() < 26 { let mut total = 0; - println!("You're rolling {}", x); - for value in x.rolls() { + println!("You're rolling {}", roll); + for value in roll.rolls() { println!("You rolled: {}", value); total += value; } println!("Total value of dice = {}", total); println!(""); }else { - println!("You're rolling {}", x); - println!("Total: {}", x.total()) + println!("You're rolling {}", roll); + println!("Total: {}", roll.total()) } } } diff --git a/src/config.rs b/src/config.rs index 9791eab..1773a7c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -52,8 +52,8 @@ impl Config { /// Converts a `Config` into a `Roll`. Returns a `Result` that's /// either `Ok(Roll)` or an `Err(_)` pub fn to_roll(&self) -> Result { - let number = self.first.parse::()?; - let die = self.second.parse::()?; + let number = self.first.parse::()?; + let die = self.second.parse::()?; Ok(Roll::new(number, die)) } diff --git a/src/lib.rs b/src/lib.rs index 952b0e9..d041eef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,9 @@ pub extern crate rand; pub mod roll; pub mod config; +//pub mod error; pub use roll::*; pub use roll::Roll; +pub use std::{num, io}; +//pub use error::RollError; diff --git a/src/roll.rs b/src/roll.rs index e81ede4..5ae7115 100644 --- a/src/roll.rs +++ b/src/roll.rs @@ -1,19 +1,21 @@ //! A struct in which our data is held for rolling. By ensuring that -//! only `u32`s are used we prevent the need for additional type +//! only `usize`s are used we prevent the need for additional type //! checking at time of use. */ extern crate rand; use rand::Rng; use std::fmt; +use std::num::ParseIntError; +use super::std; #[derive(Debug, Clone, PartialEq)] pub struct Roll { - die: u32, - number: u32, + die: usize, + number: usize, } impl Roll { /// A fn to create new `Roll` structs, private to prevent /// being used outside the library. - pub fn new(number: u32, die: u32) -> Roll { + pub fn new(number: usize, die: usize) -> Roll { Roll { die: die, number: number, @@ -27,37 +29,51 @@ impl Roll { /// let x = Roll::new(&2, &6); /// assert!(x.number() == 2); /// ``` - pub fn number(&self) -> u32 { + pub fn number(&self) -> usize { self.number } /// 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 - pub fn die(&self) -> u32 { + pub fn die(&self) -> usize { self.die } /// Creates a Vector to hold each of the values rolled. Making ///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. - pub fn rolls(self) -> Vec { + pub fn rolls(self) -> Vec { self.collect() } /// 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 - pub fn total(self) -> u32 { + pub fn total(self) -> usize { self.sum() } + + pub fn from_args(args: &mut std::env::Args) -> Result, ParseIntError> { + args.next(); + let mut total: Vec = vec![]; + while args.len() >= 2 { + let roll: Vec = args.take(2).collect(); + total.push(Roll::new( + roll[0].parse::()?, + roll[1].parse::()?, + ) + ); + } + Ok(total) + } } /// Turns `Roll` into an `Iterator`, causing it to create a series of random ///numbers between 1 and the chosen die size. It counts down the number field ///of the `Roll` and returns `None` to end the iteration. It's only capable of -///returning u32 values, which should never be a problem. +///returning usize values, which should never be a problem. impl Iterator for Roll { - type Item = u32; + type Item = usize; fn next(&mut self) -> Option { if self.number != 0 {