use std::io::Write;
use crate::VerifiedFinding;
use super::{ReportError, Reporter, WriterBackedReporter};
pub struct JsonlReporter<W: Write + Send> {
writer: W,
}
impl<W: Write + Send> JsonlReporter<W> {
pub fn new(writer: W) -> Self {
Self { writer }
}
}
impl<W: Write + Send> 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.flush_writer()
}
}
impl<W: Write + Send> WriterBackedReporter for JsonlReporter<W> {
type Writer = W;
fn writer_mut(&mut self) -> &mut Self::Writer {
&mut self.writer
}
}
pub struct JsonArrayReporter<W: Write + Send> {
writer: W,
first: bool,
}
impl<W: Write + Send> JsonArrayReporter<W> {
pub fn new(mut writer: W) -> Result<Self, ReportError> {
write!(writer, "[")?;
Ok(Self {
writer,
first: true,
})
}
}
impl<W: Write + Send> Reporter for JsonArrayReporter<W> {
fn report(&mut self, finding: &VerifiedFinding) -> Result<(), ReportError> {
if !self.first {
write!(self.writer, ",")?;
}
serde_json::to_writer(&mut self.writer, finding)?;
self.first = false;
Ok(())
}
fn finish(&mut self) -> Result<(), ReportError> {
write!(self.writer, "]")?;
self.flush_writer()
}
}
impl<W: Write + Send> WriterBackedReporter for JsonArrayReporter<W> {
type Writer = W;
fn writer_mut(&mut self) -> &mut Self::Writer {
&mut self.writer
}
}
pub type JsonReporter<W> = JsonArrayReporter<W>;