Skip to main content

garbage_code_hunter/rules/
generic.rs

1use std::path::Path;
2
3use crate::analyzer::CodeIssue;
4
5/// A language-agnostic code quality rule that works on raw text content.
6///
7/// Kept as fallback for languages without tree-sitter grammar.
8pub trait GenericRule: Send + Sync {
9    /// Returns the unique identifier for this rule (e.g. `"c-goto-abuse"`).
10    fn name(&self) -> &'static str;
11
12    /// Analyze file content and return detected issues.
13    fn check_content(&self, file_path: &Path, content: &str, lang: &str) -> Vec<CodeIssue>;
14}
15
16/// Engine that runs all registered generic (text-based) rules against source files.
17pub struct GenericRuleEngine {
18    rules: Vec<Box<dyn GenericRule>>,
19}
20
21impl Default for GenericRuleEngine {
22    fn default() -> Self {
23        Self::new()
24    }
25}
26
27impl GenericRuleEngine {
28    pub fn new() -> Self {
29        Self { rules: Vec::new() }
30    }
31
32    pub fn check_file(&self, file_path: &Path, content: &str, lang: &str) -> Vec<CodeIssue> {
33        let mut issues = Vec::new();
34        for rule in &self.rules {
35            issues.extend(rule.check_content(file_path, content, lang));
36        }
37        issues
38    }
39
40    pub fn rule_names(&self) -> Vec<&'static str> {
41        self.rules.iter().map(|r| r.name()).collect()
42    }
43}