39 lines
1.1 KiB
Rust
39 lines
1.1 KiB
Rust
|
//
|
||
|
use std::path::Path;
|
||
|
|
||
|
use crate::model::{Config, FoundMarkers, Line, Marker};
|
||
|
use anyhow::Result;
|
||
|
|
||
|
pub fn find_markers(config: Config) -> Result<FoundMarkers, anyhow::Error> {
|
||
|
let mut markers = FoundMarkers::default();
|
||
|
for file in ignore::Walk::new(config.fs().base()).flatten() {
|
||
|
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(())
|
||
|
}
|