2019-05-22 14:50:23 +03:00
|
|
|
use crate::exercise::{Exercise, Mode};
|
2019-01-10 00:04:08 +03:00
|
|
|
use crate::verify::test;
|
2019-01-09 22:33:43 +03:00
|
|
|
use indicatif::ProgressBar;
|
|
|
|
|
2019-04-11 23:41:24 +03:00
|
|
|
pub fn run(exercise: &Exercise) -> Result<(), ()> {
|
|
|
|
match exercise.mode {
|
2019-11-12 13:35:40 +03:00
|
|
|
Mode::Test => test(exercise)?,
|
2019-04-11 23:41:24 +03:00
|
|
|
Mode::Compile => compile_and_run(exercise)?,
|
2019-03-06 23:47:00 +03:00
|
|
|
}
|
2019-04-11 23:41:24 +03:00
|
|
|
Ok(())
|
2019-03-06 23:47:00 +03:00
|
|
|
}
|
2019-01-09 22:33:58 +03:00
|
|
|
|
2020-02-20 22:11:53 +03:00
|
|
|
fn compile_and_run(exercise: &Exercise) -> Result<(), ()> {
|
2019-03-11 17:09:20 +03:00
|
|
|
let progress_bar = ProgressBar::new_spinner();
|
2019-04-11 23:41:24 +03:00
|
|
|
progress_bar.set_message(format!("Compiling {}...", exercise).as_str());
|
2019-03-11 17:09:20 +03:00
|
|
|
progress_bar.enable_steady_tick(100);
|
2019-04-07 19:12:03 +03:00
|
|
|
|
2020-02-20 22:11:53 +03:00
|
|
|
let compilation_result = exercise.compile();
|
|
|
|
let compilation = match compilation_result {
|
|
|
|
Ok(compilation) => compilation,
|
|
|
|
Err(output) => {
|
|
|
|
progress_bar.finish_and_clear();
|
|
|
|
warn!(
|
|
|
|
"Compilation of {} failed!, Compiler error message:\n",
|
|
|
|
exercise
|
|
|
|
);
|
|
|
|
println!("{}", output.stderr);
|
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-11 23:41:24 +03:00
|
|
|
progress_bar.set_message(format!("Running {}...", exercise).as_str());
|
2020-02-20 22:11:53 +03:00
|
|
|
let result = compilation.run();
|
|
|
|
progress_bar.finish_and_clear();
|
2019-03-06 23:47:00 +03:00
|
|
|
|
2020-02-20 22:11:53 +03:00
|
|
|
match result {
|
|
|
|
Ok(output) => {
|
|
|
|
println!("{}", output.stdout);
|
|
|
|
success!("Successfully ran {}", exercise);
|
2019-03-06 23:47:00 +03:00
|
|
|
Ok(())
|
2020-02-20 22:11:53 +03:00
|
|
|
}
|
|
|
|
Err(output) => {
|
|
|
|
println!("{}", output.stdout);
|
|
|
|
println!("{}", output.stderr);
|
2019-03-06 23:47:00 +03:00
|
|
|
|
2020-02-20 22:11:53 +03:00
|
|
|
warn!("Ran {} with errors", exercise);
|
2019-03-06 23:47:00 +03:00
|
|
|
Err(())
|
2019-01-09 22:33:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|