From 74bbf6432d065d910aea8860fc4bbe62c0dba988 Mon Sep 17 00:00:00 2001 From: MeiK Date: Fri, 27 Mar 2020 19:01:25 +0800 Subject: [PATCH] Update --- src/config.rs | 10 ++++++++++ src/main.rs | 26 +++++++++++++++++++------- tests/config.yml | 9 +++++++++ 3 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 tests/config.yml diff --git a/src/config.rs b/src/config.rs index d68a86b..8ca380c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,10 +1,12 @@ use std::result; +use std::fs; use yaml_rust::{ScanError, Yaml, YamlLoader}; #[derive(Debug)] pub enum Error { YamlScanError(ScanError), YamlParseError(String), + ReadFileError, } pub type Result = result::Result; @@ -86,6 +88,14 @@ impl Config { Ok(Config { languages }) } + pub fn load_from_file(filename: &str) -> Result { + let contents = match fs::read_to_string(filename) { + Ok(value) => value, + Err(_) => return Err(Error::ReadFileError), + }; + let config = Config::load_yaml(&contents)?; + Ok(config) + } pub fn default() -> Result { let config_yaml = " languages: diff --git a/src/main.rs b/src/main.rs index ba31e40..9e64f0a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,11 @@ extern crate libc; -use std::collections::BTreeMap; use handlebars::Handlebars; +use std::collections::BTreeMap; +use std::env; -mod runner; mod config; +mod runner; fn main() { let mut handlebars = Handlebars::new(); @@ -15,11 +16,22 @@ fn main() { data.insert("world".to_string(), "世界!".to_string()); println!("{}", handlebars.render("t1", &data).unwrap()); - let _config = match config::Config::default() { - Ok(value) => value, - Err(err) => { - println!("{:?}", err); - return + let args: Vec = env::args().collect(); + let _config = if args.len() > 1 { + match config::Config::load_from_file(&args[1]) { + Ok(value) => value, + Err(err) => { + println!("{:?}", err); + return + } + } + } else { + match config::Config::default() { + Ok(value) => value, + Err(err) => { + println!("{:?}", err); + return + } } }; diff --git a/tests/config.yml b/tests/config.yml new file mode 100644 index 0000000..5a600a0 --- /dev/null +++ b/tests/config.yml @@ -0,0 +1,9 @@ +languages: + - language: c + version: 7.5.0 + compile_command: /usr/bin/gcc {{filename}} -o a.out + run_command: ./a.out + - language: python + version: 3.6.9 + compile_command: /usr/bin/python3 -m compileall {{filename}} + run_command: /use/bin/python3 {{filename}}