Skip to main content

ailint_core/reporter/
mod.rs

1//! Output formatters for lint results.
2
3pub mod json;
4pub mod markdown;
5pub mod sarif;
6pub mod terminal;
7
8use std::io::Write;
9
10use anyhow::Result;
11use serde::{Deserialize, Serialize};
12
13use crate::rules::Violation;
14
15/// Which reporter format to use.
16#[derive(
17    Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema,
18)]
19#[serde(rename_all = "lowercase")]
20pub enum ReporterKind {
21    /// Human-readable table on stdout.
22    #[default]
23    Terminal,
24    /// Machine-readable JSON report.
25    Json,
26    /// SARIF 2.1.0, for code-scanning integrations.
27    Sarif,
28    /// Markdown table, for PR comments and docs.
29    Markdown,
30}
31
32/// A reporter serializes violations to some sink.
33pub trait Reporter {
34    /// Write all violations to `out` in this reporter's format.
35    fn report(&self, violations: &[Violation], out: &mut dyn Write) -> Result<()>;
36}
37
38/// Construct a boxed reporter for the given kind.
39pub fn make(kind: ReporterKind) -> Box<dyn Reporter> {
40    match kind {
41        ReporterKind::Terminal => Box::new(terminal::TerminalReporter::default()),
42        ReporterKind::Json => Box::new(json::JsonReporter::default()),
43        ReporterKind::Sarif => Box::new(sarif::SarifReporter),
44        ReporterKind::Markdown => Box::new(markdown::MarkdownReporter::default()),
45    }
46}