use async_trait::async_trait;
use mongodb::bson::Document;
use std::path::Path;
use tokio::fs::File;
use tokio::io::BufWriter;
use crate::error::Result;
pub mod jsonl;
pub mod csv;
pub use jsonl::JsonLWriter;
pub use csv::CsvWriter;
#[async_trait]
pub trait FormatWriter: Send {
async fn write_batch(&mut self, docs: &[Document]) -> Result<usize>;
async fn finalize(&mut self) -> Result<()>;
async fn file_size(&self) -> Result<u64>;
}
pub(crate) async fn create_writer(path: &str) -> Result<BufWriter<File>> {
let file = File::create(path).await.map_err(|e| {
crate::error::ExecutionError::InvalidOperation(format!("Failed to create file: {}", e))
})?;
Ok(BufWriter::with_capacity(8 * 1024 * 1024, file)) }
pub(crate) fn validate_path(path: &str) -> Result<()> {
let path_obj = Path::new(path);
if let Some(parent) = path_obj.parent() {
if !parent.as_os_str().is_empty() && !parent.exists() {
return Err(crate::error::ExecutionError::InvalidOperation(format!(
"Directory does not exist: {}",
parent.display()
))
.into());
}
}
Ok(())
}