use std::{future::Future, pin::Pin, task};
use futures_core::Stream;
use pin_project_lite::pin_project;
use tokio::sync::mpsc;
#[cfg(feature = "redis")]
#[cfg_attr(docsrs, doc(cfg(feature = "redis")))]
pub mod redis;
#[cfg(feature = "fred")]
#[cfg_attr(docsrs, doc(cfg(feature = "fred")))]
pub mod fred;
pin_project! {
#[derive(Debug)]
pub struct MessageStream<T> {
#[pin]
rx: mpsc::Receiver<T>,
}
}
impl<T> MessageStream<T> {
pub fn new_empty() -> Self {
let (_, rx) = mpsc::channel(1);
Self { rx }
}
pub fn new(rx: mpsc::Receiver<T>) -> Self {
Self { rx }
}
}
impl<T> Stream for MessageStream<T> {
type Item = T;
fn poll_next(
self: Pin<&mut Self>,
cx: &mut task::Context<'_>,
) -> task::Poll<Option<Self::Item>> {
self.project().rx.poll_recv(cx)
}
}
pub type ChanItem = (String, Vec<u8>);
pub trait Driver: Clone + Send + Sync + 'static {
type Error: std::error::Error + Send + 'static;
fn publish(
&self,
chan: String,
val: Vec<u8>,
) -> impl Future<Output = Result<(), Self::Error>> + Send;
fn subscribe(
&self,
chan: String,
size: usize,
) -> impl Future<Output = Result<MessageStream<ChanItem>, Self::Error>> + Send;
fn unsubscribe(&self, pat: String) -> impl Future<Output = Result<(), Self::Error>> + Send;
fn num_serv(&self, chan: &str) -> impl Future<Output = Result<u16, Self::Error>> + Send;
}