This commit is contained in:
MeiK 2020-03-27 19:01:25 +08:00
parent 66dcafd389
commit 74bbf6432d
3 changed files with 38 additions and 7 deletions

View File

@ -1,10 +1,12 @@
use std::result; use std::result;
use std::fs;
use yaml_rust::{ScanError, Yaml, YamlLoader}; use yaml_rust::{ScanError, Yaml, YamlLoader};
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
YamlScanError(ScanError), YamlScanError(ScanError),
YamlParseError(String), YamlParseError(String),
ReadFileError,
} }
pub type Result<T> = result::Result<T, Error>; pub type Result<T> = result::Result<T, Error>;
@ -86,6 +88,14 @@ impl Config {
Ok(Config { languages }) Ok(Config { languages })
} }
pub fn load_from_file(filename: &str) -> Result<Config> {
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<Config> { pub fn default() -> Result<Config> {
let config_yaml = " let config_yaml = "
languages: languages:

View File

@ -1,10 +1,11 @@
extern crate libc; extern crate libc;
use std::collections::BTreeMap;
use handlebars::Handlebars; use handlebars::Handlebars;
use std::collections::BTreeMap;
use std::env;
mod runner;
mod config; mod config;
mod runner;
fn main() { fn main() {
let mut handlebars = Handlebars::new(); let mut handlebars = Handlebars::new();
@ -15,12 +16,23 @@ fn main() {
data.insert("world".to_string(), "世界!".to_string()); data.insert("world".to_string(), "世界!".to_string());
println!("{}", handlebars.render("t1", &data).unwrap()); println!("{}", handlebars.render("t1", &data).unwrap());
let _config = match config::Config::default() { let args: Vec<String> = env::args().collect();
let _config = if args.len() > 1 {
match config::Config::load_from_file(&args[1]) {
Ok(value) => value, Ok(value) => value,
Err(err) => { Err(err) => {
println!("{:?}", err); println!("{:?}", err);
return return
} }
}
} else {
match config::Config::default() {
Ok(value) => value,
Err(err) => {
println!("{:?}", err);
return
}
}
}; };
process(); process();

9
tests/config.yml Normal file
View File

@ -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}}