use crate::{
commands,
error::Error,
interfaces::{ClientLike, FredResult},
types::{FromValue, MultipleStrings, Value},
};
use bytes_utils::Str;
use fred_macros::rm_send_if;
use futures::Future;
use std::convert::TryInto;
#[rm_send_if(feature = "glommio")]
pub trait PubsubInterface: ClientLike + Sized + Send {
fn subscribe<S>(&self, channels: S) -> impl Future<Output = FredResult<()>> + Send
where
S: Into<MultipleStrings> + Send,
{
async move {
into!(channels);
commands::pubsub::subscribe(self, channels).await
}
}
fn unsubscribe<S>(&self, channels: S) -> impl Future<Output = FredResult<()>> + Send
where
S: Into<MultipleStrings> + Send,
{
async move {
into!(channels);
commands::pubsub::unsubscribe(self, channels).await
}
}
fn psubscribe<S>(&self, patterns: S) -> impl Future<Output = FredResult<()>> + Send
where
S: Into<MultipleStrings> + Send,
{
async move {
into!(patterns);
commands::pubsub::psubscribe(self, patterns).await
}
}
fn punsubscribe<S>(&self, patterns: S) -> impl Future<Output = FredResult<()>> + Send
where
S: Into<MultipleStrings> + Send,
{
async move {
into!(patterns);
commands::pubsub::punsubscribe(self, patterns).await
}
}
fn publish<R, S, V>(&self, channel: S, message: V) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
S: Into<Str> + Send,
V: TryInto<Value> + Send,
V::Error: Into<Error> + Send,
{
async move {
into!(channel);
try_into!(message);
commands::pubsub::publish(self, channel, message).await?.convert()
}
}
fn ssubscribe<C>(&self, channels: C) -> impl Future<Output = FredResult<()>> + Send
where
C: Into<MultipleStrings> + Send,
{
async move {
into!(channels);
commands::pubsub::ssubscribe(self, channels).await
}
}
fn sunsubscribe<C>(&self, channels: C) -> impl Future<Output = FredResult<()>> + Send
where
C: Into<MultipleStrings> + Send,
{
async move {
into!(channels);
commands::pubsub::sunsubscribe(self, channels).await
}
}
fn spublish<R, S, V>(&self, channel: S, message: V) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
S: Into<Str> + Send,
V: TryInto<Value> + Send,
V::Error: Into<Error> + Send,
{
async move {
into!(channel);
try_into!(message);
commands::pubsub::spublish(self, channel, message).await?.convert()
}
}
fn pubsub_channels<R, S>(&self, pattern: S) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
S: Into<Str> + Send,
{
async move {
into!(pattern);
commands::pubsub::pubsub_channels(self, pattern).await?.convert()
}
}
fn pubsub_numpat<R>(&self) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
{
async move { commands::pubsub::pubsub_numpat(self).await?.convert() }
}
fn pubsub_numsub<R, S>(&self, channels: S) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
S: Into<MultipleStrings> + Send,
{
async move {
into!(channels);
commands::pubsub::pubsub_numsub(self, channels).await?.convert()
}
}
fn pubsub_shardchannels<R, S>(&self, pattern: S) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
S: Into<Str> + Send,
{
async move {
into!(pattern);
commands::pubsub::pubsub_shardchannels(self, pattern).await?.convert()
}
}
fn pubsub_shardnumsub<R, S>(&self, channels: S) -> impl Future<Output = FredResult<R>> + Send
where
R: FromValue,
S: Into<MultipleStrings> + Send,
{
async move {
into!(channels);
commands::pubsub::pubsub_shardnumsub(self, channels).await?.convert()
}
}
}