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:
commit
8bc35a0c04
55
src/main.rs
55
src/main.rs
@ -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 raw_world = match ConfParser::parse(Rule::world, lines.remove(0)) {
|
||||
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);
|
||||
for n in raw_world.split_whitespace() {
|
||||
let v: i32 = match n.parse::<i32>() {
|
||||
Ok(x) => x,
|
||||
Err(_) => return Err(String::from("World config is broken.")),
|
||||
};
|
||||
let v: i32 = n.parse::<i32>().unwrap();
|
||||
w.push(v);
|
||||
}
|
||||
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)) {
|
||||
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();
|
||||
@ -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 pos_x = match setup.next() {
|
||||
Some(raw) => 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::<i32>() {
|
||||
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::<i32>() {
|
||||
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::<i32>().unwrap();
|
||||
let r_y = pos_y.parse::<i32>().unwrap();
|
||||
let r_o = match orientation {
|
||||
"N" => robot::Orientation::N,
|
||||
"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);
|
||||
|
||||
// Load robot inside the pool.
|
||||
match check_map(&r, &world) {
|
||||
Ok(()) => pool.push(r),
|
||||
|
Loading…
Reference in New Issue
Block a user