-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement rule match reporter (fixes #4)
- Loading branch information
Showing
4 changed files
with
98 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,44 @@ | ||
use crate::location::*; | ||
use crate::reporter::Reporter; | ||
use crate::ruleset::{Rule, Ruleset}; | ||
use glob; | ||
use std::fs; | ||
use std::path::Path; | ||
use walkdir::WalkDir; | ||
|
||
fn apply_rule_to_path(rule: &Rule, path: &Path) { | ||
let text = fs::read_to_string(path).unwrap(); | ||
fn apply_rule_to_path(loc: &FileMatchLocation, rule: &Rule, reporter: &mut Reporter) { | ||
let text = fs::read_to_string(loc.file).unwrap(); | ||
|
||
for (nline, line) in text.lines().enumerate() { | ||
if rule.regex.is_match(line) { | ||
println!("{}:{}: {}", path.display(), nline + 1, rule.title); | ||
reporter.report( | ||
&MatchLocation::Line(LineMatchLocation::from_file(loc, nline + 1)), | ||
&rule.title, | ||
); | ||
} | ||
} | ||
} | ||
|
||
fn apply_rule_to_target(rule: &Rule, target: &Path) { | ||
fn apply_rule_to_target(loc: &RootMatchLocation, rule: &Rule, reporter: &mut Reporter) { | ||
let mut match_options = glob::MatchOptions::new(); | ||
match_options.require_literal_separator = true; | ||
|
||
for path in WalkDir::new(target) | ||
for path in WalkDir::new(loc.root) | ||
.into_iter() | ||
.filter_map(|e| e.ok()) | ||
.filter(|e| e.file_type().is_file()) | ||
.map(|e| e.into_path()) | ||
{ | ||
let path = path.strip_prefix(&target).unwrap(); | ||
let path = path.strip_prefix(&loc.root).unwrap(); | ||
if rule.glob.matches_path_with(path, match_options) { | ||
apply_rule_to_path(rule, path); | ||
apply_rule_to_path(&FileMatchLocation::from_root(loc, path), rule, reporter); | ||
} | ||
} | ||
} | ||
|
||
pub fn apply_ruleset_to_target(ruleset: &Ruleset, target: &Path) { | ||
pub fn apply_ruleset_to_target(ruleset: &Ruleset, target: &Path, reporter: &mut Reporter) { | ||
let loc = &RootMatchLocation { root: target }; | ||
for rule in &ruleset.rules { | ||
apply_rule_to_target(&rule, target); | ||
apply_rule_to_target(&loc, &rule, reporter); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use std::path::Path; | ||
|
||
pub struct RootMatchLocation<'a> { | ||
pub root: &'a Path, | ||
} | ||
|
||
pub struct FileMatchLocation<'a> { | ||
pub root: &'a Path, | ||
pub file: &'a Path, | ||
} | ||
|
||
impl<'a> FileMatchLocation<'a> { | ||
pub fn from_root(root: &RootMatchLocation<'a>, file: &'a Path) -> FileMatchLocation<'a> { | ||
FileMatchLocation { | ||
root: root.root, | ||
file, | ||
} | ||
} | ||
} | ||
|
||
pub struct LineMatchLocation<'a> { | ||
pub root: &'a Path, | ||
pub file: &'a Path, | ||
pub line: usize, | ||
} | ||
|
||
impl<'a> LineMatchLocation<'a> { | ||
pub fn from_file(file: &FileMatchLocation<'a>, line: usize) -> LineMatchLocation<'a> { | ||
LineMatchLocation { | ||
root: file.root, | ||
file: file.file, | ||
line, | ||
} | ||
} | ||
} | ||
|
||
pub enum MatchLocation<'a> { | ||
Root(RootMatchLocation<'a>), | ||
File(FileMatchLocation<'a>), | ||
Line(LineMatchLocation<'a>), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use crate::location::MatchLocation; | ||
use std::path::PathBuf; | ||
|
||
pub struct Reporter { | ||
prev_root: PathBuf, | ||
} | ||
|
||
impl Reporter { | ||
pub fn new() -> Reporter { | ||
Reporter { | ||
prev_root: Default::default(), | ||
} | ||
} | ||
|
||
pub fn report(&mut self, location: &MatchLocation, message: &str) { | ||
let current_root = match location { | ||
MatchLocation::Root(loc) => loc.root, | ||
MatchLocation::File(loc) => loc.root, | ||
MatchLocation::Line(loc) => loc.root, | ||
}; | ||
|
||
if current_root != self.prev_root { | ||
println!("in {}", current_root.display()); | ||
self.prev_root = current_root.to_path_buf(); | ||
} | ||
|
||
match location { | ||
MatchLocation::Root(loc) => println!("- {}", message), | ||
MatchLocation::File(loc) => println!("- {}: {}", loc.file.display(), message), | ||
|
||
MatchLocation::Line(loc) => { | ||
println!("- {}:{}: {}", loc.file.display(), loc.line, message) | ||
} | ||
} | ||
} | ||
} |