use crate::error::Result;
use crate::models::{stream_event::Event, Position};
use async_trait::async_trait;
use serde_json::Value;
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct SyncResult {
pub success_count: usize,
pub failed_count: usize,
pub last_position: Option<Position>,
pub errors: Vec<String>,
}
impl SyncResult {
pub fn new() -> Self {
Self {
success_count: 0,
failed_count: 0,
last_position: None,
errors: Vec::new(),
}
}
pub fn add_success(&mut self) {
self.success_count += 1;
}
pub fn add_failure(&mut self, error: String) {
self.failed_count += 1;
self.errors.push(error);
}
pub fn is_successful(&self) -> bool {
self.failed_count == 0
}
}
#[async_trait]
pub trait DestinationAdapter: Send + Sync {
async fn connect(&mut self) -> Result<()>;
async fn process_events(&mut self, events: Vec<Event>) -> Result<SyncResult>;
async fn ensure_index(
&mut self,
index_name: &str,
schema: Option<HashMap<String, Value>>,
) -> Result<()>;
async fn import_data(
&mut self,
index_name: &str,
documents: Vec<Value>,
primary_key: Option<&str>,
) -> Result<SyncResult>;
async fn swap_indexes(&mut self, from: &str, to: &str) -> Result<()>;
async fn delete_index(&mut self, index_name: &str) -> Result<()>;
async fn get_index_stats(&self, index_name: &str) -> Result<Value>;
async fn health_check(&self) -> Result<bool>;
async fn is_healthy(&self) -> bool {
self.health_check().await.unwrap_or(false)
}
async fn disconnect(&mut self) -> Result<()>;
}
impl Default for SyncResult {
fn default() -> Self {
Self::new()
}
}