1
0
mirror of https://github.com/rust-lang/rustlings.git synced 2025-06-06 23:36:37 +02:00
rustlings/src/run.rs

36 lines
974 B
Rust
Raw Normal View History

2024-04-05 00:44:43 +02:00
use anyhow::{bail, Result};
2024-04-09 17:15:12 +02:00
use crossterm::style::Stylize;
use std::io::{self, Write};
use crate::app_state::{AppState, ExercisesProgress};
2019-01-09 20:33:43 +01:00
pub fn run(app_state: &mut AppState) -> Result<()> {
let exercise = app_state.current_exercise();
2024-03-31 16:55:33 +02:00
let output = exercise.run()?;
let mut stdout = io::stdout().lock();
stdout.write_all(&output.stdout)?;
stdout.write_all(b"\n")?;
stdout.write_all(&output.stderr)?;
stdout.flush()?;
2024-04-04 21:06:11 +02:00
2024-04-05 00:44:43 +02:00
if !output.status.success() {
2024-04-12 19:24:26 +02:00
app_state.set_pending(app_state.current_exercise_ind())?;
2024-04-05 00:44:43 +02:00
bail!("Ran {exercise} with errors");
2019-01-09 20:33:43 +01:00
}
2024-03-31 16:55:33 +02:00
stdout.write_fmt(format_args!(
"{}{}",
"✓ Successfully ran ".green(),
exercise.path.to_string_lossy().green(),
))?;
match app_state.done_current_exercise(&mut stdout)? {
ExercisesProgress::AllDone => (),
ExercisesProgress::Pending => println!("Next exercise: {}", app_state.current_exercise()),
}
2024-04-05 00:44:43 +02:00
2024-03-31 16:55:33 +02:00
Ok(())
2019-01-09 20:33:43 +01:00
}