From 78dd6d618dfe0a5b79ab75afbc450c1d0ffcc816 Mon Sep 17 00:00:00 2001 From: Andrew Jacob Gremlich Date: Wed, 5 Jun 2019 14:23:57 -0600 Subject: [PATCH] Removed unwrap functions for proper error handler. --- Cargo.lock | 2 ++ src/game/command.rs | 24 +++++++++++++++++++++--- src/game/mod.rs | 11 ++++++----- src/main.rs | 16 +++++++++++++--- 4 files changed, 42 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f9b6daa..66e81d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,3 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. [[package]] name = "autocfg" version = "0.1.2" diff --git a/src/game/command.rs b/src/game/command.rs index 3ed6fda..425d3cc 100644 --- a/src/game/command.rs +++ b/src/game/command.rs @@ -9,14 +9,32 @@ impl Command { pub fn new(user_input: &String) -> Command { let commands_parsed: Vec<&str> = user_input.split_whitespace().collect(); - let letter_command: char = commands_parsed[0].trim().parse::().unwrap(); + let letter_command: char = match commands_parsed[0].trim().parse::() { + Ok(n) => n, + Err(error) => { + println!("error: {}", error); + 'e' + } + }; let mut x_command: u8 = 0; let mut y_command: u8 = 0; if commands_parsed.len() > 1 { - x_command = commands_parsed[1].trim().parse::().unwrap(); - y_command = commands_parsed[2].trim().parse::().unwrap(); + x_command = match commands_parsed[1].trim().parse::() { + Ok(n) => n, + Err(error) => { + println!("error: {}", error); + 0 + } + }; + y_command = match commands_parsed[2].trim().parse::() { + Ok(n) => n, + Err(error) => { + println!("error: {}", error); + 0 + } + }; } Command { diff --git a/src/game/mod.rs b/src/game/mod.rs index 93ac797..e03d62d 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -59,11 +59,12 @@ pub fn looper(diff: &str) { let mut user_input = String::new(); loop { - /* - Locks this handle and reads a line of user_input into the specified buffer. - See the above note about the `unwrap` function. - */ - stdin().read_line(&mut user_input).unwrap(); + match stdin().read_line(&mut user_input) { + Ok(_n) => { + println!("Command Given {}", user_input); + } + Err(error) => println!("error: {}", error), + } let Command { move_command, diff --git a/src/main.rs b/src/main.rs index cf8dcb3..76de032 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,17 +12,27 @@ mod game; fn main() { /* `Vec` is an array of strings whose ownership is in this function scope. - `env::args().collect()` is the way to collect commandline arguments. + `env::args().collect()` is the way to collect command line arguments. "A heap-allocated vector that is resizable at runtime."" */ + + let input_difficulty = ["easy", "medium", "hard"]; let args: Vec = env::args().collect(); if args.len() < 2 { println!("Not enough arguments! Must be `rust_edge [easy, medium, hard]`"); process::exit(1); + } else if args.len() > 2 { + println!("Too many arguments! Must be `rust_edge [easy, medium, hard]`"); + process::exit(1); } - let difficulty = &args[1]; + let difficulty: &str = &args[1]; - let _game_loop = game::looper(difficulty); + if input_difficulty.contains(&difficulty) { + let _game_loop = game::looper(difficulty); + } else { + println!("Invalid difficulty. You inputted {:?}", difficulty); + println!("Too many arguments! Must be `rust_edge [easy, medium, hard]`"); + } }