From 95851f229b1e9d7b5bd2cadde156800b7dde38ed Mon Sep 17 00:00:00 2001 From: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com> Date: Sat, 7 Nov 2020 16:12:01 +0100 Subject: [PATCH 1/3] remove trash check and use unwrap that will NEVER EVER panic! --- src/main.rs | 51 ++++++--------------------------------------------- 1 file changed, 6 insertions(+), 45 deletions(-) diff --git a/src/main.rs b/src/main.rs index 492e1ca..84d0107 100644 --- a/src/main.rs +++ b/src/main.rs @@ -81,10 +81,7 @@ fn parse_config(conf: String, pool: &mut Vec) -> Result = Vec::with_capacity(2); for n in raw_world.split_whitespace() { - let v: i32 = match n.parse::() { - Ok(x) => x, - Err(_) => return Err(String::from("World config is broken.")), - }; + let v: i32 = n.parse::().unwrap(); w.push(v); } let world = world::World { x: w[0], y: w[1] }; @@ -109,47 +106,12 @@ fn parse_config(conf: String, pool: &mut Vec) -> Result raw, - None => { - return Err(String::from( - "Could not read the first token of the setup line !", - )) - } - }; - let pos_y = match setup.next() { - Some(raw) => raw, - None => { - return Err(String::from( - "Could not read the second token of the setup line !", - )) - } - }; - let orientation = match setup.next() { - Some(raw) => raw, - None => { - return Err(String::from( - "Could not read the third token of the setup line !", - )) - } - }; + let pos_x = setup.next().unwrap(); + let pos_y = setup.next().unwrap(); + let orientation = setup.next().unwrap(); // Convert values of the setup line - let r_x = match pos_x.parse::() { - Ok(raw) => raw, - Err(_) => { - return Err(String::from( - "Could not convert the first token of the setup ligne to i32 !", - )) - } - }; - let r_y = match pos_y.parse::() { - Ok(raw) => raw, - Err(_) => { - return Err(String::from( - "Could not convert the second token of the setup ligne to i32 !", - )) - } - }; + let r_x = pos_x.parse::().unwrap(); + let r_y = pos_y.parse::().unwrap(); let r_o = match orientation { "N" => robot::Orientation::N, "E" => robot::Orientation::E, @@ -168,7 +130,6 @@ fn parse_config(conf: String, pool: &mut Vec) -> Result pool.push(r), From 6b308fc71eb08ba8b98d3bbf16bfed89032d2c0d Mon Sep 17 00:00:00 2001 From: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com> Date: Sat, 7 Nov 2020 16:32:44 +0100 Subject: [PATCH 2/3] clean up error messages --- src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index cd013d4..307984e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -78,7 +78,7 @@ fn parse_config(conf: String, pool: &mut Vec) -> Result = conf.split('\n').collect(); let raw_world = match ConfParser::parse(Rule::world, lines.remove(0)) { Ok(s) => s.as_str(), - Err(e) => return Err(format!("{}", e)), + Err(e) => return Err(String::from("World config is broken.")), }; let mut w: Vec = Vec::with_capacity(2); for n in raw_world.split_whitespace() { @@ -95,7 +95,7 @@ fn parse_config(conf: String, pool: &mut Vec) -> Result s.as_str(), - Err(e) => return Err(format!("{}", e)), + Err(e) => return Err(String::from("Robot setup is broken.")), }; let rand_instructions = gen_random_instructions(); From 16acb570a6c897dc79effd7573603a338e284f7f Mon Sep 17 00:00:00 2001 From: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com> Date: Sat, 7 Nov 2020 16:42:19 +0100 Subject: [PATCH 3/3] cremove unused variables --- src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 307984e..4d64130 100644 --- a/src/main.rs +++ b/src/main.rs @@ -78,7 +78,7 @@ fn parse_config(conf: String, pool: &mut Vec) -> Result = conf.split('\n').collect(); let raw_world = match ConfParser::parse(Rule::world, lines.remove(0)) { Ok(s) => s.as_str(), - Err(e) => return Err(String::from("World config is broken.")), + Err(_) => return Err(String::from("World config is broken.")), }; let mut w: Vec = Vec::with_capacity(2); for n in raw_world.split_whitespace() { @@ -95,7 +95,7 @@ fn parse_config(conf: String, pool: &mut Vec) -> Result s.as_str(), - Err(e) => return Err(String::from("Robot setup is broken.")), + Err(_) => return Err(String::from("Robot setup is broken.")), }; let rand_instructions = gen_random_instructions();