refactor(xatu): return structured reports
This commit was merged in pull request #73.
This commit is contained in:
@@ -4,9 +4,35 @@ mod parser;
|
||||
mod rules;
|
||||
|
||||
pub use file_store::{FileId, FileStore, SourceFile};
|
||||
pub use rules::{Diagnostic, Finding};
|
||||
|
||||
pub fn run(files: Vec<SourceFile>) -> status::Result<()> {
|
||||
#[derive(Debug)]
|
||||
pub struct Report {
|
||||
pub files: FileStore,
|
||||
pub diagnostics: Vec<Diagnostic>,
|
||||
}
|
||||
|
||||
impl Report {
|
||||
pub fn is_clean(&self) -> bool {
|
||||
self.diagnostics.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Report {
|
||||
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
for diagnostic in &self.diagnostics {
|
||||
let file = self.files.get(diagnostic.file_id);
|
||||
writeln!(formatter, "{}:{diagnostic}", file.path.display())?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Analyzes source files and returns the diagnostics Xatu finds.
|
||||
pub fn future_sight(files: Vec<SourceFile>) -> status::Result<Report> {
|
||||
let mut store = FileStore::default();
|
||||
let mut diagnostics = Vec::new();
|
||||
|
||||
for file in files {
|
||||
let file_id = store.add(file);
|
||||
@@ -20,17 +46,11 @@ pub fn run(files: Vec<SourceFile>) -> status::Result<()> {
|
||||
)));
|
||||
}
|
||||
|
||||
for diagnostic in rules::run_rules(&parsed) {
|
||||
println!(
|
||||
"{}:{}..{}: {}: {}",
|
||||
file.path.display(),
|
||||
diagnostic.finding.range.start,
|
||||
diagnostic.finding.range.end,
|
||||
diagnostic.finding.rule_id,
|
||||
diagnostic.finding.message,
|
||||
);
|
||||
}
|
||||
diagnostics.extend(rules::run_rules(&parsed));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
Ok(Report {
|
||||
files: store,
|
||||
diagnostics,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user