summaryrefslogtreecommitdiff
path: root/src/run.rs
diff options
context:
space:
mode:
authorlyn <819880950@qq.com>2019-03-06 21:47:00 +0100
committerlyn <819880950@qq.com>2019-03-06 21:47:33 +0100
commit70e59cca3caf92a1daedac4fcf5d8940215e5529 (patch)
treee2a8f58ffe5717ec7a08407ae4d1ee70945ab37e /src/run.rs
parent7d6e2812fb7b22ff673f753cfc2a7c44fa1c57b8 (diff)
standardize exercise running via an external toml file
Diffstat (limited to 'src/run.rs')
-rw-r--r--src/run.rs93
1 files changed, 55 insertions, 38 deletions
diff --git a/src/run.rs b/src/run.rs
index ea79069..b41ed33 100644
--- a/src/run.rs
+++ b/src/run.rs
@@ -2,54 +2,71 @@ use crate::util::clean;
use crate::verify::test;
use console::{style, Emoji};
use indicatif::ProgressBar;
+use std::fs;
use std::process::Command;
+use toml::Value;
-pub fn run(matches: clap::ArgMatches) {
+pub fn run(matches: clap::ArgMatches) -> Result<(), ()> {
if let Some(filename) = matches.value_of("file") {
- if matches.is_present("test") {
- match test(filename) {
- Ok(_) => (),
- Err(_) => (),
- }
- std::process::exit(0);
+ let toml: Value = fs::read_to_string("info.toml").unwrap().parse().unwrap();
+ let tomlvec: &Vec<Value> = toml.get("exercises").unwrap().as_array().unwrap();
+ let mut exercises = tomlvec.clone();
+ exercises.retain(|i| i.get("path").unwrap().as_str().unwrap() == filename);
+ if exercises.is_empty() {
+ println!("No exercise found for your filename!");
+ std::process::exit(1);
}
- let bar = ProgressBar::new_spinner();
- bar.set_message(format!("Compiling {}...", filename).as_str());
- bar.enable_steady_tick(100);
- let compilecmd = Command::new("rustc")
- .args(&[filename, "-o", "temp", "--color", "always"])
- .output()
- .expect("fail");
- bar.set_message(format!("Running {}...", filename).as_str());
- if compilecmd.status.success() {
- let runcmd = Command::new("./temp").output().expect("fail");
- bar.finish_and_clear();
- if runcmd.status.success() {
- println!("{}", String::from_utf8_lossy(&runcmd.stdout));
- let formatstr = format!("{} Successfully ran {}", Emoji("✅", "✓"), filename);
- println!("{}", style(formatstr).green());
- clean();
- } else {
- println!("{}", String::from_utf8_lossy(&runcmd.stdout));
- println!("{}", String::from_utf8_lossy(&runcmd.stderr));
+ let exercise: &Value = &exercises[0];
+ match exercise.get("mode").unwrap().as_str().unwrap() {
+ "test" => test(exercise.get("path").unwrap().as_str().unwrap())?,
+ "compile" => compile_and_run(exercise.get("path").unwrap().as_str().unwrap())?,
+ _ => (),
+ }
+ Ok(())
+ } else {
+ panic!("Please supply a filename!");
+ }
+}
- let formatstr = format!("{} Ran {} with errors", Emoji("⚠️ ", "!"), filename);
- println!("{}", style(formatstr).red());
- clean();
- }
+pub fn compile_and_run(filename: &str) -> Result<(), ()> {
+ let bar = ProgressBar::new_spinner();
+ bar.set_message(format!("Compiling {}...", filename).as_str());
+ bar.enable_steady_tick(100);
+ let compilecmd = Command::new("rustc")
+ .args(&[filename, "-o", "temp", "--color", "always"])
+ .output()
+ .expect("fail");
+ bar.set_message(format!("Running {}...", filename).as_str());
+ if compilecmd.status.success() {
+ let runcmd = Command::new("./temp").output().expect("fail");
+ bar.finish_and_clear();
+
+ if runcmd.status.success() {
+ println!("{}", String::from_utf8_lossy(&runcmd.stdout));
+ let formatstr = format!("{} Successfully ran {}", Emoji("✅", "✓"), filename);
+ println!("{}", style(formatstr).green());
+ clean();
+ Ok(())
} else {
- bar.finish_and_clear();
- let formatstr = format!(
- "{} Compilation of {} failed! Compiler error message:\n",
- Emoji("⚠️ ", "!"),
- filename
- );
+ println!("{}", String::from_utf8_lossy(&runcmd.stdout));
+ println!("{}", String::from_utf8_lossy(&runcmd.stderr));
+
+ let formatstr = format!("{} Ran {} with errors", Emoji("⚠️ ", "!"), filename);
println!("{}", style(formatstr).red());
- println!("{}", String::from_utf8_lossy(&compilecmd.stderr));
clean();
+ Err(())
}
} else {
- panic!("Please supply a filename!");
+ bar.finish_and_clear();
+ let formatstr = format!(
+ "{} Compilation of {} failed! Compiler error message:\n",
+ Emoji("⚠️ ", "!"),
+ filename
+ );
+ println!("{}", style(formatstr).red());
+ println!("{}", String::from_utf8_lossy(&compilecmd.stderr));
+ clean();
+ Err(())
}
}