rustlings-solutions/src/run.rs

56 lines
2.0 KiB
Rust
Raw Normal View History

2019-01-09 19:33:58 +00:00
use crate::util::clean;
2019-01-09 21:04:08 +00:00
use crate::verify::test;
2019-01-09 19:33:43 +00:00
use console::{style, Emoji};
use indicatif::ProgressBar;
use std::process::Command;
pub fn run(matches: clap::ArgMatches) {
if let Some(filename) = matches.value_of("file") {
2019-01-09 21:04:08 +00:00
if matches.is_present("test") {
match test(filename) {
Ok(_) => (),
Err(_) => (),
}
std::process::exit(0);
}
2019-01-09 19:33:43 +00:00
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"])
.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();
2019-01-09 19:33:58 +00:00
2019-01-09 19:33:43 +00:00
if runcmd.status.success() {
println!("{}", String::from_utf8_lossy(&runcmd.stdout));
2019-01-09 19:33:58 +00:00
let formatstr = format!("{} Successfully ran {}", Emoji("", ""), filename);
2019-01-09 19:33:43 +00:00
println!("{}", style(formatstr).green());
clean();
} else {
println!("{}", String::from_utf8_lossy(&runcmd.stdout));
println!("{}", String::from_utf8_lossy(&runcmd.stderr));
2019-01-09 19:33:58 +00:00
let formatstr = format!("{} Ran {} with errors", Emoji("⚠️ ", "!"), filename);
2019-01-09 19:33:43 +00:00
println!("{}", style(formatstr).red());
clean();
}
} 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();
}
} else {
panic!("Please supply a filename!");
}
}