// use std::{collections::HashSet, path::Path}; use crate::{ issues::Issue, model::{Config, Line, Marker, Markers}, }; use anyhow::Result; use ignore::Walk; pub fn find_markers(config: &Config, issues: HashSet) -> Result { let mut markers = Markers::default(); for file in Walk::new(config.fs().base()).flatten() { let path = file.path(); if config.fs().path_is_file(path)? { scan_file(path, config, &mut markers, &issues)?; } } Ok(markers) } fn scan_file( file: &Path, config: &Config, found_markers: &mut Markers, issues: &HashSet, ) -> 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 + 1) // line numbers are not 0-based, but enumerate is .value(line.to_owned()) .build() }) .filter_map(|line| line.into_marker().ok()) .filter(|marker| !matches!(marker, Marker::Unmarked)) .map(|marker| has_open_issue(marker, issues)) .for_each(|marker| found_markers.add_marker(marker)); Ok(()) } fn has_open_issue(marker: Marker, issues: &HashSet) -> Marker { if let Marker::Valid(_, ref issue) = marker { let has_open_issue = issues.contains(issue); if has_open_issue { marker } else { marker.into_closed() } } else { marker } }