use crate::app_state::AppState;
use crate::compiler::Compiler;
use crate::exercise::Exercise;
use crate::runner::RunStatus;
use crate::term::{self, write_stdout};
use std::path::Path;
pub fn report_outcome(
exercise: &Exercise,
compiler: &Compiler,
status: &RunStatus,
revealed: Option<&Path>,
) -> anyhow::Result<()> {
match status {
RunStatus::Passed(result) => {
term::print_success(&format!("{} passed!", exercise.name()))?;
if !result.output.is_empty() {
term::print_stage_output("Program", &result.output)?;
}
if let Some(path) = revealed {
write_stdout("\r\n")?;
term::print_info(&format!(
"Official solution revealed: {} — compare it with yours!",
path.display()
))?;
}
}
RunStatus::Failed(result) => {
term::print_error(&format!(
"{} failed at stage: {}",
exercise.name(),
result.stage
))?;
term::print_stage_output(result.stage, &result.output)?;
}
RunStatus::Unsupported => {
term::print_warning(&format!(
"{} requires {} (current compiler: {}).",
exercise.name(),
exercise.required_compilers(),
compiler.kind()
))?;
}
RunStatus::Missing => {
term::print_error(&format!(
"Exercise file not found: {}",
exercise.path.display()
))?;
}
};
Ok(())
}
pub fn report_terse(exercise: &Exercise, status: &RunStatus) -> anyhow::Result<()> {
match status {
RunStatus::Passed(_) => term::print_success(exercise.name())?,
RunStatus::Failed(result) => term::print_error(&format!(
"{} (failed at: {})",
exercise.name(),
result.stage
))?,
RunStatus::Unsupported => term::print_warning(&format!(
"{}: skipped (requires {})",
exercise.name(),
exercise.required_compilers()
))?,
RunStatus::Missing => term::print_error(&format!("{}: file not found", exercise.name()))?,
};
Ok(())
}
pub fn status_marker(state: &AppState, index: usize, exercise: &Exercise) -> &'static str {
if state.is_done(exercise.name()) {
"✓"
} else if !exercise.supported {
"−"
} else if index == state.current_index {
"→"
} else {
" "
}
}
pub fn compiler_note(exercise: &Exercise) -> String {
if exercise.supported {
String::new()
} else {
format!(" (requires {})", exercise.required_compilers())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::info_file::ExerciseInfo;
use std::path::{Path, PathBuf};
fn make_exercise(name: &str, dir: &Path, supported: bool) -> Exercise {
Exercise {
info: ExerciseInfo {
name: name.to_string(),
dir: "test".to_string(),
test: false,
sanitizers: false,
flags: Vec::new(),
compilers: Some(vec!["gcc".to_string()]),
hint: None,
hints: None,
},
path: dir.join(format!("{name}.c")),
test_path: PathBuf::from("/nonexistent_test.c"),
pristine_path: PathBuf::from("/nonexistent.c"),
legacy_marker: PathBuf::from("/nonexistent/legacy"),
solution_path: PathBuf::from("/nonexistent"),
reveal_path: dir.join(format!("{name}_revealed.c")),
supported,
}
}
#[test]
fn done_beats_unsupported() {
let tmp = tempfile::TempDir::new().unwrap();
let state = {
let mut s =
AppState::from_parts(vec![make_exercise("a", tmp.path(), false)], tmp.path());
s.mark_done("a");
s
};
assert_eq!(status_marker(&state, 0, &state.exercises[0]), "✓");
}
#[test]
fn unsupported_beats_current() {
let tmp = tempfile::TempDir::new().unwrap();
let mut state =
AppState::from_parts(vec![make_exercise("a", tmp.path(), false)], tmp.path());
state.current_index = 0;
assert_eq!(status_marker(&state, 0, &state.exercises[0]), "−");
}
#[test]
fn current_and_plain_exercises() {
let tmp = tempfile::TempDir::new().unwrap();
let mut state = AppState::from_parts(
vec![
make_exercise("a", tmp.path(), true),
make_exercise("b", tmp.path(), true),
],
tmp.path(),
);
state.current_index = 1;
assert_eq!(status_marker(&state, 0, &state.exercises[0]), " ");
assert_eq!(status_marker(&state, 1, &state.exercises[1]), "→");
}
#[test]
fn compiler_note_only_for_unsupported() {
let tmp = tempfile::TempDir::new().unwrap();
assert_eq!(compiler_note(&make_exercise("a", tmp.path(), true)), "");
assert_eq!(
compiler_note(&make_exercise("b", tmp.path(), false)),
" (requires gcc)"
);
}
}