use std::sync::Arc;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use crate::{Error, Message};
pub mod file;
pub mod http;
pub mod kafka;
pub mod redis;
pub mod stdout;
#[async_trait]
pub trait Output: Send + Sync {
async fn connect(&self) -> Result<(), Error>;
async fn write(&self, msg: &Message) -> Result<(), Error>;
async fn close(&self) -> Result<(), Error>;
}
#[async_trait]
pub trait OutputBatch: Send + Sync {
async fn connect(&self) -> Result<(), Error>;
async fn write(&self, msg: &[Message]) -> Result<(), Error>;
async fn close(&self) -> Result<(), Error>;
}
#[async_trait]
impl<T> OutputBatch for T
where
T: Output,
{
async fn connect(&self) -> Result<(), Error> {
self.connect().await
}
async fn write(&self, msg: &[Message]) -> Result<(), Error> {
for x in msg {
self.write(x).await?
}
Ok(())
}
async fn close(&self) -> Result<(), Error> {
self.close().await
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum OutputConfig {
File(file::FileOutputConfig),
Stdout(stdout::StdoutOutputConfig),
}
impl OutputConfig {
pub fn build(&self) -> Result<Arc<dyn OutputBatch>, Error> {
match self {
OutputConfig::File(config) => Ok(Arc::new(file::FileOutput::new(config)?)),
OutputConfig::Stdout(config) => Ok(Arc::new(stdout::StdoutOutput::new(config)?)),
}
}
}