diff options
| author | Roberto Vidal <vidal.roberto.j@gmail.com> | 2020-02-20 20:11:53 +0100 |
|---|---|---|
| committer | Roberto Vidal <vidal.roberto.j@gmail.com> | 2020-02-20 20:27:05 +0100 |
| commit | 43dc31193afddc15c78d2ceb57f7c68da90e8a46 (patch) | |
| tree | 83e1bc003d79916aa934d7b42f05e94ce073f2ed /src/run.rs | |
| parent | 83bbd9e82e98b4ec1ab81a1d55bb8688e920a905 (diff) | |
refactor: exercise evaluation
Exercise evaluation (compilation + execution) now uses Results
Success/failure messages are standardized
Diffstat (limited to 'src/run.rs')
| -rw-r--r-- | src/run.rs | 54 |
1 files changed, 26 insertions, 28 deletions
@@ -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(()) } } |
