Don't need Config anymore

This commit is contained in:
Beefki 2017-11-02 11:33:53 -05:00
parent 1aae111af1
commit 9b2044a00a

View File

@ -1,68 +0,0 @@
//! Creates a `Config` struct to be used to gather command line
//! arguments at the time the program is called, so it can be
//! converted easily to a `Roll`
use super::*;
use std::num::ParseIntError;
#[derive(Debug)]
pub struct Config {
first: String,
second: String,
}
impl Config {
/// Takes the arguments passed at launch and returns an Ok(_)
/// or a str with a static lifetime. This allows for error
/// handling whenever this is called.
///
/// It checks to see if there are enough arguments passed to make
/// a proper `Config` and if not returns an `Err(&str)` that
/// explains what happened to the user.
///
/// #Errors
///
/// `Config::new(args)` will error when the args given don't match up with
/// the required types or when not enough args are passed to create a `Config`
pub fn new(args: &mut std::env::Args) -> Result <Vec<Config>, &'static str> {
args.next();
let mut vec = vec![];
if args.len() % 2 != 0 {
return Err("Number of args not even")
}
for _ in 0..args.len()/2 {
let number = match args.next() {
None => return Err("Incorret data type entered for number of dice"),
Some(arg) => arg,
};
let die = match args.next() {
None => return Err("Incorrect data type entered for die size"),
Some(arg) => arg,
};
let config = Config {
first : number,
second : die,
};
vec.push(config)
};
Ok(vec)
}
/// Converts a `Config` into a `Roll`. Returns a `Result` that's
/// either `Ok(Roll)` or an `Err(_)`
pub fn to_roll(&self) -> Result<Roll, ParseIntError> {
let number = self.first.parse::<usize>()?;
let die = self.second.parse::<usize>()?;
Ok(Roll::new(number, die))
}
}
#[cfg(test)]
#[test]
fn roll_check() {
let x = Config { first: "2".to_string(), second: "6".to_string() };
let x = x.to_roll().unwrap();
let y = Roll::new(2, 6);
assert_eq!(x, y);
}