rustlings/src/run.rs

47 lines
1.6 KiB
Rust
Raw Normal View History

2024-03-31 10:55:33 -04:00
use anyhow::{bail, Result};
use std::io::{self, stdout, Write};
2023-08-26 17:35:07 -04:00
use std::time::Duration;
2024-03-28 17:11:16 -04:00
use crate::embedded::{WriteStrategy, EMBEDDED_FILES};
use crate::exercise::{Exercise, Mode};
2019-01-09 16:04:08 -05:00
use crate::verify::test;
2019-01-09 14:33:43 -05: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 10:55:33 -04:00
pub fn run(exercise: &Exercise, verbose: bool) -> Result<()> {
match exercise.mode {
2024-03-31 10:55:33 -04:00
Mode::Test => test(exercise, verbose),
Mode::Compile | Mode::Clippy => compile_and_run(exercise),
}
}
2019-01-09 14:33:58 -05:00
// Resets the exercise by stashing the changes.
2024-03-28 17:11:16 -04: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 10:55:33 -04:00
fn compile_and_run(exercise: &Exercise) -> Result<()> {
2019-03-11 10:09:20 -04:00
let progress_bar = ProgressBar::new_spinner();
2024-03-31 10:55:33 -04:00
progress_bar.set_message(format!("Running {exercise}..."));
2023-08-26 17:35:07 -04:00
progress_bar.enable_steady_tick(Duration::from_millis(100));
2019-04-07 12:12:03 -04:00
2024-03-31 10:55:33 -04:00
let output = exercise.run()?;
progress_bar.finish_and_clear();
2024-03-31 10:55:33 -04: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 14:33:43 -05:00
}
2024-03-31 10:55:33 -04:00
success!("Successfully ran {}", exercise);
Ok(())
2019-01-09 14:33:43 -05:00
}