Change string conversion methods

This commit is contained in:
Martin HART 2020-10-30 15:29:52 +01:00
parent 46d4f27e24
commit 5d4d3a29c7
1 changed files with 54 additions and 22 deletions

View File

@ -51,27 +51,43 @@ fn parse_config(conf: String, pool: &mut Vec<robot::Robot>) -> Result<world::Wor
// The first line of the config file should be the World. // The first line of the config file should be the World.
let raw_line: &str = match lines.next() { let raw_line: &str = match lines.next() {
Some(raw) => raw, Some(raw) => raw,
None => return Err("Could not read the first line of the config file !".to_string()), None => {
return Err(String::from(
"Could not read the first line of the config file !",
))
}
}; };
let mut tokens = raw_line.split_whitespace(); let mut tokens = raw_line.split_whitespace();
let token1 = match tokens.next() { let token1 = match tokens.next() {
Some(raw) => raw, Some(raw) => raw,
None => return Err("Could not read the first token of the first line !".to_string()), None => {
return Err(String::from(
"Could not read the first token of the first line !",
))
}
}; };
let token2 = match tokens.next() { let token2 = match tokens.next() {
Some(raw) => raw, Some(raw) => raw,
None => return Err("Could not read the second token of the first line !".to_string()), None => {
return Err(String::from(
"Could not read the second token of the first line !",
))
}
}; };
let x: i32 = match token1.parse::<i32>() { let x: i32 = match token1.parse::<i32>() {
Ok(x) => x, Ok(x) => x,
Err(_) => { Err(_) => {
return Err("Could not convert token one from the first string to i32".to_string()) return Err(String::from(
"Could not convert token one from the first string to i32",
))
} }
}; };
let y: i32 = match token2.parse::<i32>() { let y: i32 = match token2.parse::<i32>() {
Ok(x) => x, Ok(x) => x,
Err(_) => { Err(_) => {
return Err("Could not convert token two from the first string to i32".to_string()) return Err(String::from(
"Could not convert token two from the first string to i32",
))
} }
}; };
let w = world::World { x, y }; let w = world::World { x, y };
@ -84,50 +100,62 @@ fn parse_config(conf: String, pool: &mut Vec<robot::Robot>) -> Result<world::Wor
Some(x) => x, Some(x) => x,
}; };
if !empty_line.is_empty() { if !empty_line.is_empty() {
return Err("This line should be empty !".to_string()); return Err(String::from("This line should be empty !"));
} }
let raw_setup = match lines.next() { let raw_setup = match lines.next() {
None => return Err("This line should be the config !".to_string()), None => return Err(String::from("This line should be the config !")),
Some(raw) => raw, Some(raw) => raw,
}; };
if raw_setup.is_empty() { if raw_setup.is_empty() {
return Err("This line should not be empty !".to_string()); return Err(String::from("This line should not be empty !"));
} }
let raw_inst = match lines.next() { let raw_inst = match lines.next() {
None => return Err("This line should be the instruction !".to_string()), None => return Err(String::from("This line should be the instruction !")),
Some(raw) => raw, Some(raw) => raw,
}; };
if raw_inst.is_empty() { if raw_inst.is_empty() {
return Err("This line should not be empty !".to_string()); return Err(String::from("This line should not be empty !"));
} }
// Parse the setup line of the robot. // Parse the setup line of the robot.
let mut setup = raw_setup.split_whitespace(); let mut setup = raw_setup.split_whitespace();
let pos_x = match setup.next() { let pos_x = match setup.next() {
None => return Err("Could not read the first token of the setup line !".to_string()), None => {
return Err(String::from(
"Could not read the first token of the setup line !",
))
}
Some(raw) => raw, Some(raw) => raw,
}; };
let pos_y = match setup.next() { let pos_y = match setup.next() {
None => return Err("Could not read the second token of the setup line !".to_string()), None => {
return Err(String::from(
"Could not read the second token of the setup line !",
))
}
Some(raw) => raw, Some(raw) => raw,
}; };
let orientation = match setup.next() { let orientation = match setup.next() {
None => return Err("Could not read the third token of the setup line !".to_string()), None => {
return Err(String::from(
"Could not read the third token of the setup line !",
))
}
Some(raw) => raw, Some(raw) => raw,
}; };
// Convert values of the setup line // Convert values of the setup line
let r_x = match pos_x.parse::<i32>() { let r_x = match pos_x.parse::<i32>() {
Err(_) => { Err(_) => {
return Err( return Err(String::from(
"Could not convert the first token of the setup ligne to i32 !".to_string(), "Could not convert the first token of the setup ligne to i32 !",
) ))
} }
Ok(raw) => raw, Ok(raw) => raw,
}; };
let r_y = match pos_y.parse::<i32>() { let r_y = match pos_y.parse::<i32>() {
Err(_) => { Err(_) => {
return Err( return Err(String::from(
"Could not convert the second token of the setup ligne to i32 !".to_string(), "Could not convert the second token of the setup ligne to i32 !",
) ))
} }
Ok(raw) => raw, Ok(raw) => raw,
}; };
@ -137,14 +165,18 @@ fn parse_config(conf: String, pool: &mut Vec<robot::Robot>) -> Result<world::Wor
"S" => robot::Orientation::S, "S" => robot::Orientation::S,
"W" => robot::Orientation::W, "W" => robot::Orientation::W,
_ => { _ => {
return Err( return Err(String::from(
"The third token of the setup line do not match any orientations !".to_string(), "The third token of the setup line do not match any orientations !",
) ))
} }
}; };
// Convert instructions line. // Convert instructions line.
let inst: Vec<char> = raw_inst.chars().collect(); let inst: Vec<char> = raw_inst.chars().collect();
if !world::is_instructions(&inst) {
return Err(String::from("Invalid instructions !"));
}
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.