forgejo-todo-checker/src/scanner.rs

40 lines
1.2 KiB
Rust
Raw Normal View History

2024-09-19 19:12:41 +01:00
//
use std::path::Path;
use crate::model::{Config, FoundMarkers, Line, Marker};
use anyhow::Result;
2024-09-19 20:18:53 +01:00
use ignore::Walk;
2024-09-19 19:12:41 +01:00
pub fn find_markers(config: Config) -> Result<FoundMarkers, anyhow::Error> {
let mut markers = FoundMarkers::default();
2024-09-19 20:18:53 +01:00
for file in Walk::new(config.fs().base()).flatten() {
2024-09-19 19:12:41 +01:00
let path = file.path();
if config.fs().path_is_file(path)? {
scan_file(path, &config, &mut markers)?;
}
}
Ok(markers)
}
fn scan_file(file: &Path, config: &Config, found_markers: &mut FoundMarkers) -> Result<()> {
let relative_path = file.strip_prefix(config.fs().base())?.to_path_buf();
config
.fs()
.file_read_to_string(file)?
.lines()
.enumerate()
.map(|(n, line)| {
Line::builder()
.file(file.to_path_buf())
.relative_path(relative_path.clone())
.num(n)
.value(line.to_owned())
.build()
})
.filter_map(|line| line.into_marker().ok())
.filter(|marker| !matches!(marker, Marker::Unmarked))
.for_each(|marker| found_markers.add_marker(marker));
Ok(())
}