rsigma_runtime/io/
file.rs1use std::fs::{File, OpenOptions};
2use std::io::{BufWriter, Write};
3use std::path::Path;
4
5use rsigma_eval::ProcessResult;
6
7use crate::error::RuntimeError;
8
9pub struct FileSink {
11 writer: BufWriter<File>,
12}
13
14impl FileSink {
15 pub fn open(path: &Path) -> Result<Self, RuntimeError> {
17 let file = OpenOptions::new().create(true).append(true).open(path)?;
18 Ok(FileSink {
19 writer: BufWriter::new(file),
20 })
21 }
22
23 pub fn send(&mut self, result: &ProcessResult) -> Result<(), RuntimeError> {
25 if result.is_empty() {
26 return Ok(());
27 }
28
29 for m in result {
30 let json = serde_json::to_string(m)?;
31 writeln!(self.writer, "{json}")?;
32 }
33
34 self.writer.flush()?;
35 Ok(())
36 }
37
38 pub fn send_raw(&mut self, json: &str) -> Result<(), RuntimeError> {
40 writeln!(self.writer, "{json}")?;
41 self.writer.flush()?;
42 Ok(())
43 }
44}