Compare commits

..

No commits in common. "078f6ffc1cf18546079d03bee99f0903c9e14703" and "bdf826a026cfe7f89c31433cfd2b9a32cbe66d2c" have entirely different histories.

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, exit, Command}; use std::process::{self, 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,8 +15,7 @@ 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";
// Checks if the line contains the "I AM NOT DONE" comment. fn not_done(input: &str) -> bool {
fn contains_not_done_comment(input: &str) -> bool {
( (
space0::<_, ()>, space0::<_, ()>,
"//", "//",
@ -220,15 +219,12 @@ 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| {
println!( panic!(
"Failed to open the exercise file {}: {e}", "We were unable 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') {
@ -240,80 +236,70 @@ path = "{}.rs""#,
Ok(n) Ok(n)
}; };
let mut current_line_number: usize = 1; let mut matched_line_ind: usize = 0;
// Keep the last `CONTEXT` lines while iterating over the file lines.
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 {
let n = read_line(&mut line).unwrap_or_else(|e| { match read_line(&mut line) {
println!( Ok(0) => break,
"Failed to read the exercise file {}: {e}", Ok(_) => {
self.path.display(), if not_done(&line) {
); let mut context = Vec::with_capacity(2 * CONTEXT + 1);
exit(1); for (ind, prev_line) in prev_lines
}); .into_iter()
.rev()
.take(matched_line_ind)
.enumerate()
{
context.push(ContextLine {
line: prev_line,
// TODO
number: matched_line_ind - CONTEXT + ind + 1,
important: false,
});
}
// Reached the end of the file and didn't find the comment. context.push(ContextLine {
if n == 0 { line,
return State::Done; number: matched_line_ind + 1,
} important: true,
});
if contains_not_done_comment(&line) { for ind in 0..CONTEXT {
let mut context = Vec::with_capacity(2 * CONTEXT + 1); let mut next_line = String::with_capacity(256);
// Previous lines. let Ok(n) = read_line(&mut next_line) else {
for (ind, prev_line) in prev_lines break;
.into_iter() };
.take(current_line_number - 1)
.enumerate()
.rev()
{
context.push(ContextLine {
line: prev_line,
number: current_line_number - 1 - ind,
important: false,
});
}
// Current line. if n == 0 {
context.push(ContextLine { break;
line, }
number: current_line_number,
important: true,
});
// Next lines. context.push(ContextLine {
for ind in 0..CONTEXT { line: next_line,
let mut next_line = String::with_capacity(256); number: matched_line_ind + ind + 2,
let Ok(n) = read_line(&mut next_line) else { important: false,
// If an error occurs, just ignore the next lines. });
break; }
};
// Reached the end of the file. return State::Pending(context);
if n == 0 {
break;
} }
context.push(ContextLine { matched_line_ind += 1;
line: next_line, for prev_line in &mut prev_lines {
number: current_line_number + 1 + ind, mem::swap(&mut line, prev_line);
important: false, }
}); line.clear();
} }
Err(e) => panic!(
return State::Pending(context); "We were unable to read the exercise file {}! {e}",
self.path.display()
),
} }
current_line_number += 1;
// Add the current line as a previous line and shift the older lines by one.
for prev_line in &mut prev_lines {
mem::swap(&mut line, prev_line);
}
// The current line now contains the oldest previous line.
// Recycle it for reading the next line.
line.clear();
} }
State::Done
} }
// Check that the exercise looks to be solved using self.state() // Check that the exercise looks to be solved using self.state()
@ -442,17 +428,17 @@ mod test {
#[test] #[test]
fn test_not_done() { fn test_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("I AM NOT DONE"));
assert!(!contains_not_done_comment("// NOT DONE")); assert!(!not_done("// NOT DONE"));
assert!(!contains_not_done_comment("DONE")); assert!(!not_done("DONE"));
} }
} }