diff --git a/src/main.rs b/src/main.rs index 71a1e16..850f528 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,35 @@ use std::fs; use std::io; mod robot; +mod world; + +/// Parse the config file, generate the world and robot pool. +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 !"), + }; + 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 !"), + }; + let token2 = match tokens.next() { + Some(raw) => raw, + None => return Err("Could not read the second token of the first line !"), + }; + let x: i32 = match token1.parse::() { + Ok(x) => x, + Err(err) => return Err("Could not convert token one from the first string to i32"), + }; + let y: i32 = match token2.parse::() { + Ok(x) => x, + Err(err) => return Err("Could not convert token two from the first string to i32"), + }; + Ok(world::World { x, y }) +} /// Retrieve the content of a file and return it as a string. fn open_file(filename: &str) -> io::Result { @@ -41,7 +70,8 @@ fn main() -> Result<(), Box> { let raw_conf = open_file(matches.value_of("file").unwrap_or("two_robots.txt"))?; - let robot_pool: Vec = Vec::new(); + let mut robot_pool: Vec = Vec::new(); + let world: world::World = parse_config(raw_conf, &mut robot_pool)?; Ok(()) } diff --git a/src/world.rs b/src/world.rs index ab9ac8e..357b82d 100644 --- a/src/world.rs +++ b/src/world.rs @@ -1,5 +1,5 @@ /// The World is represented here. pub struct World { - x: i32, - y: i32, + pub x: i32, + pub y: i32, }