dancing_droids/src/main.rs

71 lines
2.2 KiB
Rust
Raw Normal View History

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.
fn parse_config(raw_conf: String, pool: &Vec<robot::Robot>) -> Result<world::World, &'static str> {
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,
};
2020-10-28 12:36:56 +01:00
Ok(world::World { x: 5, y: 5 })
}
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.
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 12:00:17 +01:00
let robot_pool: Vec<robot::Robot> = Vec::new();
2020-10-28 12:36:56 +01:00
let world = parse_config(raw_conf, &robot_pool)?;
2020-10-28 12:00:17 +01:00
2020-10-19 12:50:48 +02:00
Ok(())
}
#[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());
}
}