From b5bbe710ff79313dc575ee051328ae14b7fac978 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Thu, 23 Nov 2023 01:39:52 +0100 Subject: [PATCH] right-trim --> comments --- src/scan.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/src/scan.rs b/src/scan.rs index 34fb088..d7b20a4 100644 --- a/src/scan.rs +++ b/src/scan.rs @@ -40,6 +40,14 @@ fn parse_priority(word: &str) -> Option { } } +fn clean_line<'a>(line: &'a str, delimiter_word: &str) -> &'a str { + return line.split_once(delimiter_word).unwrap().1 + .trim() + .trim_end_matches("*/") + .trim_end_matches("-->") + .trim(); +} + pub fn scan_string(str: String, filename: PathBuf, entries: &mut Vec) { for (line_num, line) in str.lines().enumerate() { if ! line.to_lowercase().contains("todo") { @@ -51,7 +59,7 @@ pub fn scan_string(str: String, filename: PathBuf, entries: &mut Vec) { continue; } - let text = line.split_once(word).unwrap().1.trim().trim_end_matches("*/").trim(); + let text = clean_line(line, word); // Handles: `todo`, `TODO`, `todo:`, `TODO:` if word.to_lowercase().trim_end_matches(':') == "todo" { @@ -160,7 +168,7 @@ pub fn scan_todo_file(path: &Path, entries: &mut Vec) -> io::Result<()> { if word.to_lowercase().starts_with("todo") && word.chars().any(|ch| PRIORITY_CHARS.contains(&ch)) { if let Some(priority) = parse_priority(word) { entries.push(Entry { - text: line.split_once(word).unwrap().1.trim().trim_end_matches("*/").trim().to_string(), + text: clean_line(line, word).to_string(), location: Location { file: path.to_path_buf(), line: line_num + 1, @@ -233,7 +241,7 @@ pub fn scan_readme_file(path: &Path, entries: &mut Vec) -> io::Result<()> if word.to_lowercase().starts_with("todo") && word.chars().any(|ch| PRIORITY_CHARS.contains(&ch)) { if let Some(priority) = parse_priority(word) { entries.push(Entry { - text: line.split_once(word).unwrap().1.trim().trim_end_matches("*/").trim().to_string(), + text: clean_line(line, word).to_string(), location: Location { file: path.to_path_buf(), line: line_num + 1, @@ -272,6 +280,7 @@ fn generic_test() { * TODO baz TODO baz2 TODO baz2 todo + */ "#; @@ -281,7 +290,7 @@ fn generic_test() { scan_string(str.to_string(), path.clone(), &mut entries); - assert_eq!(5, entries.len()); + assert_eq!(6, entries.len()); assert_eq!(Entry { data: EntryData::Generic, @@ -327,6 +336,15 @@ fn generic_test() { line: 10, } }, entries[4]); + + assert_eq!(Entry { + data: EntryData::Generic, + text: String::from("foo2"), + location: Location { + file: path.clone(), + line: 11, + } + }, entries[5]); } #[test] @@ -342,6 +360,7 @@ fn category_test() { // TODO@baz2 a /* TODO@baz3 */ // TODO@baz3 b + "#; let mut entries: Vec = vec![]; @@ -350,7 +369,7 @@ fn category_test() { scan_string(str.to_string(), path.clone(), &mut entries); - assert_eq!(6, entries.len()); + assert_eq!(7, entries.len()); assert_eq!(Entry { data: EntryData::Category(String::from("foo")), @@ -405,6 +424,15 @@ fn category_test() { line: 11, } }, entries[5]); + + assert_eq!(Entry { + data: EntryData::Category(String::from("baz3")), + text: String::from(""), + location: Location { + file: path.clone(), + line: 12, + } + }, entries[6]); } #[test] @@ -423,6 +451,7 @@ fn priority_test() { // TODO1 a /* TODO2 */ // TODO3 b + "#; let mut entries: Vec = vec![]; @@ -431,7 +460,7 @@ fn priority_test() { scan_string(str.to_string(), path.clone(), &mut entries); - assert_eq!(9, entries.len()); + assert_eq!(10, entries.len()); assert_eq!(Entry { data: EntryData::Priority(-1), @@ -513,6 +542,15 @@ fn priority_test() { line: 14, } }, entries[8]); + + assert_eq!(Entry { + data: EntryData::Priority(4), + text: String::from("b"), + location: Location { + file: path.clone(), + line: 15, + } + }, entries[9]); } #[test]