mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-25 23:04:17 -05:00
Optimize state
This commit is contained in:
parent
01b7d6334c
commit
0aeaccc3a5
1 changed files with 75 additions and 37 deletions
106
src/exercise.rs
106
src/exercise.rs
|
@ -1,16 +1,16 @@
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::env;
|
|
||||||
use std::fmt::{self, Display, Formatter};
|
use std::fmt::{self, Display, Formatter};
|
||||||
use std::fs::{self, remove_file, File};
|
use std::fs::{self, remove_file, File};
|
||||||
use std::io::Read;
|
use std::io::{self, BufRead, BufReader};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::process::{self, Command};
|
use std::process::{self, Command};
|
||||||
|
use std::{array, env, mem};
|
||||||
|
|
||||||
const RUSTC_COLOR_ARGS: &[&str] = &["--color", "always"];
|
const RUSTC_COLOR_ARGS: &[&str] = &["--color", "always"];
|
||||||
const RUSTC_EDITION_ARGS: &[&str] = &["--edition", "2021"];
|
const RUSTC_EDITION_ARGS: &[&str] = &["--edition", "2021"];
|
||||||
const RUSTC_NO_DEBUG_ARGS: &[&str] = &["-C", "strip=debuginfo"];
|
const RUSTC_NO_DEBUG_ARGS: &[&str] = &["-C", "strip=debuginfo"];
|
||||||
const I_AM_DONE_REGEX: &str = r"(?m)^\s*///?\s*I\s+AM\s+NOT\s+DONE";
|
const I_AM_DONE_REGEX: &str = r"^\s*///?\s*I\s+AM\s+NOT\s+DONE";
|
||||||
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";
|
||||||
|
|
||||||
|
@ -205,51 +205,89 @@ path = "{}.rs""#,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn state(&self) -> State {
|
pub fn state(&self) -> State {
|
||||||
let mut source_file = File::open(&self.path).unwrap_or_else(|e| {
|
let source_file = File::open(&self.path).unwrap_or_else(|e| {
|
||||||
panic!(
|
panic!(
|
||||||
"We were unable to open the exercise file {}! {e}",
|
"We were unable to open the exercise file {}! {e}",
|
||||||
self.path.display()
|
self.path.display()
|
||||||
)
|
)
|
||||||
});
|
});
|
||||||
|
let mut source_reader = BufReader::new(source_file);
|
||||||
let source = {
|
let mut read_line = |buf: &mut String| -> io::Result<_> {
|
||||||
let mut s = String::new();
|
let n = source_reader.read_line(buf)?;
|
||||||
source_file.read_to_string(&mut s).unwrap_or_else(|e| {
|
if buf.ends_with('\n') {
|
||||||
panic!(
|
buf.pop();
|
||||||
"We were unable to read the exercise file {}! {e}",
|
if buf.ends_with('\r') {
|
||||||
self.path.display()
|
buf.pop();
|
||||||
)
|
}
|
||||||
});
|
}
|
||||||
s
|
Ok(n)
|
||||||
};
|
};
|
||||||
|
|
||||||
let re = Regex::new(I_AM_DONE_REGEX).unwrap();
|
let re = Regex::new(I_AM_DONE_REGEX).unwrap();
|
||||||
|
let mut matched_line_ind: usize = 0;
|
||||||
|
let mut prev_lines: [_; CONTEXT] = array::from_fn(|_| String::with_capacity(256));
|
||||||
|
let mut line = String::with_capacity(256);
|
||||||
|
|
||||||
if !re.is_match(&source) {
|
loop {
|
||||||
return State::Done;
|
match read_line(&mut line) {
|
||||||
|
Ok(0) => break,
|
||||||
|
Ok(_) => {
|
||||||
|
if re.is_match(&line) {
|
||||||
|
let mut context = Vec::with_capacity(2 * CONTEXT + 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,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let matched_line_index = source
|
context.push(ContextLine {
|
||||||
.lines()
|
line,
|
||||||
.enumerate()
|
number: matched_line_ind + 1,
|
||||||
.find_map(|(i, line)| if re.is_match(line) { Some(i) } else { None })
|
important: true,
|
||||||
.expect("This should not happen at all");
|
});
|
||||||
|
|
||||||
let min_line = ((matched_line_index as i32) - (CONTEXT as i32)).max(0) as usize;
|
for ind in 0..CONTEXT {
|
||||||
let max_line = matched_line_index + CONTEXT;
|
let mut next_line = String::with_capacity(256);
|
||||||
|
let Ok(n) = read_line(&mut next_line) else {
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
|
||||||
let context = source
|
if n == 0 {
|
||||||
.lines()
|
break;
|
||||||
.enumerate()
|
}
|
||||||
.filter(|&(i, _)| i >= min_line && i <= max_line)
|
|
||||||
.map(|(i, line)| ContextLine {
|
|
||||||
line: line.to_string(),
|
|
||||||
number: i + 1,
|
|
||||||
important: i == matched_line_index,
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
State::Pending(context)
|
context.push(ContextLine {
|
||||||
|
line: next_line,
|
||||||
|
number: matched_line_ind + ind + 2,
|
||||||
|
important: false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return State::Pending(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
matched_line_ind += 1;
|
||||||
|
for prev_line in &mut prev_lines {
|
||||||
|
mem::swap(&mut line, prev_line);
|
||||||
|
}
|
||||||
|
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()
|
||||||
|
|
Loading…
Reference in a new issue