keyhog_core/report/
json.rs1use std::io::Write;
5
6use crate::VerifiedFinding;
7
8use super::{ReportError, Reporter, WriterBackedReporter};
9
10pub struct JsonlReporter<W: Write + Send> {
21 writer: W,
22}
23
24impl<W: Write + Send> JsonlReporter<W> {
25 pub fn new(writer: W) -> Self {
36 Self { writer }
37 }
38}
39
40impl<W: Write + Send> Reporter for JsonlReporter<W> {
41 fn report(&mut self, finding: &VerifiedFinding) -> Result<(), ReportError> {
42 serde_json::to_writer(&mut self.writer, finding)?;
43 writeln!(self.writer)?;
44 Ok(())
45 }
46
47 fn finish(&mut self) -> Result<(), ReportError> {
48 self.flush_writer()
49 }
50}
51
52impl<W: Write + Send> WriterBackedReporter for JsonlReporter<W> {
53 type Writer = W;
54
55 fn writer_mut(&mut self) -> &mut Self::Writer {
56 &mut self.writer
57 }
58}
59
60pub struct JsonArrayReporter<W: Write + Send> {
73 writer: W,
74 first: bool,
75}
76
77impl<W: Write + Send> JsonArrayReporter<W> {
78 pub fn new(mut writer: W) -> Result<Self, ReportError> {
91 write!(writer, "[")?;
92 Ok(Self {
93 writer,
94 first: true,
95 })
96 }
97}
98
99impl<W: Write + Send> Reporter for JsonArrayReporter<W> {
100 fn report(&mut self, finding: &VerifiedFinding) -> Result<(), ReportError> {
101 if !self.first {
102 write!(self.writer, ",")?;
103 }
104 serde_json::to_writer(&mut self.writer, finding)?;
105 self.first = false;
106 Ok(())
107 }
108
109 fn finish(&mut self) -> Result<(), ReportError> {
110 write!(self.writer, "]")?;
111 self.flush_writer()
112 }
113}
114
115impl<W: Write + Send> WriterBackedReporter for JsonArrayReporter<W> {
116 type Writer = W;
117
118 fn writer_mut(&mut self) -> &mut Self::Writer {
119 &mut self.writer
120 }
121}
122
123pub type JsonReporter<W> = JsonArrayReporter<W>;