2024-03-31 10:55:33 -04:00
|
|
|
use anyhow::{Context, Result};
|
2019-04-12 17:48:57 -04:00
|
|
|
use serde::Deserialize;
|
2024-03-31 10:55:33 -04:00
|
|
|
use std::fmt::{self, Debug, Display, Formatter};
|
|
|
|
use std::fs::{self, File};
|
2024-03-24 13:34:46 -04:00
|
|
|
use std::io::{self, BufRead, BufReader};
|
2019-05-22 07:50:23 -04:00
|
|
|
use std::path::PathBuf;
|
2024-03-31 10:55:33 -04:00
|
|
|
use std::process::{exit, Command, Output};
|
|
|
|
use std::{array, mem};
|
2024-03-24 17:22:55 -04:00
|
|
|
use winnow::ascii::{space0, Caseless};
|
2024-03-24 14:18:19 -04:00
|
|
|
use winnow::combinator::opt;
|
|
|
|
use winnow::Parser;
|
2019-04-11 16:41:24 -04:00
|
|
|
|
2024-03-31 10:55:33 -04:00
|
|
|
use crate::embedded::EMBEDDED_FILES;
|
|
|
|
|
|
|
|
// The number of context lines above and below a highlighted line.
|
2019-11-11 07:38:24 -05:00
|
|
|
const CONTEXT: usize = 2;
|
2019-04-11 16:41:24 -04:00
|
|
|
|
2024-03-31 10:55:33 -04:00
|
|
|
// Check if the line contains the "I AM NOT DONE" comment.
|
2024-03-25 21:14:25 -04:00
|
|
|
fn contains_not_done_comment(input: &str) -> bool {
|
2024-03-24 14:18:19 -04:00
|
|
|
(
|
|
|
|
space0::<_, ()>,
|
|
|
|
"//",
|
|
|
|
opt('/'),
|
|
|
|
space0,
|
2024-03-24 17:22:55 -04:00
|
|
|
Caseless("I AM NOT DONE"),
|
2024-03-24 14:18:19 -04:00
|
|
|
)
|
|
|
|
.parse_next(&mut &*input)
|
|
|
|
.is_ok()
|
|
|
|
}
|
|
|
|
|
2020-06-04 10:31:17 -04:00
|
|
|
// The mode of the exercise.
|
2024-03-31 10:55:33 -04:00
|
|
|
#[derive(Deserialize, Copy, Clone)]
|
2019-04-11 16:41:24 -04:00
|
|
|
#[serde(rename_all = "lowercase")]
|
|
|
|
pub enum Mode {
|
2024-03-31 10:55:33 -04:00
|
|
|
// The exercise should be compiled as a binary
|
2019-04-11 16:41:24 -04:00
|
|
|
Compile,
|
2024-03-31 10:55:33 -04:00
|
|
|
// The exercise should be compiled as a test harness
|
2019-04-11 16:41:24 -04:00
|
|
|
Test,
|
2024-03-31 10:55:33 -04:00
|
|
|
// The exercise should be linted with clippy
|
2020-02-14 09:25:03 -05:00
|
|
|
Clippy,
|
2019-04-11 16:41:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct ExerciseList {
|
|
|
|
pub exercises: Vec<Exercise>,
|
|
|
|
}
|
|
|
|
|
2024-03-31 10:55:33 -04:00
|
|
|
impl ExerciseList {
|
|
|
|
pub fn parse() -> Result<Self> {
|
|
|
|
// Read a local `info.toml` if it exists.
|
|
|
|
// Mainly to let the tests work for now.
|
|
|
|
if let Ok(file_content) = fs::read_to_string("info.toml") {
|
|
|
|
toml_edit::de::from_str(&file_content)
|
|
|
|
} else {
|
|
|
|
toml_edit::de::from_str(EMBEDDED_FILES.info_toml_content)
|
|
|
|
}
|
|
|
|
.context("Failed to parse `info.toml`")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialized from the `info.toml` file.
|
|
|
|
#[derive(Deserialize)]
|
2019-04-11 16:41:24 -04:00
|
|
|
pub struct Exercise {
|
2020-06-04 10:31:17 -04:00
|
|
|
// Name of the exercise
|
2019-11-11 09:46:32 -05:00
|
|
|
pub name: String,
|
2020-06-04 10:31:17 -04:00
|
|
|
// The path to the file containing the exercise's source code
|
2019-04-11 16:41:24 -04:00
|
|
|
pub path: PathBuf,
|
2024-03-31 10:55:33 -04:00
|
|
|
// The mode of the exercise
|
2019-04-11 16:41:24 -04:00
|
|
|
pub mode: Mode,
|
2020-06-04 10:31:17 -04:00
|
|
|
// The hint text associated with the exercise
|
2019-11-11 10:51:38 -05:00
|
|
|
pub hint: String,
|
2019-04-11 16:41:24 -04:00
|
|
|
}
|
|
|
|
|
2024-03-31 10:55:33 -04:00
|
|
|
// The state of an Exercise.
|
2024-03-26 12:47:33 -04:00
|
|
|
#[derive(PartialEq, Eq, Debug)]
|
2019-11-11 07:38:24 -05:00
|
|
|
pub enum State {
|
|
|
|
Done,
|
|
|
|
Pending(Vec<ContextLine>),
|
|
|
|
}
|
|
|
|
|
2024-03-31 10:55:33 -04:00
|
|
|
// The context information of a pending exercise.
|
2024-03-26 12:47:33 -04:00
|
|
|
#[derive(PartialEq, Eq, Debug)]
|
2019-11-11 07:38:24 -05:00
|
|
|
pub struct ContextLine {
|
2024-03-31 10:55:33 -04:00
|
|
|
// The source code line
|
2019-11-11 07:38:24 -05:00
|
|
|
pub line: String,
|
2024-03-31 10:55:33 -04:00
|
|
|
// The line number
|
2019-11-11 07:38:24 -05:00
|
|
|
pub number: usize,
|
2024-03-31 10:55:33 -04:00
|
|
|
// Whether this is important and should be highlighted
|
2019-11-11 07:38:24 -05:00
|
|
|
pub important: bool,
|
|
|
|
}
|
|
|
|
|
2019-04-11 16:41:24 -04:00
|
|
|
impl Exercise {
|
2024-03-31 10:55:33 -04:00
|
|
|
fn cargo_cmd(&self, command: &str, args: &[&str]) -> Result<Output> {
|
|
|
|
Command::new("cargo")
|
|
|
|
.arg(command)
|
|
|
|
.arg("--color")
|
|
|
|
.arg("always")
|
|
|
|
.arg("-q")
|
|
|
|
.arg("--bin")
|
|
|
|
.arg(&self.name)
|
|
|
|
.args(args)
|
2019-04-11 16:41:24 -04:00
|
|
|
.output()
|
2024-03-31 10:55:33 -04:00
|
|
|
.context("Failed to run Cargo")
|
|
|
|
}
|
2019-04-11 16:41:24 -04:00
|
|
|
|
2024-03-31 10:55:33 -04:00
|
|
|
pub fn run(&self) -> Result<Output> {
|
|
|
|
match self.mode {
|
|
|
|
Mode::Compile => self.cargo_cmd("run", &[]),
|
|
|
|
Mode::Test => self.cargo_cmd("test", &["--", "--nocapture"]),
|
|
|
|
Mode::Clippy => self.cargo_cmd(
|
|
|
|
"clippy",
|
|
|
|
&["--", "-D", "warnings", "-D", "clippy::float_cmp"],
|
|
|
|
),
|
2020-02-20 14:11:53 -05:00
|
|
|
}
|
2019-04-11 16:41:24 -04:00
|
|
|
}
|
2019-11-11 07:38:24 -05:00
|
|
|
|
|
|
|
pub fn state(&self) -> State {
|
2024-03-24 13:34:46 -04:00
|
|
|
let source_file = File::open(&self.path).unwrap_or_else(|e| {
|
2024-03-25 21:14:25 -04:00
|
|
|
println!(
|
|
|
|
"Failed to open the exercise file {}: {e}",
|
|
|
|
self.path.display(),
|
|
|
|
);
|
|
|
|
exit(1);
|
2023-09-25 03:36:43 -04:00
|
|
|
});
|
2024-03-24 13:34:46 -04:00
|
|
|
let mut source_reader = BufReader::new(source_file);
|
2024-03-25 21:14:25 -04:00
|
|
|
|
|
|
|
// Read the next line into `buf` without the newline at the end.
|
2024-03-24 13:34:46 -04:00
|
|
|
let mut read_line = |buf: &mut String| -> io::Result<_> {
|
|
|
|
let n = source_reader.read_line(buf)?;
|
|
|
|
if buf.ends_with('\n') {
|
|
|
|
buf.pop();
|
|
|
|
if buf.ends_with('\r') {
|
|
|
|
buf.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(n)
|
2019-11-11 07:38:24 -05:00
|
|
|
};
|
|
|
|
|
2024-03-25 21:14:25 -04:00
|
|
|
let mut current_line_number: usize = 1;
|
2024-03-25 21:26:26 -04:00
|
|
|
// Keep the last `CONTEXT` lines while iterating over the file lines.
|
2024-03-24 13:34:46 -04:00
|
|
|
let mut prev_lines: [_; CONTEXT] = array::from_fn(|_| String::with_capacity(256));
|
|
|
|
let mut line = String::with_capacity(256);
|
2019-11-11 07:38:24 -05:00
|
|
|
|
2024-03-24 13:34:46 -04:00
|
|
|
loop {
|
2024-03-25 21:14:25 -04:00
|
|
|
let n = read_line(&mut line).unwrap_or_else(|e| {
|
|
|
|
println!(
|
|
|
|
"Failed to read the exercise file {}: {e}",
|
|
|
|
self.path.display(),
|
|
|
|
);
|
|
|
|
exit(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Reached the end of the file and didn't find the comment.
|
|
|
|
if n == 0 {
|
|
|
|
return State::Done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if contains_not_done_comment(&line) {
|
|
|
|
let mut context = Vec::with_capacity(2 * CONTEXT + 1);
|
2024-03-25 21:26:26 -04:00
|
|
|
// Previous lines.
|
2024-03-25 21:14:25 -04:00
|
|
|
for (ind, prev_line) in prev_lines
|
|
|
|
.into_iter()
|
|
|
|
.take(current_line_number - 1)
|
|
|
|
.enumerate()
|
|
|
|
.rev()
|
|
|
|
{
|
|
|
|
context.push(ContextLine {
|
|
|
|
line: prev_line,
|
|
|
|
number: current_line_number - 1 - ind,
|
|
|
|
important: false,
|
|
|
|
});
|
|
|
|
}
|
2024-03-24 13:34:46 -04:00
|
|
|
|
2024-03-25 21:26:26 -04:00
|
|
|
// Current line.
|
2024-03-25 21:14:25 -04:00
|
|
|
context.push(ContextLine {
|
|
|
|
line,
|
|
|
|
number: current_line_number,
|
|
|
|
important: true,
|
|
|
|
});
|
|
|
|
|
2024-03-25 21:26:26 -04:00
|
|
|
// Next lines.
|
2024-03-25 21:14:25 -04:00
|
|
|
for ind in 0..CONTEXT {
|
|
|
|
let mut next_line = String::with_capacity(256);
|
|
|
|
let Ok(n) = read_line(&mut next_line) else {
|
2024-03-25 21:26:26 -04:00
|
|
|
// If an error occurs, just ignore the next lines.
|
2024-03-25 21:14:25 -04:00
|
|
|
break;
|
|
|
|
};
|
|
|
|
|
2024-03-25 21:26:26 -04:00
|
|
|
// Reached the end of the file.
|
2024-03-25 21:14:25 -04:00
|
|
|
if n == 0 {
|
|
|
|
break;
|
2024-03-24 13:34:46 -04:00
|
|
|
}
|
2024-03-25 21:14:25 -04:00
|
|
|
|
|
|
|
context.push(ContextLine {
|
|
|
|
line: next_line,
|
|
|
|
number: current_line_number + 1 + ind,
|
|
|
|
important: false,
|
|
|
|
});
|
2024-03-24 13:34:46 -04:00
|
|
|
}
|
2024-03-25 21:14:25 -04:00
|
|
|
|
|
|
|
return State::Pending(context);
|
2024-03-24 13:34:46 -04:00
|
|
|
}
|
2019-11-11 07:38:24 -05:00
|
|
|
|
2024-03-25 21:14:25 -04:00
|
|
|
current_line_number += 1;
|
2024-03-25 21:26:26 -04:00
|
|
|
// Add the current line as a previous line and shift the older lines by one.
|
2024-03-25 21:14:25 -04:00
|
|
|
for prev_line in &mut prev_lines {
|
|
|
|
mem::swap(&mut line, prev_line);
|
|
|
|
}
|
2024-03-25 21:26:26 -04:00
|
|
|
// The current line now contains the oldest previous line.
|
|
|
|
// Recycle it for reading the next line.
|
2024-03-25 21:14:25 -04:00
|
|
|
line.clear();
|
|
|
|
}
|
2019-11-11 07:38:24 -05:00
|
|
|
}
|
2020-12-12 13:45:37 -05:00
|
|
|
|
|
|
|
// Check that the exercise looks to be solved using self.state()
|
|
|
|
// This is not the best way to check since
|
2021-03-12 09:26:57 -05:00
|
|
|
// the user can just remove the "I AM NOT DONE" string from the file
|
2020-12-12 13:45:37 -05:00
|
|
|
// without actually having solved anything.
|
|
|
|
// The only other way to truly check this would to compile and run
|
|
|
|
// the exercise; which would be both costly and counterintuitive
|
|
|
|
pub fn looks_done(&self) -> bool {
|
|
|
|
self.state() == State::Done
|
|
|
|
}
|
2019-04-11 16:41:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Exercise {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
2024-03-31 10:55:33 -04:00
|
|
|
self.path.fmt(f)
|
2019-04-11 16:41:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 17:48:57 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
2023-09-27 16:02:14 -04:00
|
|
|
|
2019-11-11 07:38:24 -05:00
|
|
|
#[test]
|
|
|
|
fn test_pending_state() {
|
|
|
|
let exercise = Exercise {
|
2019-11-11 11:28:19 -05:00
|
|
|
name: "pending_exercise".into(),
|
2024-03-30 15:48:30 -04:00
|
|
|
path: PathBuf::from("tests/fixture/state/exercises/pending_exercise.rs"),
|
2019-11-11 07:38:24 -05:00
|
|
|
mode: Mode::Compile,
|
2019-11-11 11:28:19 -05:00
|
|
|
hint: String::new(),
|
2019-11-11 07:38:24 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
let state = exercise.state();
|
|
|
|
let expected = vec![
|
|
|
|
ContextLine {
|
|
|
|
line: "// fake_exercise".to_string(),
|
|
|
|
number: 1,
|
|
|
|
important: false,
|
|
|
|
},
|
|
|
|
ContextLine {
|
|
|
|
line: "".to_string(),
|
|
|
|
number: 2,
|
|
|
|
important: false,
|
|
|
|
},
|
|
|
|
ContextLine {
|
|
|
|
line: "// I AM NOT DONE".to_string(),
|
|
|
|
number: 3,
|
|
|
|
important: true,
|
|
|
|
},
|
|
|
|
ContextLine {
|
|
|
|
line: "".to_string(),
|
|
|
|
number: 4,
|
|
|
|
important: false,
|
|
|
|
},
|
|
|
|
ContextLine {
|
|
|
|
line: "fn main() {".to_string(),
|
|
|
|
number: 5,
|
|
|
|
important: false,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
assert_eq!(state, State::Pending(expected));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_finished_exercise() {
|
|
|
|
let exercise = Exercise {
|
2019-11-11 11:28:19 -05:00
|
|
|
name: "finished_exercise".into(),
|
2024-03-30 15:48:30 -04:00
|
|
|
path: PathBuf::from("tests/fixture/state/exercises/finished_exercise.rs"),
|
2019-11-11 07:38:24 -05:00
|
|
|
mode: Mode::Compile,
|
2019-11-11 11:28:19 -05:00
|
|
|
hint: String::new(),
|
2019-11-11 07:38:24 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(exercise.state(), State::Done);
|
|
|
|
}
|
2020-06-04 10:31:17 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_exercise_with_output() {
|
|
|
|
let exercise = Exercise {
|
2020-09-27 15:57:51 -04:00
|
|
|
name: "exercise_with_output".into(),
|
2024-03-30 15:48:30 -04:00
|
|
|
path: PathBuf::from("tests/fixture/success/exercises/testSuccess.rs"),
|
2020-06-04 10:31:17 -04:00
|
|
|
mode: Mode::Test,
|
|
|
|
hint: String::new(),
|
|
|
|
};
|
2024-03-31 10:55:33 -04:00
|
|
|
let out = exercise.run().unwrap();
|
|
|
|
assert_eq!(out.stdout, b"THIS TEST TOO SHALL PASS");
|
2020-06-04 10:31:17 -04:00
|
|
|
}
|
2024-03-24 14:18:19 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_not_done() {
|
2024-03-25 21:14:25 -04:00
|
|
|
assert!(contains_not_done_comment("// I AM NOT DONE"));
|
|
|
|
assert!(contains_not_done_comment("/// I AM NOT DONE"));
|
|
|
|
assert!(contains_not_done_comment("// I AM NOT DONE"));
|
|
|
|
assert!(contains_not_done_comment("/// I AM NOT DONE"));
|
|
|
|
assert!(contains_not_done_comment("// I AM NOT DONE "));
|
|
|
|
assert!(contains_not_done_comment("// I AM NOT DONE!"));
|
|
|
|
assert!(contains_not_done_comment("// I am not done"));
|
|
|
|
assert!(contains_not_done_comment("// i am NOT done"));
|
|
|
|
|
|
|
|
assert!(!contains_not_done_comment("I AM NOT DONE"));
|
|
|
|
assert!(!contains_not_done_comment("// NOT DONE"));
|
|
|
|
assert!(!contains_not_done_comment("DONE"));
|
2024-03-24 14:18:19 -04:00
|
|
|
}
|
2019-04-11 16:41:24 -04:00
|
|
|
}
|