Merge branch 'parsing-config-file' into 'master'
Parsing config file prototype See merge request mhart/DancingDroids!32
This commit is contained in:
commit
15b68ae764
32
src/main.rs
32
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<robot::Robot>) -> Result<world::World, &'static str> {
|
||||
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::<i32>() {
|
||||
Ok(x) => x,
|
||||
Err(err) => return Err("Could not convert token one from the first string to i32"),
|
||||
};
|
||||
let y: i32 = match token2.parse::<i32>() {
|
||||
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<String> {
|
||||
@ -41,7 +70,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
let raw_conf = open_file(matches.value_of("file").unwrap_or("two_robots.txt"))?;
|
||||
|
||||
let robot_pool: Vec<robot::Robot> = Vec::new();
|
||||
let mut robot_pool: Vec<robot::Robot> = Vec::new();
|
||||
let world: world::World = parse_config(raw_conf, &mut robot_pool)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/// The World is represented here.
|
||||
pub struct World {
|
||||
x: i32,
|
||||
y: i32,
|
||||
pub x: i32,
|
||||
pub y: i32,
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user