oxedyne/fe2o3/fe2o3_text/tests/annealer_corpus/rustlings_run.rs
1.8 KiB, 1 run
created by r1870400018:11818, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use anyhow::Result; |
| 2 | use crossterm::{ |
| 3 | QueueableCommand, |
| 4 | style::{Color, ResetColor, SetForegroundColor}, |
| 5 | }; |
| 6 | use std::{ |
| 7 | io::{self, Write}, |
| 8 | process::ExitCode, |
| 9 | }; |
| 10 | |
| 11 | use crate::{ |
| 12 | app_state::{AppState, ExercisesProgress}, |
| 13 | exercise::{OUTPUT_CAPACITY, RunnableExercise, solution_link_line}, |
| 14 | }; |
| 15 | |
| 16 | pub fn run(app_state: &mut AppState) -> Result<ExitCode> { |
| 17 | let exercise = app_state.current_exercise(); |
| 18 | let mut output = Vec::with_capacity(OUTPUT_CAPACITY); |
| 19 | let success = exercise.run_exercise(Some(&mut output), app_state.cmd_runner())?; |
| 20 | |
| 21 | let mut stdout = io::stdout().lock(); |
| 22 | stdout.write_all(&output)?; |
| 23 | |
| 24 | if !success { |
| 25 | app_state.set_pending(app_state.current_exercise_ind())?; |
| 26 | |
| 27 | stdout.write_all(b"Ran ")?; |
| 28 | app_state |
| 29 | .current_exercise() |
| 30 | .terminal_file_link(&mut stdout, app_state.emit_file_links())?; |
| 31 | stdout.write_all(b" with errors\n")?; |
| 32 | |
| 33 | return Ok(ExitCode::FAILURE); |
| 34 | } |
| 35 | |
| 36 | stdout.queue(SetForegroundColor(Color::Green))?; |
| 37 | stdout.write_all("✓ Successfully ran ".as_bytes())?; |
| 38 | stdout.write_all(exercise.path.as_bytes())?; |
| 39 | stdout.queue(ResetColor)?; |
| 40 | stdout.write_all(b"\n")?; |
| 41 | |
| 42 | if let Some(solution_path) = app_state.current_solution_path()? { |
| 43 | stdout.write_all(b"\n")?; |
| 44 | solution_link_line(&mut stdout, &solution_path, app_state.emit_file_links())?; |
| 45 | stdout.write_all(b"\n")?; |
| 46 | } |
| 47 | |
| 48 | match app_state.done_current_exercise::<false>(&mut stdout)? { |
| 49 | ExercisesProgress::NewPending | ExercisesProgress::CurrentPending => { |
| 50 | stdout.write_all(b"Next exercise: ")?; |
| 51 | app_state |
| 52 | .current_exercise() |
| 53 | .terminal_file_link(&mut stdout, app_state.emit_file_links())?; |
| 54 | stdout.write_all(b"\n")?; |
| 55 | } |
| 56 | ExercisesProgress::AllDone => (), |
| 57 | } |
| 58 | |
| 59 | Ok(ExitCode::SUCCESS) |
| 60 | } |