Skip to content

Commit

Permalink
Implement rule match reporter (fixes #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
AMDmi3 committed Feb 8, 2024
1 parent 50d3646 commit 2bdce12
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 10 deletions.
24 changes: 15 additions & 9 deletions src/applier.rs
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);
}
}
41 changes: 41 additions & 0 deletions src/location.rs
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>),
}
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
mod applier;
mod location;
mod parser;
mod reporter;
mod ruleset;

use crate::applier::apply_ruleset_to_target;
use crate::parser::parse_config_from_file;
use crate::reporter::Reporter;
use clap::Parser;
use std::path::PathBuf;

Expand All @@ -24,7 +27,9 @@ fn main() {

let config = parse_config_from_file(&args.config);

let mut reporter = Reporter::new();

for target in args.targets {
apply_ruleset_to_target(&config, &target);
apply_ruleset_to_target(&config, &target, &mut reporter);
}
}
36 changes: 36 additions & 0 deletions src/reporter.rs
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)
}
}
}
}

0 comments on commit 2bdce12

Please sign in to comment.