diff options
| author | Mo <76752051+mo8it@users.noreply.github.com> | 2024-04-04 15:48:07 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-04 15:48:07 +0200 |
| commit | 8c8f30d8ce3b732de649938d8945496bd769ac22 (patch) | |
| tree | 3679a78e468872f05cec1de4e489acbbc08a11a8 /src/run.rs | |
| parent | 459c52137ac7b8aa8500a46f04b0e848ba48a969 (diff) | |
| parent | b6c434c445d91a9e886e5639b078635e5eca4eb3 (diff) | |
Merge pull request #1931 from mo8it/standalone-binary
Standalone binary
Diffstat (limited to 'src/run.rs')
| -rw-r--r-- | src/run.rs | 70 |
1 files changed, 17 insertions, 53 deletions
@@ -1,4 +1,5 @@ -use std::process::Command; +use anyhow::{bail, Result}; +use std::io::{stdout, Write}; use std::time::Duration; use crate::exercise::{Exercise, Mode}; @@ -9,67 +10,30 @@ use indicatif::ProgressBar; // and run the ensuing binary. // The verbose argument helps determine whether or not to show // the output from the test harnesses (if the mode of the exercise is test) -pub fn run(exercise: &Exercise, verbose: bool) -> Result<(), ()> { +pub fn run(exercise: &Exercise, verbose: bool) -> Result<()> { match exercise.mode { - Mode::Test => test(exercise, verbose)?, - Mode::Compile => compile_and_run(exercise)?, - Mode::Clippy => compile_and_run(exercise)?, - } - Ok(()) -} - -// Resets the exercise by stashing the changes. -pub fn reset(exercise: &Exercise) -> Result<(), ()> { - let command = Command::new("git") - .arg("stash") - .arg("--") - .arg(&exercise.path) - .spawn(); - - match command { - Ok(_) => Ok(()), - Err(_) => Err(()), + Mode::Test => test(exercise, verbose), + Mode::Compile | Mode::Clippy => compile_and_run(exercise), } } -// Invoke the rust compiler on the path of the given exercise -// and run the ensuing binary. +// Compile and run an exercise. // This is strictly for non-test binaries, so output is displayed -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}...")); + progress_bar.set_message(format!("Running {exercise}...")); progress_bar.enable_steady_tick(Duration::from_millis(100)); - 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}...")); - let result = compilation.run(); + let output = exercise.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(()) - } + stdout().write_all(&output.stdout)?; + if !output.status.success() { + stdout().write_all(&output.stderr)?; + warn!("Ran {} with errors", exercise); + bail!("TODO"); } + + success!("Successfully ran {}", exercise); + Ok(()) } |
