summaryrefslogtreecommitdiff
path: root/src/run.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2020-02-26 11:48:01 +0000
committerbors <bors@rust-lang.org>2020-02-26 11:48:01 +0000
commit7e8530b21ff355c9b52e07eb3ba25278746a6932 (patch)
treef100795973f84cf46fb240adcdaaaa2cd76a1aaf /src/run.rs
parent98358597a981aeeb039d756934d1ff91a2d8d7dc (diff)
parent43dc31193afddc15c78d2ceb57f7c68da90e8a46 (diff)
Auto merge of #271 - jrvidal:refactor, r=fmoko
refactor: exercise evaluation After working a bit on #270, I realized that it'd be useful to first perform a minor refactor of exercise evaluation. * Now we have standard methods to compile + execute that return `Result`s. * Success/failure messages are standardized.
Diffstat (limited to 'src/run.rs')
-rw-r--r--src/run.rs54
1 files changed, 26 insertions, 28 deletions
diff --git a/src/run.rs b/src/run.rs
index 1484351..cfde7ab 100644
--- a/src/run.rs
+++ b/src/run.rs
@@ -1,6 +1,5 @@
use crate::exercise::{Exercise, Mode};
use crate::verify::test;
-use console::{style, Emoji};
use indicatif::ProgressBar;
pub fn run(exercise: &Exercise) -> Result<(), ()> {
@@ -11,42 +10,41 @@ pub fn run(exercise: &Exercise) -> Result<(), ()> {
Ok(())
}
-pub fn compile_and_run(exercise: &Exercise) -> Result<(), ()> {
+fn compile_and_run(exercise: &Exercise) -> Result<(), ()> {
let progress_bar = ProgressBar::new_spinner();
progress_bar.set_message(format!("Compiling {}...", exercise).as_str());
progress_bar.enable_steady_tick(100);
- let compilecmd = exercise.compile();
+ 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());
- if compilecmd.status.success() {
- let runcmd = exercise.run();
- progress_bar.finish_and_clear();
+ let result = compilation.run();
+ progress_bar.finish_and_clear();
- if runcmd.status.success() {
- println!("{}", String::from_utf8_lossy(&runcmd.stdout));
- let formatstr = format!("{} Successfully ran {}", Emoji("✅", "✓"), exercise);
- println!("{}", style(formatstr).green());
- exercise.clean();
+ match result {
+ Ok(output) => {
+ println!("{}", output.stdout);
+ success!("Successfully ran {}", exercise);
Ok(())
- } else {
- println!("{}", String::from_utf8_lossy(&runcmd.stdout));
- println!("{}", String::from_utf8_lossy(&runcmd.stderr));
+ }
+ Err(output) => {
+ println!("{}", output.stdout);
+ println!("{}", output.stderr);
- let formatstr = format!("{} Ran {} with errors", Emoji("⚠️ ", "!"), exercise);
- println!("{}", style(formatstr).red());
- exercise.clean();
+ warn!("Ran {} with errors", exercise);
Err(())
}
- } else {
- progress_bar.finish_and_clear();
- let formatstr = format!(
- "{} Compilation of {} failed! Compiler error message:\n",
- Emoji("⚠️ ", "!"),
- exercise
- );
- println!("{}", style(formatstr).red());
- println!("{}", String::from_utf8_lossy(&compilecmd.stderr));
- exercise.clean();
- Err(())
}
}