summaryrefslogtreecommitdiff
path: root/src/verify.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/verify.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/verify.rs')
-rw-r--r--src/verify.rs104
1 files changed, 52 insertions, 52 deletions
diff --git a/src/verify.rs b/src/verify.rs
index 3796bbd..3d14896 100644
--- a/src/verify.rs
+++ b/src/verify.rs
@@ -1,11 +1,11 @@
use crate::exercise::{Exercise, Mode, State};
-use console::{style, Emoji};
+use console::style;
use indicatif::ProgressBar;
pub fn verify<'a>(start_at: impl IntoIterator<Item = &'a Exercise>) -> Result<(), &'a Exercise> {
for exercise in start_at {
let compile_result = match exercise.mode {
- Mode::Test => compile_and_test_interactively(&exercise),
+ Mode::Test => compile_and_test(&exercise, RunMode::Interactive),
Mode::Compile => compile_only(&exercise),
};
if !compile_result.unwrap_or(false) {
@@ -15,8 +15,13 @@ pub fn verify<'a>(start_at: impl IntoIterator<Item = &'a Exercise>) -> Result<()
Ok(())
}
+enum RunMode {
+ Interactive,
+ NonInteractive,
+}
+
pub fn test(exercise: &Exercise) -> Result<(), ()> {
- compile_and_test(exercise, true)?;
+ compile_and_test(exercise, RunMode::NonInteractive)?;
Ok(())
}
@@ -24,69 +29,64 @@ fn compile_only(exercise: &Exercise) -> Result<bool, ()> {
let progress_bar = ProgressBar::new_spinner();
progress_bar.set_message(format!("Compiling {}...", exercise).as_str());
progress_bar.enable_steady_tick(100);
- let compile_output = exercise.compile();
+ let compilation_result = exercise.compile();
progress_bar.finish_and_clear();
- if compile_output.status.success() {
- let formatstr = format!("{} Successfully compiled {}!", Emoji("✅", "✓"), exercise);
- println!("{}", style(formatstr).green());
- exercise.clean();
- Ok(prompt_for_completion(&exercise))
- } else {
- let formatstr = format!(
- "{} Compilation of {} failed! Compiler error message:\n",
- Emoji("⚠️ ", "!"),
- exercise
- );
- println!("{}", style(formatstr).red());
- println!("{}", String::from_utf8_lossy(&compile_output.stderr));
- exercise.clean();
- Err(())
- }
-}
-fn compile_and_test_interactively(exercise: &Exercise) -> Result<bool, ()> {
- compile_and_test(exercise, false)
+ match compilation_result {
+ Ok(_) => {
+ success!("Successfully compiled {}!", exercise);
+ Ok(prompt_for_completion(&exercise))
+ }
+ Err(output) => {
+ warn!(
+ "Compilation of {} failed! Compiler error message:\n",
+ exercise
+ );
+ println!("{}", output.stderr);
+ Err(())
+ }
+ }
}
-fn compile_and_test(exercise: &Exercise, skip_prompt: bool) -> Result<bool, ()> {
+fn compile_and_test(exercise: &Exercise, run_mode: RunMode) -> Result<bool, ()> {
let progress_bar = ProgressBar::new_spinner();
progress_bar.set_message(format!("Testing {}...", exercise).as_str());
progress_bar.enable_steady_tick(100);
- let compile_output = exercise.compile();
- if compile_output.status.success() {
- progress_bar.set_message(format!("Running {}...", exercise).as_str());
+ let compilation_result = exercise.compile();
+
+ let compilation = match compilation_result {
+ Ok(compilation) => compilation,
+ Err(output) => {
+ progress_bar.finish_and_clear();
+ warn!(
+ "Compiling of {} failed! Please try again. Here's the output:",
+ exercise
+ );
+ println!("{}", output.stderr);
+ return Err(());
+ }
+ };
- let runcmd = exercise.run();
- progress_bar.finish_and_clear();
+ let result = compilation.run();
+ progress_bar.finish_and_clear();
- if runcmd.status.success() {
- let formatstr = format!("{} Successfully tested {}!", Emoji("✅", "✓"), exercise);
- println!("{}", style(formatstr).green());
- exercise.clean();
- Ok(skip_prompt || prompt_for_completion(exercise))
- } else {
- let formatstr = format!(
- "{} Testing of {} failed! Please try again. Here's the output:",
- Emoji("⚠️ ", "!"),
+ match result {
+ Ok(_) => {
+ if let RunMode::Interactive = run_mode {
+ Ok(prompt_for_completion(&exercise))
+ } else {
+ Ok(true)
+ }
+ }
+ Err(output) => {
+ warn!(
+ "Testing of {} failed! Please try again. Here's the output:",
exercise
);
- println!("{}", style(formatstr).red());
- println!("{}", String::from_utf8_lossy(&runcmd.stdout));
- exercise.clean();
+ println!("{}", output.stdout);
Err(())
}
- } else {
- progress_bar.finish_and_clear();
- let formatstr = format!(
- "{} Compiling of {} failed! Please try again. Here's the output:",
- Emoji("⚠️ ", "!"),
- exercise
- );
- println!("{}", style(formatstr).red());
- println!("{}", String::from_utf8_lossy(&compile_output.stderr));
- exercise.clean();
- Err(())
}
}