use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use super::connection::Connections;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum FanoutTarget {
Channel(String),
User(String),
Connection(String),
KeyValue { key: String, value: String },
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Payload {
Text(String),
Binary(Vec<u8>),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Envelope {
pub origin: String,
pub target: FanoutTarget,
pub payload: Payload,
pub exclude: Option<String>,
}
#[async_trait]
pub trait Fanout: Send + Sync + 'static {
async fn publish(&self, envelope: Envelope) -> anyhow::Result<()>;
async fn attach(&self, _connections: Connections) -> anyhow::Result<()> {
Ok(())
}
fn shutdown(&self) {}
fn is_local_only(&self) -> bool {
false
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct LocalOnly;
#[async_trait]
impl Fanout for LocalOnly {
async fn publish(&self, _envelope: Envelope) -> anyhow::Result<()> {
Ok(())
}
fn is_local_only(&self) -> bool {
true
}
}