Skip to content

Commit

Permalink
Removed unwrap functions for proper error handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Jacob Gremlich committed Jun 5, 2019
1 parent 1298950 commit 78dd6d6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 21 additions & 3 deletions src/game/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<char>().unwrap();
let letter_command: char = match commands_parsed[0].trim().parse::<char>() {
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::<u8>().unwrap();
y_command = commands_parsed[2].trim().parse::<u8>().unwrap();
x_command = match commands_parsed[1].trim().parse::<u8>() {
Ok(n) => n,
Err(error) => {
println!("error: {}", error);
0
}
};
y_command = match commands_parsed[2].trim().parse::<u8>() {
Ok(n) => n,
Err(error) => {
println!("error: {}", error);
0
}
};
}

Command {
Expand Down
11 changes: 6 additions & 5 deletions src/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 13 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,27 @@ mod game;
fn main() {
/*
`Vec<String>` 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<String> = 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]`");
}
}

0 comments on commit 78dd6d6

Please sign in to comment.