|
| 1 | +use crate::location::*; |
| 2 | +use crate::reporter::Reporter; |
1 | 3 | use crate::ruleset::{Rule, Ruleset}; |
2 | 4 | use glob; |
3 | 5 | use std::fs; |
4 | 6 | use std::path::Path; |
5 | 7 | use walkdir::WalkDir; |
6 | 8 |
|
7 | | -fn apply_rule_to_path(rule: &Rule, path: &Path) { |
8 | | - let text = fs::read_to_string(path).unwrap(); |
| 9 | +fn apply_rule_to_path(loc: &FileMatchLocation, rule: &Rule, reporter: &mut Reporter) { |
| 10 | + let text = fs::read_to_string(loc.file).unwrap(); |
9 | 11 |
|
10 | 12 | for (nline, line) in text.lines().enumerate() { |
11 | 13 | if rule.regex.is_match(line) { |
12 | | - println!("{}:{}: {}", path.display(), nline + 1, rule.title); |
| 14 | + reporter.report( |
| 15 | + &MatchLocation::Line(LineMatchLocation::from_file(loc, nline + 1)), |
| 16 | + &rule.title, |
| 17 | + ); |
13 | 18 | } |
14 | 19 | } |
15 | 20 | } |
16 | 21 |
|
17 | | -fn apply_rule_to_target(rule: &Rule, target: &Path) { |
| 22 | +fn apply_rule_to_target(loc: &RootMatchLocation, rule: &Rule, reporter: &mut Reporter) { |
18 | 23 | let mut match_options = glob::MatchOptions::new(); |
19 | 24 | match_options.require_literal_separator = true; |
20 | 25 |
|
21 | | - for path in WalkDir::new(target) |
| 26 | + for path in WalkDir::new(loc.root) |
22 | 27 | .into_iter() |
23 | 28 | .filter_map(|e| e.ok()) |
24 | 29 | .filter(|e| e.file_type().is_file()) |
25 | 30 | .map(|e| e.into_path()) |
26 | 31 | { |
27 | | - let path = path.strip_prefix(&target).unwrap(); |
| 32 | + let path = path.strip_prefix(&loc.root).unwrap(); |
28 | 33 | if rule.glob.matches_path_with(path, match_options) { |
29 | | - apply_rule_to_path(rule, path); |
| 34 | + apply_rule_to_path(&FileMatchLocation::from_root(loc, path), rule, reporter); |
30 | 35 | } |
31 | 36 | } |
32 | 37 | } |
33 | 38 |
|
34 | | -pub fn apply_ruleset_to_target(ruleset: &Ruleset, target: &Path) { |
| 39 | +pub fn apply_ruleset_to_target(ruleset: &Ruleset, target: &Path, reporter: &mut Reporter) { |
| 40 | + let loc = &RootMatchLocation { root: target }; |
35 | 41 | for rule in &ruleset.rules { |
36 | | - apply_rule_to_target(&rule, target); |
| 42 | + apply_rule_to_target(&loc, &rule, reporter); |
37 | 43 | } |
38 | 44 | } |
0 commit comments