rustlings/src/run.rs

47 lines
1.6 KiB
Rust
Raw Normal View History

2024-03-31 15:55:33 +01:00
use anyhow::{bail, Result};
use std::io::{self, stdout, Write};
2023-08-26 22:35:07 +01:00
use std::time::Duration;
2024-03-28 21:11:16 +00:00
use crate::embedded::{WriteStrategy, EMBEDDED_FILES};
use crate::exercise::{Exercise, Mode};
2019-01-09 21:04:08 +00:00
use crate::verify::test;
2019-01-09 19:33:43 +00:00
use indicatif::ProgressBar;
// Invoke the rust compiler on the path of the given exercise,
// 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)
2024-03-31 15:55:33 +01:00
pub fn run(exercise: &Exercise, verbose: bool) -> Result<()> {
match exercise.mode {
2024-03-31 15:55:33 +01:00
Mode::Test => test(exercise, verbose),
Mode::Compile | Mode::Clippy => compile_and_run(exercise),
}
}
2019-01-09 19:33:58 +00:00
// Resets the exercise by stashing the changes.
2024-03-28 21:11:16 +00:00
pub fn reset(exercise: &Exercise) -> io::Result<()> {
EMBEDDED_FILES.write_exercise_to_disk(&exercise.path, WriteStrategy::Overwrite)
}
// Invoke the rust compiler on the path of the given exercise
// and run the ensuing binary.
// This is strictly for non-test binaries, so output is displayed
2024-03-31 15:55:33 +01:00
fn compile_and_run(exercise: &Exercise) -> Result<()> {
2019-03-11 14:09:20 +00:00
let progress_bar = ProgressBar::new_spinner();
2024-03-31 15:55:33 +01:00
progress_bar.set_message(format!("Running {exercise}..."));
2023-08-26 22:35:07 +01:00
progress_bar.enable_steady_tick(Duration::from_millis(100));
2019-04-07 17:12:03 +01:00
2024-03-31 15:55:33 +01:00
let output = exercise.run()?;
progress_bar.finish_and_clear();
2024-03-31 15:55:33 +01:00
stdout().write_all(&output.stdout)?;
if !output.status.success() {
stdout().write_all(&output.stderr)?;
warn!("Ran {} with errors", exercise);
bail!("TODO");
2019-01-09 19:33:43 +00:00
}
2024-03-31 15:55:33 +01:00
success!("Successfully ran {}", exercise);
Ok(())
2019-01-09 19:33:43 +00:00
}