2024-09-19 19:12:41 +01:00
|
|
|
//
|
|
|
|
use std::path::Path;
|
|
|
|
|
2024-09-20 14:28:23 +01:00
|
|
|
use crate::model::{Config, Line, Marker, Markers};
|
2024-09-19 19:12:41 +01:00
|
|
|
use anyhow::Result;
|
2024-09-19 20:18:53 +01:00
|
|
|
use ignore::Walk;
|
2024-09-19 19:12:41 +01:00
|
|
|
|
2024-09-20 14:28:23 +01:00
|
|
|
pub fn find_markers(config: &Config) -> Result<Markers, anyhow::Error> {
|
|
|
|
let mut markers = Markers::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)? {
|
2024-09-20 07:23:36 +01:00
|
|
|
scan_file(path, config, &mut markers)?;
|
2024-09-19 19:12:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(markers)
|
|
|
|
}
|
|
|
|
|
2024-09-20 14:28:23 +01:00
|
|
|
fn scan_file(file: &Path, config: &Config, found_markers: &mut Markers) -> Result<()> {
|
2024-09-19 19:12:41 +01:00
|
|
|
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(())
|
|
|
|
}
|