rustlings/src/run.rs

38 lines
1,000 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};
use crate::app_state::{AppState, ExercisesProgress};
2019-01-09 14:33:43 -05:00
pub fn run(app_state: &mut AppState) -> Result<()> {
let exercise = app_state.current_exercise();
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
println!(
"{}{}",
"✓ Successfully ran ".green(),
exercise.path.to_string_lossy().green(),
);
match app_state.done_current_exercise()? {
ExercisesProgress::AllDone => println!(
"🎉 Congratulations! You have done all the exercises!
🔚 There are no more exercises to do next!"
),
ExercisesProgress::Pending => println!("Next exercise: {}", app_state.current_exercise()),
}
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
}