Skip to content

Commit

Permalink
support scanning todo!() Rust macros
Browse files Browse the repository at this point in the history
  • Loading branch information
stancl committed Dec 11, 2023
1 parent a9a6a66 commit b4a74cb
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
7 changes: 7 additions & 0 deletions samples/2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn foo() {
// Rust TODOs can only be generic
todo!("generic");
todo!();
todo!("@foo not category");
todo!("00 not priority");
}
63 changes: 63 additions & 0 deletions src/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ fn parse_priority(word: &str) -> Option<isize> {
}
}

/// Remove closing tags, comments, and whitespace
fn clean_line<'a>(line: &'a str, delimiter_word: &str) -> &'a str {
return line.split_once(delimiter_word).unwrap().1
.trim()
Expand Down Expand Up @@ -154,6 +155,19 @@ pub fn scan_string(str: String, filename: PathBuf, entries: &mut Vec<Entry>) {

let text = clean_line(line, word);

if word.starts_with("todo!(") {
entries.push(Entry {
text: line.trim().to_string(),
location: Location {
file: filename.clone(),
line: line_num + 1,
},
data: EntryData::Generic,
});

break;
}

// Handles: `todo`, `TODO`, `todo:`, `TODO:`
// Also trims `"` and `'` to handle cases like `foo="bar todo"`
if word.to_lowercase().trim_end_matches(':').trim_end_matches('"').trim_end_matches('\'') == "todo" {
Expand Down Expand Up @@ -773,6 +787,55 @@ mod tests {
}, entries[9]);
}

#[test]
fn sample_test_rs() {
let mut entries: Vec<Entry> = vec![];

let mut path = std::env::current_dir().unwrap();
path.push("samples");
path.push("2.rs");

scan_file(path.as_path(), &mut entries).unwrap();

assert_eq!(4, entries.len());

assert_eq!(Entry {
data: EntryData::Generic,
text: String::from("todo!(\"generic\");"),
location: Location {
file: path.clone(),
line: 3,
}
}, entries[0]);

assert_eq!(Entry {
data: EntryData::Generic,
text: String::from("todo!();"),
location: Location {
file: path.clone(),
line: 4,
}
}, entries[1]);

assert_eq!(Entry {
data: EntryData::Generic,
text: String::from("todo!(\"@foo not category\");"),
location: Location {
file: path.clone(),
line: 5,
}
}, entries[2]);

assert_eq!(Entry {
data: EntryData::Generic,
text: String::from("todo!(\"00 not priority\");"),
location: Location {
file: path.clone(),
line: 6,
}
}, entries[3]);
}

#[test]
fn todo_file_test() {
let mut entries: Vec<Entry> = vec![];
Expand Down

0 comments on commit b4a74cb

Please sign in to comment.