2020-10-26 13:55:05 +01:00
|
|
|
// Dancing Droids
|
2020-10-26 15:29:14 +01:00
|
|
|
// Copyright (C) 2020 Martin HART, Volodymyr PATUTA, Stephane Elias BENABDESLAM
|
2020-10-26 13:55:05 +01:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2020-10-20 12:48:28 +02:00
|
|
|
use clap::{App, Arg};
|
2020-10-13 10:31:50 +02:00
|
|
|
use std::fs;
|
|
|
|
use std::io;
|
|
|
|
|
2020-10-27 22:55:31 +01:00
|
|
|
mod robot;
|
2020-10-28 12:36:56 +01:00
|
|
|
mod world;
|
|
|
|
|
|
|
|
/// Parse the config file, generate the world and robot pool.
|
2020-10-28 16:17:34 +01:00
|
|
|
fn parse_config(conf: String, pool: &mut Vec<robot::Robot>) -> Result<world::World, &'static str> {
|
|
|
|
let mut lines = conf.lines();
|
2020-10-28 13:09:07 +01:00
|
|
|
// The first line of the config file should be the World.
|
2020-10-28 14:06:03 +01:00
|
|
|
let raw_line: &str = match lines.next() {
|
2020-10-28 13:09:07 +01:00
|
|
|
Some(raw) => raw,
|
2020-10-28 18:05:22 +01:00
|
|
|
None => return Err("Could not read the first line of the config file !"),
|
2020-10-28 13:09:07 +01:00
|
|
|
};
|
2020-10-28 16:42:36 +01:00
|
|
|
let mut tokens = raw_line.split_whitespace();
|
2020-10-28 16:50:49 +01:00
|
|
|
let token1 = match tokens.next() {
|
|
|
|
Some(raw) => raw,
|
2020-10-28 18:05:22 +01:00
|
|
|
None => return Err("Could not read the first token of the first line !"),
|
2020-10-28 16:50:49 +01:00
|
|
|
};
|
|
|
|
let token2 = match tokens.next() {
|
|
|
|
Some(raw) => raw,
|
2020-10-28 18:05:22 +01:00
|
|
|
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"),
|
2020-10-28 16:50:49 +01:00
|
|
|
};
|
2020-10-28 18:29:16 +01:00
|
|
|
Ok(world::World { x, y })
|
2020-10-28 12:36:56 +01:00
|
|
|
}
|
2020-10-27 22:55:31 +01:00
|
|
|
|
2020-10-17 18:16:40 +02:00
|
|
|
/// Retrieve the content of a file and return it as a string.
|
2020-10-13 10:19:34 +02:00
|
|
|
fn open_file(filename: &str) -> io::Result<String> {
|
|
|
|
let content = fs::read_to_string(filename)?;
|
|
|
|
Ok(content)
|
|
|
|
}
|
|
|
|
|
2020-10-19 12:50:48 +02:00
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
2020-10-20 13:42:32 +02:00
|
|
|
// We handle CLI flags here.
|
2020-10-20 12:48:28 +02:00
|
|
|
let matches = App::new("DancingDroids")
|
|
|
|
.version("0.1.0")
|
|
|
|
.about("When droids dance togethers")
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("file")
|
|
|
|
.short("f")
|
|
|
|
.long("file")
|
|
|
|
.takes_value(true)
|
|
|
|
.help("Configuration file"),
|
|
|
|
)
|
|
|
|
.get_matches();
|
|
|
|
|
2020-10-20 13:17:50 +02:00
|
|
|
let raw_conf = open_file(matches.value_of("file").unwrap_or("two_robots.txt"))?;
|
2020-10-20 14:06:10 +02:00
|
|
|
|
2020-10-28 16:17:34 +01:00
|
|
|
let mut robot_pool: Vec<robot::Robot> = Vec::new();
|
|
|
|
let world: world::World = parse_config(raw_conf, &mut robot_pool)?;
|
2020-10-28 12:00:17 +01:00
|
|
|
|
2020-10-19 12:50:48 +02:00
|
|
|
Ok(())
|
2020-10-11 15:14:20 +02:00
|
|
|
}
|
2020-10-13 12:07:47 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-10-13 12:15:04 +02:00
|
|
|
#[test]
|
|
|
|
fn test_open_file() {
|
|
|
|
assert!(open_file("two_robots.txt").is_ok());
|
|
|
|
assert!(open_file("test_unexisting_file.extension").is_err());
|
|
|
|
}
|
2020-10-13 12:07:47 +02:00
|
|
|
}
|