Skip to content

Commit c0c1129

Browse files
committed
Replace regex with winnow
1 parent f205ee3 commit c0c1129

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ glob = "0.3.0"
1515
home = "0.5.9"
1616
indicatif = "0.17.8"
1717
notify-debouncer-mini = "0.4.1"
18-
regex = "1.10.3"
1918
serde_json = "1.0.114"
2019
serde = { version = "1.0.197", features = ["derive"] }
2120
toml = "0.8.10"
21+
winnow = "0.6.5"
2222

2323
[[bin]]
2424
name = "rustlings"

src/exercise.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,38 @@
1-
use regex::Regex;
21
use serde::Deserialize;
32
use std::fmt::{self, Display, Formatter};
43
use std::fs::{self, remove_file, File};
54
use std::io::{self, BufRead, BufReader};
65
use std::path::PathBuf;
76
use std::process::{self, Command};
87
use std::{array, env, mem};
8+
use winnow::ascii::{space0, space1};
9+
use winnow::combinator::opt;
10+
use winnow::Parser;
911

1012
const RUSTC_COLOR_ARGS: &[&str] = &["--color", "always"];
1113
const RUSTC_EDITION_ARGS: &[&str] = &["--edition", "2021"];
1214
const RUSTC_NO_DEBUG_ARGS: &[&str] = &["-C", "strip=debuginfo"];
13-
const I_AM_DONE_REGEX: &str = r"^\s*///?\s*I\s+AM\s+NOT\s+DONE";
1415
const CONTEXT: usize = 2;
1516
const CLIPPY_CARGO_TOML_PATH: &str = "./exercises/22_clippy/Cargo.toml";
1617

18+
fn not_done(input: &str) -> bool {
19+
(
20+
space0::<_, ()>,
21+
"//",
22+
opt('/'),
23+
space0,
24+
'I',
25+
space1,
26+
"AM",
27+
space1,
28+
"NOT",
29+
space1,
30+
"DONE",
31+
)
32+
.parse_next(&mut &*input)
33+
.is_ok()
34+
}
35+
1736
// Get a temporary file name that is hopefully unique
1837
#[inline]
1938
fn temp_file() -> String {
@@ -223,7 +242,6 @@ path = "{}.rs""#,
223242
Ok(n)
224243
};
225244

226-
let re = Regex::new(I_AM_DONE_REGEX).unwrap();
227245
let mut matched_line_ind: usize = 0;
228246
let mut prev_lines: [_; CONTEXT] = array::from_fn(|_| String::with_capacity(256));
229247
let mut line = String::with_capacity(256);
@@ -232,7 +250,7 @@ path = "{}.rs""#,
232250
match read_line(&mut line) {
233251
Ok(0) => break,
234252
Ok(_) => {
235-
if re.is_match(&line) {
253+
if not_done(&line) {
236254
let mut context = Vec::with_capacity(2 * CONTEXT + 1);
237255
for (ind, prev_line) in prev_lines
238256
.into_iter()
@@ -413,4 +431,22 @@ mod test {
413431
let out = exercise.compile().unwrap().run().unwrap();
414432
assert!(out.stdout.contains("THIS TEST TOO SHALL PASS"));
415433
}
434+
435+
#[test]
436+
fn test_not_done() {
437+
assert!(not_done("// I AM NOT DONE"));
438+
assert!(not_done("/// I AM NOT DONE"));
439+
assert!(not_done("// I AM NOT DONE"));
440+
assert!(not_done("/// I AM NOT DONE"));
441+
assert!(not_done("// I AM NOT DONE"));
442+
assert!(not_done("// I AM NOT DONE"));
443+
assert!(not_done("// I AM NOT DONE"));
444+
assert!(not_done("// I AM NOT DONE "));
445+
assert!(not_done("// I AM NOT DONE!"));
446+
447+
assert!(!not_done("I AM NOT DONE"));
448+
assert!(!not_done("// NOT DONE"));
449+
assert!(!not_done("DONE"));
450+
assert!(!not_done("// i am not done"));
451+
}
416452
}

0 commit comments

Comments
 (0)