summaryrefslogtreecommitdiff
path: root/src/verify.rs
blob: aec2185c272ab4a6c13384e0fe012424b2be7364 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use anyhow::Result;
use crossterm::style::{Attribute, ContentStyle, Stylize};
use std::io::{stdout, Write};

use crate::exercise::{Exercise, Mode, State};

pub enum VerifyState<'a> {
    AllExercisesDone,
    Failed(&'a Exercise),
}

// Verify that the provided container of Exercise objects
// can be compiled and run without any failures.
// Any such failures will be reported to the end user.
// If the Exercise being verified is a test, the verbose boolean
// determines whether or not the test harness outputs are displayed.
pub fn verify<'a>(
    pending_exercises: impl IntoIterator<Item = &'a Exercise>,
    progress: (usize, usize),
) -> Result<VerifyState<'a>> {
    let (mut num_done, total) = progress;
    println!(
        "Progress: {num_done}/{total} ({:.1}%)\n",
        num_done as f32 / total as f32 * 100.0,
    );

    for exercise in pending_exercises {
        let output = exercise.run()?;

        {
            let mut stdout = stdout().lock();
            stdout.write_all(&output.stdout)?;
            stdout.write_all(&output.stderr)?;
            stdout.flush()?;
        }

        if !output.status.success() {
            return Ok(VerifyState::Failed(exercise));
        }

        println!();
        // TODO: Color
        match exercise.mode {
            Mode::Compile => println!("Successfully ran {exercise}!"),
            Mode::Test => println!("Successfully tested {exercise}!"),
            Mode::Clippy => println!("Successfully checked {exercise}!"),
        }

        if let State::Pending(context) = exercise.state()? {
            println!(
                "\nYou can keep working on this exercise,
or jump into the next one by removing the {} comment:\n",
                "`I AM NOT DONE`".bold()
            );

            for context_line in context {
                let formatted_line = if context_line.important {
                    format!("{}", context_line.line.bold())
                } else {
                    context_line.line
                };

                println!(
                    "{:>2} {}  {}",
                    ContentStyle {
                        foreground_color: Some(crossterm::style::Color::Blue),
                        background_color: None,
                        underline_color: None,
                        attributes: Attribute::Bold.into()
                    }
                    .apply(context_line.number),
                    "|".blue(),
                    formatted_line,
                );
            }
            return Ok(VerifyState::Failed(exercise));
        }

        num_done += 1;
        println!(
            "Progress: {num_done}/{total} ({:.1}%)\n",
            num_done as f32 / total as f32 * 100.0,
        );
    }

    Ok(VerifyState::AllExercisesDone)
}