/ src / sinks / file.rs
file.rs
 1  use crate::graph::GraphUpdate;
 2  use crate::sinks::Sink;
 3  use anyhow::Result;
 4  use std::fs::File;
 5  use std::io::{BufWriter, Write};
 6  
 7  pub struct JsonlFileSink {
 8      w: BufWriter<File>,
 9  }
10  
11  impl JsonlFileSink {
12      pub fn create(path: &str) -> Result<Self> {
13          let file = File::create(path)?;
14          Ok(Self {
15              w: BufWriter::new(file),
16          })
17      }
18  }
19  
20  impl Sink for JsonlFileSink {
21      fn emit(&mut self, update: &GraphUpdate) -> Result<()> {
22          let s = serde_json::to_string(update)?;
23          writeln!(self.w, "{s}")?;
24          Ok(())
25      }
26  
27      fn flush(&mut self) -> Result<()> {
28          self.w.flush()?;
29          Ok(())
30      }
31  }