From 555a0ff6b8fdad5f3bf798fb573ca4ca13b5d4fd Mon Sep 17 00:00:00 2001 From: Martin HART Date: Wed, 28 Oct 2020 12:20:20 +0100 Subject: [PATCH 1/9] x and y should be public --- src/world.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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, } From 430e7068317f8ea5d1c078e4694b4afa266bbdd1 Mon Sep 17 00:00:00 2001 From: Martin HART Date: Wed, 28 Oct 2020 12:36:56 +0100 Subject: [PATCH 2/9] Prototype of the parsing_config() func --- src/main.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main.rs b/src/main.rs index 71a1e16..344b600 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,16 @@ use std::fs; use std::io; mod robot; +mod world; + +/// Parse the config file, generate the world and robot pool. +fn parse_config(raw_conf: String, pool: &Vec) -> Result { + // test exemple, i am currently writting the real code. + // This function return the map OR an error inside a Result so we + // can catch error using '?' in main + // we also create robots here and put them into the pool. + Ok(world::World { x: 5, y: 5 }) +} /// Retrieve the content of a file and return it as a string. fn open_file(filename: &str) -> io::Result { @@ -42,6 +52,7 @@ 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 world = parse_config(raw_conf, &robot_pool)?; Ok(()) } From bbcbfd4d419e454638d5699a2b89873e9ef74d35 Mon Sep 17 00:00:00 2001 From: Martin HART Date: Wed, 28 Oct 2020 13:09:07 +0100 Subject: [PATCH 3/9] Catch the first ligne of the config file or throw a error ! --- src/main.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 344b600..851406c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,10 +22,12 @@ mod world; /// Parse the config file, generate the world and robot pool. fn parse_config(raw_conf: String, pool: &Vec) -> Result { - // test exemple, i am currently writting the real code. - // This function return the map OR an error inside a Result so we - // can catch error using '?' in main - // we also create robots here and put them into the pool. + let mut lines = raw_conf.lines(); + // The first line of the config file should be the World. + let raw_map: &str = match lines.next() { + None => return Err("Could not read the first line of the config file !"), + Some(raw) => raw, + }; Ok(world::World { x: 5, y: 5 }) } From 0a63ab5b537ea281556a2e0593626ac7cd535c67 Mon Sep 17 00:00:00 2001 From: Martin HART Date: Wed, 28 Oct 2020 14:06:03 +0100 Subject: [PATCH 4/9] exemple token --- src/main.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 851406c..81876d4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,10 +24,14 @@ mod world; fn parse_config(raw_conf: String, pool: &Vec) -> Result { let mut lines = raw_conf.lines(); // The first line of the config file should be the World. - let raw_map: &str = match lines.next() { + let raw_line: &str = match lines.next() { None => return Err("Could not read the first line of the config file !"), Some(raw) => raw, }; + let raw_token = raw_line.split_whitespace(); + let token1 = raw_token.next(); + let token2 = raw_token.next(); + // Need to check if the robots fit in the World. Ok(world::World { x: 5, y: 5 }) } @@ -54,7 +58,7 @@ 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 world = parse_config(raw_conf, &robot_pool)?; + let world: world::World = parse_config(raw_conf, &robot_pool)?; Ok(()) } From 9d0b96b4d71e1ee4112c9310b7198df8bc3380ae Mon Sep 17 00:00:00 2001 From: Martin HART Date: Wed, 28 Oct 2020 16:17:34 +0100 Subject: [PATCH 5/9] add mut to the pool O_o --- src/main.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 81876d4..e994322 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,17 +21,18 @@ mod robot; mod world; /// Parse the config file, generate the world and robot pool. -fn parse_config(raw_conf: String, pool: &Vec) -> Result { - let mut lines = raw_conf.lines(); +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() { None => return Err("Could not read the first line of the config file !"), Some(raw) => raw, }; + /* let raw_token = raw_line.split_whitespace(); let token1 = raw_token.next(); let token2 = raw_token.next(); - // Need to check if the robots fit in the World. + */ Ok(world::World { x: 5, y: 5 }) } @@ -57,8 +58,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 world: world::World = parse_config(raw_conf, &robot_pool)?; + let mut robot_pool: Vec = Vec::new(); + let world: world::World = parse_config(raw_conf, &mut robot_pool)?; Ok(()) } From e4001c99c758db02c748a26d1000d1ca16e146e9 Mon Sep 17 00:00:00 2001 From: Martin HART Date: Wed, 28 Oct 2020 16:42:36 +0100 Subject: [PATCH 6/9] add token --- src/main.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index e994322..d1ae7d2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,11 +28,10 @@ fn parse_config(conf: String, pool: &mut Vec) -> Result return Err("Could not read the first line of the config file !"), Some(raw) => raw, }; - /* - let raw_token = raw_line.split_whitespace(); - let token1 = raw_token.next(); - let token2 = raw_token.next(); - */ + let mut tokens = raw_line.split_whitespace(); + let token1 = tokens.next(); + let token2 = tokens.next(); + println!("{}, {}", token1.unwrap(), token2.unwrap()); Ok(world::World { x: 5, y: 5 }) } From 194d6f5df211c86f0ba9b9909fc1b8bdf7502ecf Mon Sep 17 00:00:00 2001 From: Martin HART Date: Wed, 28 Oct 2020 16:50:49 +0100 Subject: [PATCH 7/9] Remove .unwrap() shit --- src/main.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index d1ae7d2..cb3ab9d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,9 +29,15 @@ fn parse_config(conf: String, pool: &mut Vec) -> Result raw, }; let mut tokens = raw_line.split_whitespace(); - let token1 = tokens.next(); - let token2 = tokens.next(); - println!("{}, {}", token1.unwrap(), token2.unwrap()); + let token1 = match tokens.next() { + None => return Err("Could not read the first token of the first line !"), + Some(raw) => raw, + }; + let token2 = match tokens.next() { + None => return Err("Could not read the second token of the first line !"), + Some(raw) => raw, + }; + println!("{}, {}", token1, token2); Ok(world::World { x: 5, y: 5 }) } From 44545cdc07c8bf47c3ed3be7ea321631ae1f8fc3 Mon Sep 17 00:00:00 2001 From: Martin HART Date: Wed, 28 Oct 2020 18:05:22 +0100 Subject: [PATCH 8/9] This is really bad.... --- src/main.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index cb3ab9d..4668dc7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,20 +25,27 @@ fn parse_config(conf: String, pool: &mut Vec) -> Result return Err("Could not read the first line of the config file !"), 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() { - None => return Err("Could not read the first token of the first line !"), Some(raw) => raw, + None => return Err("Could not read the first token of the first line !"), }; let token2 = match tokens.next() { - None => return Err("Could not read the second token of the first line !"), Some(raw) => raw, + None => return Err("Could not read the second token of the first line !"), }; - println!("{}, {}", token1, token2); - Ok(world::World { x: 5, y: 5 }) + 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: x, y: y }) } /// Retrieve the content of a file and return it as a string. From 2aa2af59ec50291168f75d9faf05d3007aeca9c4 Mon Sep 17 00:00:00 2001 From: Martin HART Date: Wed, 28 Oct 2020 18:29:16 +0100 Subject: [PATCH 9/9] simple typo --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 4668dc7..850f528 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,7 +45,7 @@ fn parse_config(conf: String, pool: &mut Vec) -> Result x, Err(err) => return Err("Could not convert token two from the first string to i32"), }; - Ok(world::World { x: x, y: y }) + Ok(world::World { x, y }) } /// Retrieve the content of a file and return it as a string.