Change string conversion methods
This commit is contained in:
parent
46d4f27e24
commit
5d4d3a29c7
76
src/main.rs
76
src/main.rs
@ -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.
|
||||
let raw_line: &str = match lines.next() {
|
||||
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 token1 = match tokens.next() {
|
||||
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() {
|
||||
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>() {
|
||||
Ok(x) => x,
|
||||
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>() {
|
||||
Ok(x) => x,
|
||||
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 };
|
||||
@ -84,50 +100,62 @@ fn parse_config(conf: String, pool: &mut Vec<robot::Robot>) -> Result<world::Wor
|
||||
Some(x) => x,
|
||||
};
|
||||
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() {
|
||||
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,
|
||||
};
|
||||
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() {
|
||||
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,
|
||||
};
|
||||
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.
|
||||
let mut setup = raw_setup.split_whitespace();
|
||||
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,
|
||||
};
|
||||
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,
|
||||
};
|
||||
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,
|
||||
};
|
||||
// Convert values of the setup line
|
||||
let r_x = match pos_x.parse::<i32>() {
|
||||
Err(_) => {
|
||||
return Err(
|
||||
"Could not convert the first token of the setup ligne to i32 !".to_string(),
|
||||
)
|
||||
return Err(String::from(
|
||||
"Could not convert the first token of the setup ligne to i32 !",
|
||||
))
|
||||
}
|
||||
Ok(raw) => raw,
|
||||
};
|
||||
let r_y = match pos_y.parse::<i32>() {
|
||||
Err(_) => {
|
||||
return Err(
|
||||
"Could not convert the second token of the setup ligne to i32 !".to_string(),
|
||||
)
|
||||
return Err(String::from(
|
||||
"Could not convert the second token of the setup ligne to i32 !",
|
||||
))
|
||||
}
|
||||
Ok(raw) => raw,
|
||||
};
|
||||
@ -137,14 +165,18 @@ fn parse_config(conf: String, pool: &mut Vec<robot::Robot>) -> Result<world::Wor
|
||||
"S" => robot::Orientation::S,
|
||||
"W" => robot::Orientation::W,
|
||||
_ => {
|
||||
return Err(
|
||||
"The third token of the setup line do not match any orientations !".to_string(),
|
||||
)
|
||||
return Err(String::from(
|
||||
"The third token of the setup line do not match any orientations !",
|
||||
))
|
||||
}
|
||||
};
|
||||
|
||||
// Convert instructions line.
|
||||
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);
|
||||
|
||||
// Load robot inside the pool.
|
||||
|
Loading…
Reference in New Issue
Block a user