Merge branch 'unwrap-lol' into 'master'

remove trash check and use unwrap that will NEVER EVER panic!

See merge request mhart/DancingDroids!56
This commit is contained in:
Martin HART 2020-11-07 16:48:56 +01:00
commit 8bc35a0c04
1 changed files with 8 additions and 47 deletions

View File

@ -78,14 +78,11 @@ fn parse_config(conf: String, pool: &mut Vec<robot::Robot>) -> Result<world::Wor
let mut lines: Vec<&str> = conf.split('\n').collect(); let mut lines: Vec<&str> = conf.split('\n').collect();
let raw_world = match ConfParser::parse(Rule::world, lines.remove(0)) { let raw_world = match ConfParser::parse(Rule::world, lines.remove(0)) {
Ok(s) => s.as_str(), Ok(s) => s.as_str(),
Err(e) => return Err(format!("{}", e)), Err(_) => return Err(String::from("World config is broken.")),
}; };
let mut w: Vec<i32> = Vec::with_capacity(2); let mut w: Vec<i32> = Vec::with_capacity(2);
for n in raw_world.split_whitespace() { for n in raw_world.split_whitespace() {
let v: i32 = match n.parse::<i32>() { let v: i32 = n.parse::<i32>().unwrap();
Ok(x) => x,
Err(_) => return Err(String::from("World config is broken.")),
};
w.push(v); w.push(v);
} }
let world = world::World { x: w[0], y: w[1] }; let world = world::World { x: w[0], y: w[1] };
@ -98,7 +95,7 @@ fn parse_config(conf: String, pool: &mut Vec<robot::Robot>) -> Result<world::Wor
} }
let raw_setup = match ConfParser::parse(Rule::robot_init, lines.remove(0)) { let raw_setup = match ConfParser::parse(Rule::robot_init, lines.remove(0)) {
Ok(s) => s.as_str(), Ok(s) => s.as_str(),
Err(e) => return Err(format!("{}", e)), Err(_) => return Err(String::from("Robot setup is broken.")),
}; };
let rand_instructions = gen_random_instructions(); let rand_instructions = gen_random_instructions();
@ -110,47 +107,12 @@ fn parse_config(conf: String, pool: &mut Vec<robot::Robot>) -> Result<world::Wor
}; };
let mut setup = raw_setup.split_whitespace(); let mut setup = raw_setup.split_whitespace();
let pos_x = match setup.next() { let pos_x = setup.next().unwrap();
Some(raw) => raw, let pos_y = setup.next().unwrap();
None => { let orientation = setup.next().unwrap();
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 !",
))
}
};
// Convert values of the setup line // Convert values of the setup line
let r_x = match pos_x.parse::<i32>() { let r_x = pos_x.parse::<i32>().unwrap();
Ok(raw) => raw, let r_y = pos_y.parse::<i32>().unwrap();
Err(_) => {
return Err(String::from(
"Could not convert the first token of the setup ligne to i32 !",
))
}
};
let r_y = match pos_y.parse::<i32>() {
Ok(raw) => raw,
Err(_) => {
return Err(String::from(
"Could not convert the second token of the setup ligne to i32 !",
))
}
};
let r_o = match orientation { let r_o = match orientation {
"N" => robot::Orientation::N, "N" => robot::Orientation::N,
"E" => robot::Orientation::E, "E" => robot::Orientation::E,
@ -169,7 +131,6 @@ fn parse_config(conf: String, pool: &mut Vec<robot::Robot>) -> Result<world::Wor
} }
let r = robot::Robot::new(r_id, r_o, robot::Position { x: r_x, y: r_y }, inst); let r = robot::Robot::new(r_id, r_o, robot::Position { x: r_x, y: r_y }, inst);
// Load robot inside the pool. // Load robot inside the pool.
match check_map(&r, &world) { match check_map(&r, &world) {
Ok(()) => pool.push(r), Ok(()) => pool.push(r),