Skip to content

Commit 50d3646

Browse files
committed
Implement ruleset applier (fixes #3)
1 parent 2d4a8ba commit 50d3646

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

Cargo.lock

Lines changed: 51 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ glob = "0.3.1"
1111
regex = { version = "1.10.3", default-features = false, features = ["std"] }
1212
serde = { version = "1.0.196", features = ["derive"] }
1313
serde_yaml = "0.9.31"
14+
walkdir = "2.4.0"

src/applier.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use crate::ruleset::{Rule, Ruleset};
2+
use glob;
3+
use std::fs;
4+
use std::path::Path;
5+
use walkdir::WalkDir;
6+
7+
fn apply_rule_to_path(rule: &Rule, path: &Path) {
8+
let text = fs::read_to_string(path).unwrap();
9+
10+
for (nline, line) in text.lines().enumerate() {
11+
if rule.regex.is_match(line) {
12+
println!("{}:{}: {}", path.display(), nline + 1, rule.title);
13+
}
14+
}
15+
}
16+
17+
fn apply_rule_to_target(rule: &Rule, target: &Path) {
18+
let mut match_options = glob::MatchOptions::new();
19+
match_options.require_literal_separator = true;
20+
21+
for path in WalkDir::new(target)
22+
.into_iter()
23+
.filter_map(|e| e.ok())
24+
.filter(|e| e.file_type().is_file())
25+
.map(|e| e.into_path())
26+
{
27+
let path = path.strip_prefix(&target).unwrap();
28+
if rule.glob.matches_path_with(path, match_options) {
29+
apply_rule_to_path(rule, path);
30+
}
31+
}
32+
}
33+
34+
pub fn apply_ruleset_to_target(ruleset: &Ruleset, target: &Path) {
35+
for rule in &ruleset.rules {
36+
apply_rule_to_target(&rule, target);
37+
}
38+
}

src/main.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
mod applier;
12
mod parser;
23
mod ruleset;
34

5+
use crate::applier::apply_ruleset_to_target;
6+
use crate::parser::parse_config_from_file;
47
use clap::Parser;
58
use std::path::PathBuf;
69

@@ -18,4 +21,10 @@ struct Args {
1821

1922
fn main() {
2023
let args = Args::parse();
24+
25+
let config = parse_config_from_file(&args.config);
26+
27+
for target in args.targets {
28+
apply_ruleset_to_target(&config, &target);
29+
}
2130
}

0 commit comments

Comments
 (0)