From 70e59cca3caf92a1daedac4fcf5d8940215e5529 Mon Sep 17 00:00:00 2001 From: lyn <819880950@qq.com> Date: Wed, 6 Mar 2019 21:47:00 +0100 Subject: standardize exercise running via an external toml file --- src/run.rs | 93 +++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 55 insertions(+), 38 deletions(-) (limited to 'src/run.rs') 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 = 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(()) } } -- cgit v1.2.3