From 6665e369bfb8f5979c599f33c11084877de20559 Mon Sep 17 00:00:00 2001 From: Zykino <3809938+Zykino@users.noreply.github.com> Date: Thu, 30 Jul 2026 00:24:42 +0200 Subject: [PATCH] Improve some readability (and prevent some duplicate search within lines) --- Cargo.toml | 2 +- src/scan.rs | 40 +++++++++++++++++++++++++--------------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 09aac69..b8b2f30 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "todos" version = "0.1.1" -edition = "2021" +edition = "2024" authors = ["Samuel Ć tancl "] description = "An intuitive system for organizing TODOs in code" diff --git a/src/scan.rs b/src/scan.rs index 0280771..4dda5f3 100644 --- a/src/scan.rs +++ b/src/scan.rs @@ -161,14 +161,13 @@ pub fn add_excludes_from_gitignore(base_dir: &PathBuf, excludes: &mut Vec) { for (line_num, line) in str.lines().enumerate() { - if ! line.to_lowercase().contains("todo") { - continue; - } - - for mut word in line.split_whitespace() { - if ! word.to_lowercase().starts_with("todo") { - continue; - } + if let Some(i) = line.to_lowercase().find("todo") + && line[i - 1..i + 4].trim_start().len() == 4 + { + let mut word = line[i..] + .split_whitespace() + .next() + .expect("Prior condition should enforce we have a word starting at this position"); let text = clean_line(line, word); @@ -182,7 +181,7 @@ pub fn scan_string(str: String, filename: PathBuf, entries: &mut Vec) { data: EntryData::Generic, }); - break; + continue; } word = word.trim_end_matches(':'); @@ -199,12 +198,10 @@ pub fn scan_string(str: String, filename: PathBuf, entries: &mut Vec) { data: EntryData::Generic, }); - break; + continue; } - if word.contains('@') { - let category = word.split('@').nth(1).unwrap(); - + if let Some((_todo, category)) = word.split_once('@') { entries.push(Entry { text: text.to_string(), location: Location { @@ -214,7 +211,7 @@ pub fn scan_string(str: String, filename: PathBuf, entries: &mut Vec) { data: EntryData::Category(category.to_string()), }); - break; + continue; } if word.chars().any(|ch| PRIORITY_CHARS.contains(&ch)) { @@ -229,7 +226,7 @@ pub fn scan_string(str: String, filename: PathBuf, entries: &mut Vec) { }); } - break; + continue; } } } @@ -542,6 +539,7 @@ mod tests { /* TODO@baz3 */ // TODO@baz3 b + // TODO@ "#; let mut entries: Vec = vec![]; @@ -614,6 +612,18 @@ mod tests { line: 12, } }, entries[6]); + + assert_eq!( + Entry { + data: EntryData::Category(String::from("")), + text: String::from(""), + location: Location { + file: path.clone(), + line: 13, + } + }, + entries[7] + ); } #[test]