From 5d4d3a29c7336cc9ea88ce36e2bee5ddfe811299 Mon Sep 17 00:00:00 2001 From: Martin HART Date: Fri, 30 Oct 2020 15:29:52 +0100 Subject: [PATCH] Change string conversion methods --- src/main.rs | 76 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 54 insertions(+), 22 deletions(-) diff --git a/src/main.rs b/src/main.rs index b511384..317e5c2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -51,27 +51,43 @@ fn parse_config(conf: String, pool: &mut Vec) -> Result 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::() { 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::() { 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) -> Result 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::() { 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::() { 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) -> Result 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 = 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.