Skip to main content

ethcli/output/
mod.rs

1//! Output writers for different formats
2
3mod csv;
4mod json;
5mod sqlite;
6
7pub use self::csv::CsvWriter;
8pub use self::json::JsonWriter;
9pub use self::sqlite::SqliteWriter;
10
11use crate::config::OutputFormat;
12use crate::error::Result;
13use crate::fetcher::FetchResult;
14use std::path::Path;
15
16/// Trait for output writers
17pub trait OutputWriter {
18    /// Write a batch of logs
19    fn write_logs(&mut self, logs: &FetchResult) -> Result<()>;
20
21    /// Finalize output (flush, close, etc.)
22    fn finalize(&mut self) -> Result<()>;
23}
24
25/// Create an output writer based on format and path
26pub fn create_writer(format: OutputFormat, path: Option<&Path>) -> Result<Box<dyn OutputWriter>> {
27    match format {
28        OutputFormat::Json => {
29            let writer = JsonWriter::new(path, false)?;
30            Ok(Box::new(writer))
31        }
32        OutputFormat::NdJson => {
33            let writer = JsonWriter::new(path, true)?;
34            Ok(Box::new(writer))
35        }
36        OutputFormat::Csv => {
37            let writer = CsvWriter::new(path)?;
38            Ok(Box::new(writer))
39        }
40        OutputFormat::Sqlite => {
41            let path = path.ok_or_else(|| {
42                crate::error::OutputError::FileCreate("SQLite requires output path".to_string())
43            })?;
44            let writer = SqliteWriter::new(path)?;
45            Ok(Box::new(writer))
46        }
47    }
48}