rustlings/src/run.rs

73 lines
2.6 KiB
Rust
Raw Normal View History

2019-01-09 14:33:58 -05:00
use crate::util::clean;
2019-01-09 16:04:08 -05:00
use crate::verify::test;
2019-01-09 14:33:43 -05:00
use console::{style, Emoji};
use indicatif::ProgressBar;
use std::fs;
2019-01-09 14:33:43 -05:00
use std::process::Command;
use toml::Value;
2019-01-09 14:33:43 -05:00
pub fn run(matches: clap::ArgMatches) -> Result<(), ()> {
2019-01-09 14:33:43 -05:00
if let Some(filename) = matches.value_of("file") {
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);
2019-01-09 16:04:08 -05:00
}
2019-01-09 14:33:58 -05:00
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!");
}
}
2019-01-09 14:33:58 -05:00
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(())
2019-01-09 14:33:43 -05:00
} else {
println!("{}", String::from_utf8_lossy(&runcmd.stdout));
println!("{}", String::from_utf8_lossy(&runcmd.stderr));
let formatstr = format!("{} Ran {} with errors", Emoji("⚠️ ", "!"), filename);
2019-01-09 14:33:43 -05:00
println!("{}", style(formatstr).red());
clean();
Err(())
2019-01-09 14:33:43 -05:00
}
} else {
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(())
2019-01-09 14:33:43 -05:00
}
}