67 lines
1.4 KiB
Rust
67 lines
1.4 KiB
Rust
use crate::FileId;
|
|
use crate::ast;
|
|
use crate::rules::empty_inherit::EmptyInherit;
|
|
|
|
pub mod empty_inherit;
|
|
|
|
#[derive(Debug)]
|
|
pub struct Finding {
|
|
pub rule_id: &'static str,
|
|
pub range: ast::SourceRange,
|
|
pub message: &'static str,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct Diagnostic {
|
|
pub file_id: FileId,
|
|
pub finding: Finding,
|
|
}
|
|
|
|
impl std::fmt::Display for Diagnostic {
|
|
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
write!(
|
|
formatter,
|
|
"{}: {}: {}",
|
|
self.finding.range, self.finding.rule_id, self.finding.message,
|
|
)
|
|
}
|
|
}
|
|
|
|
pub trait NodeRule {
|
|
type Node;
|
|
|
|
const ID: &'static str;
|
|
|
|
fn select(data: &ast::NodeData) -> Option<&Self::Node>;
|
|
fn check(node: &Self::Node) -> Option<&'static str>;
|
|
}
|
|
|
|
fn run_node_rule<R>(node: &ast::Node) -> Option<Finding>
|
|
where
|
|
R: NodeRule,
|
|
{
|
|
let typed_node = R::select(&node.data)?;
|
|
let message = R::check(typed_node)?;
|
|
|
|
Some(Finding {
|
|
rule_id: R::ID,
|
|
range: node.range,
|
|
message,
|
|
})
|
|
}
|
|
|
|
pub fn run_rules(file: &ast::ParsedFile) -> Vec<Diagnostic> {
|
|
file.nodes
|
|
.iter()
|
|
.flat_map(|node| {
|
|
[run_node_rule::<EmptyInherit>(node)]
|
|
.into_iter()
|
|
.flatten()
|
|
.map(|finding| Diagnostic {
|
|
file_id: file.file_id,
|
|
finding,
|
|
})
|
|
})
|
|
.collect()
|
|
}
|