Smarter arg setup
This commit is contained in:
parent
9b2044a00a
commit
61346fcf10
@ -1,10 +1,7 @@
|
||||
//! A simple and efficient library to make dice rolls into easy to use values.
|
||||
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;
|
||||
|
15
src/roll.rs
15
src/roll.rs
@ -53,16 +53,26 @@ impl Roll {
|
||||
self.sum()
|
||||
}
|
||||
|
||||
/// Takes arguments and turns them into a `Roll` if possible,
|
||||
/// failing that it returns an error to be handled.
|
||||
/// Keeps taking args until less than 2 remain so it returns a
|
||||
/// `Vec<Roll>`
|
||||
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();
|
||||
|
||||
let mut total: Vec<Roll> = vec![];
|
||||
|
||||
// loop that checks that there's enough elements in args remaining
|
||||
// to make a `Roll` from.
|
||||
while args.len() >= 2 {
|
||||
// Collect 2 args into a `Vec<String>` and turn them into
|
||||
// a `Roll`
|
||||
let roll: Vec<String> = args.take(2).collect();
|
||||
total.push(Roll::new(
|
||||
roll[0].parse::<usize>()?,
|
||||
roll[1].parse::<usize>()?,
|
||||
)
|
||||
);
|
||||
));
|
||||
}
|
||||
Ok(total)
|
||||
}
|
||||
@ -126,4 +136,3 @@ fn totaling() {
|
||||
assert!(y.total() <= 60 && y.total() >= 3);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user