Skip to main content

rsigma_runtime/io/
stdout.rs

1use std::io::Write;
2
3use rsigma_eval::ProcessResult;
4
5use crate::error::RuntimeError;
6
7/// Serializes ProcessResult to NDJSON and writes to stdout.
8pub struct StdoutSink {
9    pretty: bool,
10}
11
12impl StdoutSink {
13    pub fn new(pretty: bool) -> Self {
14        StdoutSink { pretty }
15    }
16
17    /// Serialize and write a ProcessResult to stdout.
18    pub fn send(&self, result: &ProcessResult) -> Result<(), RuntimeError> {
19        if result.detections.is_empty() && result.correlations.is_empty() {
20            return Ok(());
21        }
22
23        let stdout = std::io::stdout();
24        let mut out = stdout.lock();
25
26        for m in &result.detections {
27            let json = if self.pretty {
28                serde_json::to_string_pretty(m)?
29            } else {
30                serde_json::to_string(m)?
31            };
32            writeln!(out, "{json}")?;
33        }
34
35        for m in &result.correlations {
36            let json = if self.pretty {
37                serde_json::to_string_pretty(m)?
38            } else {
39                serde_json::to_string(m)?
40            };
41            writeln!(out, "{json}")?;
42        }
43
44        Ok(())
45    }
46
47    /// Write a pre-serialized JSON string directly to stdout.
48    pub fn send_raw(&self, json: &str) -> Result<(), RuntimeError> {
49        let stdout = std::io::stdout();
50        let mut out = stdout.lock();
51        writeln!(out, "{json}")?;
52        Ok(())
53    }
54}