diff --git a/src/main.rs b/src/main.rs index b327483..b511384 100644 --- a/src/main.rs +++ b/src/main.rs @@ -46,74 +46,89 @@ fn check_collisions( } /// Parse the config file, generate the world and robot pool. -fn parse_config(conf: String, pool: &mut Vec) -> Result { +fn parse_config(conf: String, pool: &mut Vec) -> Result { let mut lines = conf.lines(); // 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 !"), + None => return Err("Could not read the first line of the config file !".to_string()), }; 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 !"), + None => return Err("Could not read the first token of the first line !".to_string()), }; let token2 = match tokens.next() { Some(raw) => raw, - None => return Err("Could not read the second token of the first line !"), + None => return Err("Could not read the second token of the first line !".to_string()), }; let x: i32 = match token1.parse::() { Ok(x) => x, - Err(_) => return Err("Could not convert token one from the first string to i32"), + Err(_) => { + return Err("Could not convert token one from the first string to i32".to_string()) + } }; let y: i32 = match token2.parse::() { Ok(x) => x, - Err(_) => return Err("Could not convert token two from the first string to i32"), + Err(_) => { + return Err("Could not convert token two from the first string to i32".to_string()) + } }; + let w = world::World { x, y }; + let mut r_id: u32 = 0; loop { + r_id += 1; // This line should be empty. let empty_line = match lines.next() { None => break, Some(x) => x, }; if !empty_line.is_empty() { - return Err("This line should be empty !"); + return Err("This line should be empty !".to_string()); } let raw_setup = match lines.next() { - None => return Err("This line should be the config !"), + None => return Err("This line should be the config !".to_string()), Some(raw) => raw, }; if raw_setup.is_empty() { - return Err("This line should not be empty !"); + return Err("This line should not be empty !".to_string()); } let raw_inst = match lines.next() { - None => return Err("This line should be the instruction !"), + None => return Err("This line should be the instruction !".to_string()), Some(raw) => raw, }; if raw_inst.is_empty() { - return Err("This line should not be empty !"); + return Err("This line should not be empty !".to_string()); } // 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 !"), + None => return Err("Could not read the first token of the setup line !".to_string()), Some(raw) => raw, }; let pos_y = match setup.next() { - None => return Err("Could not read the second token of the setup line !"), + None => return Err("Could not read the second token of the setup line !".to_string()), Some(raw) => raw, }; let orientation = match setup.next() { - None => return Err("Could not read the third token of the setup line !"), + None => return Err("Could not read the third token of the setup line !".to_string()), 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 !"), + Err(_) => { + return Err( + "Could not convert the first token of the setup ligne to i32 !".to_string(), + ) + } 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 !"), + Err(_) => { + return Err( + "Could not convert the second token of the setup ligne to i32 !".to_string(), + ) + } Ok(raw) => raw, }; let r_o = match orientation { @@ -121,12 +136,24 @@ fn parse_config(conf: String, pool: &mut Vec) -> Result robot::Orientation::E, "S" => robot::Orientation::S, "W" => robot::Orientation::W, - _ => return Err("The third token of the setup line do not match any orientations !"), + _ => { + return Err( + "The third token of the setup line do not match any orientations !".to_string(), + ) + } }; - println!("{}", raw_setup); - println!("{}", raw_inst); + + // Convert instructions line. + let inst: Vec = raw_inst.chars().collect(); + 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, &w) { + Ok(()) => pool.push(r), + Err(err) => return Err(err), + } } - Ok(world::World { x, y }) + Ok(w) } /// Retrieve the content of a file and return it as a string. @@ -154,6 +181,16 @@ fn main() -> Result<(), Box> { let mut robot_pool: Vec = Vec::new(); let world: world::World = parse_config(raw_conf, &mut robot_pool)?; + println!("World -> x: {}, y: {}", world.x, world.y); + println!("Number of robot -> {}", robot_pool.len()); + println!( + "robot_pool[0] -> id: {}, x: {}, y: {}", + robot_pool[0].id, robot_pool[0].p.x, robot_pool[0].p.y + ); + println!( + "robot_pool[1] -> id: {}, x: {}, y: {}", + robot_pool[1].id, robot_pool[1].p.x, robot_pool[1].p.y + ); Ok(()) }