use std::io::Write;
use crate::VerifiedFinding;
use super::{ReportError, Reporter};
pub struct JsonlReporter<W: Write> {
writer: W,
}
impl<W: Write> JsonlReporter<W> {
pub fn new(writer: W) -> Self {
Self { writer }
}
}
impl<W: Write> Reporter for JsonlReporter<W> {
fn report(&mut self, finding: &VerifiedFinding) -> Result<(), ReportError> {
serde_json::to_writer(&mut self.writer, finding)?;
writeln!(self.writer)?;
Ok(())
}
fn finish(&mut self) -> Result<(), ReportError> {
self.writer.flush()?;
Ok(())
}
}
pub struct JsonReporter<W: Write> {
writer: W,
findings: Vec<VerifiedFinding>,
}
impl<W: Write> JsonReporter<W> {
pub fn new(writer: W) -> Self {
Self {
writer,
findings: Vec::new(),
}
}
}
impl<W: Write> Reporter for JsonReporter<W> {
fn report(&mut self, finding: &VerifiedFinding) -> Result<(), ReportError> {
self.findings.push(finding.clone());
Ok(())
}
fn finish(&mut self) -> Result<(), ReportError> {
serde_json::to_writer_pretty(&mut self.writer, &self.findings)?;
writeln!(self.writer)?;
Ok(())
}
}