use std::fmt::Debug;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
pub type TransportFuture<'a> = Pin<Box<dyn Future<Output = Result<(), TransportError>> + Send + 'a>>;
#[derive(Debug, Clone)]
pub struct TransportMessage {
pub event_name: String,
pub payload: String,
}
pub type TransportMessageCallback = Arc<dyn Fn(TransportMessage) + Send + Sync>;
pub trait TransportSubscription: Send + Sync {
fn stop(&self);
fn is_active(&self) -> bool;
}
#[derive(Debug, thiserror::Error)]
pub enum TransportError {
#[error("Connection error: {0}")]
Connection(String),
#[error("Publish error: {0}")]
Publish(String),
#[error("Subscribe error: {0}")]
Subscribe(String),
#[error("Serialization error: {0}")]
Serialization(String),
}
pub trait Transport: Send + Sync {
fn publish(&self, event_name: &str, payload: &str) -> TransportFuture<'_>;
fn subscribe(
&self,
event_pattern: &str,
callback: TransportMessageCallback,
) -> Result<Box<dyn TransportSubscription>, TransportError>;
fn clone_box(&self) -> Box<dyn Transport>;
}
pub trait ForwarderHandle: Send + Sync {
fn stop(&self);
fn is_finished(&self) -> bool;
}
impl Debug for Box<dyn Transport> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Transport").finish()
}
}
impl Debug for Box<dyn TransportSubscription> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TransportSubscription").finish()
}
}