rustlings/src/run.rs

52 lines
1.5 KiB
Rust
Raw Normal View History

use crate::exercise::{Exercise, Mode};
2019-01-10 04:04:08 +07:00
use crate::verify::test;
2019-01-10 02:33:43 +07:00
use indicatif::ProgressBar;
pub fn run(exercise: &Exercise) -> Result<(), ()> {
match exercise.mode {
Mode::Test => test(exercise)?,
Mode::Compile => compile_and_run(exercise)?,
Mode::Clippy => compile_and_run(exercise)?,
}
Ok(())
}
2019-01-10 02:33:58 +07:00
fn compile_and_run(exercise: &Exercise) -> Result<(), ()> {
2019-03-11 21:09:20 +07:00
let progress_bar = ProgressBar::new_spinner();
progress_bar.set_message(format!("Compiling {}...", exercise).as_str());
2019-03-11 21:09:20 +07:00
progress_bar.enable_steady_tick(100);
2019-04-07 23:12:03 +07: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(());
}
};
progress_bar.set_message(format!("Running {}...", exercise).as_str());
let result = compilation.run();
progress_bar.finish_and_clear();
match result {
Ok(output) => {
println!("{}", output.stdout);
success!("Successfully ran {}", exercise);
Ok(())
}
Err(output) => {
println!("{}", output.stdout);
println!("{}", output.stderr);
warn!("Ran {} with errors", exercise);
Err(())
2019-01-10 02:33:43 +07:00
}
}
}