1mod 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
16pub trait OutputWriter {
18 fn write_logs(&mut self, logs: &FetchResult) -> Result<()>;
20
21 fn finalize(&mut self) -> Result<()>;
23}
24
25pub 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}