Fix context of previous lines and improve readability

This commit is contained in:
mo8it 2024-03-26 02:14:25 +01:00
parent bdf826a026
commit 7a6f71f090

View file

@ -3,7 +3,7 @@ use std::fmt::{self, Display, Formatter};
use std::fs::{self, remove_file, File}; use std::fs::{self, remove_file, File};
use std::io::{self, BufRead, BufReader}; use std::io::{self, BufRead, BufReader};
use std::path::PathBuf; use std::path::PathBuf;
use std::process::{self, Command}; use std::process::{self, exit, Command};
use std::{array, env, mem}; use std::{array, env, mem};
use winnow::ascii::{space0, Caseless}; use winnow::ascii::{space0, Caseless};
use winnow::combinator::opt; use winnow::combinator::opt;
@ -15,7 +15,8 @@ const RUSTC_NO_DEBUG_ARGS: &[&str] = &["-C", "strip=debuginfo"];
const CONTEXT: usize = 2; const CONTEXT: usize = 2;
const CLIPPY_CARGO_TOML_PATH: &str = "./exercises/22_clippy/Cargo.toml"; const CLIPPY_CARGO_TOML_PATH: &str = "./exercises/22_clippy/Cargo.toml";
fn not_done(input: &str) -> bool { // Checks if the line contains the "I AM NOT DONE" comment.
fn contains_not_done_comment(input: &str) -> bool {
( (
space0::<_, ()>, space0::<_, ()>,
"//", "//",
@ -219,12 +220,15 @@ path = "{}.rs""#,
pub fn state(&self) -> State { pub fn state(&self) -> State {
let source_file = File::open(&self.path).unwrap_or_else(|e| { let source_file = File::open(&self.path).unwrap_or_else(|e| {
panic!( println!(
"We were unable to open the exercise file {}! {e}", "Failed to open the exercise file {}: {e}",
self.path.display() self.path.display(),
) );
exit(1);
}); });
let mut source_reader = BufReader::new(source_file); let mut source_reader = BufReader::new(source_file);
// Read the next line into `buf` without the newline at the end.
let mut read_line = |buf: &mut String| -> io::Result<_> { let mut read_line = |buf: &mut String| -> io::Result<_> {
let n = source_reader.read_line(buf)?; let n = source_reader.read_line(buf)?;
if buf.ends_with('\n') { if buf.ends_with('\n') {
@ -236,33 +240,42 @@ path = "{}.rs""#,
Ok(n) Ok(n)
}; };
let mut matched_line_ind: usize = 0; let mut current_line_number: usize = 1;
let mut prev_lines: [_; CONTEXT] = array::from_fn(|_| String::with_capacity(256)); let mut prev_lines: [_; CONTEXT] = array::from_fn(|_| String::with_capacity(256));
let mut line = String::with_capacity(256); let mut line = String::with_capacity(256);
loop { loop {
match read_line(&mut line) { let n = read_line(&mut line).unwrap_or_else(|e| {
Ok(0) => break, println!(
Ok(_) => { "Failed to read the exercise file {}: {e}",
if not_done(&line) { 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); let mut context = Vec::with_capacity(2 * CONTEXT + 1);
for (ind, prev_line) in prev_lines for (ind, prev_line) in prev_lines
.into_iter() .into_iter()
.rev() .take(current_line_number - 1)
.take(matched_line_ind)
.enumerate() .enumerate()
.rev()
{ {
context.push(ContextLine { context.push(ContextLine {
line: prev_line, line: prev_line,
// TODO number: current_line_number - 1 - ind,
number: matched_line_ind - CONTEXT + ind + 1,
important: false, important: false,
}); });
} }
context.push(ContextLine { context.push(ContextLine {
line, line,
number: matched_line_ind + 1, number: current_line_number,
important: true, important: true,
}); });
@ -278,7 +291,7 @@ path = "{}.rs""#,
context.push(ContextLine { context.push(ContextLine {
line: next_line, line: next_line,
number: matched_line_ind + ind + 2, number: current_line_number + 1 + ind,
important: false, important: false,
}); });
} }
@ -286,20 +299,13 @@ path = "{}.rs""#,
return State::Pending(context); return State::Pending(context);
} }
matched_line_ind += 1; current_line_number += 1;
// Recycle the buffers.
for prev_line in &mut prev_lines { for prev_line in &mut prev_lines {
mem::swap(&mut line, prev_line); mem::swap(&mut line, prev_line);
} }
line.clear(); line.clear();
} }
Err(e) => panic!(
"We were unable to read the exercise file {}! {e}",
self.path.display()
),
}
}
State::Done
} }
// Check that the exercise looks to be solved using self.state() // Check that the exercise looks to be solved using self.state()
@ -428,17 +434,17 @@ mod test {
#[test] #[test]
fn test_not_done() { fn test_not_done() {
assert!(not_done("// I AM NOT DONE")); assert!(contains_not_done_comment("// I AM NOT DONE"));
assert!(not_done("/// I AM NOT DONE")); assert!(contains_not_done_comment("/// I AM NOT DONE"));
assert!(not_done("// I AM NOT DONE")); assert!(contains_not_done_comment("// I AM NOT DONE"));
assert!(not_done("/// I AM NOT DONE")); assert!(contains_not_done_comment("/// I AM NOT DONE"));
assert!(not_done("// I AM NOT DONE ")); assert!(contains_not_done_comment("// I AM NOT DONE "));
assert!(not_done("// I AM NOT DONE!")); assert!(contains_not_done_comment("// I AM NOT DONE!"));
assert!(not_done("// I am not done")); assert!(contains_not_done_comment("// I am not done"));
assert!(not_done("// i am NOT done")); assert!(contains_not_done_comment("// i am NOT done"));
assert!(!not_done("I AM NOT DONE")); assert!(!contains_not_done_comment("I AM NOT DONE"));
assert!(!not_done("// NOT DONE")); assert!(!contains_not_done_comment("// NOT DONE"));
assert!(!not_done("DONE")); assert!(!contains_not_done_comment("DONE"));
} }
} }