use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use crate::{Error, MessageBatch};
mod drop;
pub mod file;
pub mod http;
pub mod kafka;
pub mod mqtt;
pub mod stdout;
#[async_trait]
pub trait Output: Send + Sync {
async fn connect(&self) -> Result<(), Error>;
async fn write(&self, msg: &MessageBatch) -> Result<(), Error>;
async fn close(&self) -> Result<(), Error>;
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum OutputConfig {
File(file::FileOutputConfig),
Http(http::HttpOutputConfig),
Kafka(kafka::KafkaOutputConfig),
Mqtt(mqtt::MqttOutputConfig),
Stdout(stdout::StdoutOutputConfig),
Drop,
}
impl OutputConfig {
pub fn build(&self) -> Result<Arc<dyn Output>, Error> {
match self {
OutputConfig::File(config) => Ok(Arc::new(file::FileOutput::new(config)?)),
OutputConfig::Http(config) => Ok(Arc::new(http::HttpOutput::new(config)?)),
OutputConfig::Kafka(config) => Ok(Arc::new(kafka::KafkaOutput::new(config)?)),
OutputConfig::Mqtt(config) => Ok(Arc::new(mqtt::MqttOutput::new(config)?)),
OutputConfig::Stdout(config) => Ok(Arc::new(stdout::StdoutOutput::new(config)?)),
OutputConfig::Drop => Ok(Arc::new(drop::DropOutput)),
}
}
}