rustlings/src/run.rs

29 lines
767 B
Rust
Raw Normal View History

2024-04-04 18:44:43 -04:00
use anyhow::{bail, Result};
2024-04-09 11:15:12 -04:00
use crossterm::style::Stylize;
2024-03-31 14:08:23 -04:00
use std::io::{stdout, Write};
2024-04-04 15:06:11 -04:00
use crate::exercise::Exercise;
2019-01-09 14:33:43 -05:00
// 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-04-04 15:06:11 -04:00
pub fn run(exercise: &Exercise) -> Result<()> {
2024-03-31 10:55:33 -04:00
let output = exercise.run()?;
2024-04-04 15:06:11 -04:00
{
let mut stdout = stdout().lock();
stdout.write_all(&output.stdout)?;
stdout.write_all(&output.stderr)?;
stdout.flush()?;
}
2024-04-04 18:44:43 -04:00
if !output.status.success() {
bail!("Ran {exercise} with errors");
2019-01-09 14:33:43 -05:00
}
2024-03-31 10:55:33 -04:00
2024-04-09 11:15:12 -04:00
println!("{}", "✓ Successfully ran {exercise}".green());
2024-04-04 18:44:43 -04:00
2024-03-31 10:55:33 -04:00
Ok(())
2019-01-09 14:33:43 -05:00
}