use super::builder::Subscription;
use crate::{
forward::{SamePlatform, ValueRecord},
handler::{Emit, Handler},
sink::Sink,
source::{Source, SourceItem},
};
use std::sync::Arc;
#[derive(Debug)]
pub struct ByRecord;
#[derive(Debug)]
pub struct ByValue;
#[derive(Debug)]
pub struct One;
#[derive(Debug)]
pub struct Many;
pub trait Cardinality<R>: 'static {
type Item: Send + Sync + 'static;
fn emit(result: R) -> Emit<Self::Item>;
}
impl<R: Send + Sync + 'static> Cardinality<R> for One {
type Item = R;
fn emit(result: R) -> Emit<R> {
Emit::One(result)
}
}
impl<T: Send + Sync + 'static> Cardinality<Emit<T>> for Many {
type Item = T;
fn emit(result: Emit<T>) -> Emit<T> {
result
}
}
pub trait IntoHandler<S: Source, K, M>: Send + Sync + 'static {
type Output: Send + Sync + 'static;
#[doc(hidden)]
fn into_subscription(
self,
name: String,
source: S,
sink: K,
) -> Subscription<S, K, Self::Output>;
}
impl<S, K, H, C> IntoHandler<S, K, (ByRecord, ByRecord, C)> for H
where
S: Source,
H: Handler<SourceItem<S>>,
C: Cardinality<H::Output>,
K: Sink<C::Item>,
{
type Output = C::Item;
fn into_subscription(self, name: String, source: S, sink: K) -> Subscription<S, K, C::Item> {
let handler = Arc::new(self);
Subscription::with_handler(name, source, sink, move |input| {
let handler = handler.clone();
async move { handler.handle(input).await.map(C::emit) }
})
}
}
impl<S, K, H, C> IntoHandler<S, K, (ByValue, ByRecord, C)> for H
where
S: Source,
SourceItem<S>: ValueRecord,
H: Handler<<SourceItem<S> as ValueRecord>::Value>,
C: Cardinality<H::Output>,
K: Sink<C::Item>,
{
type Output = C::Item;
fn into_subscription(self, name: String, source: S, sink: K) -> Subscription<S, K, C::Item> {
let handler = Arc::new(self);
Subscription::with_handler(name, source, sink, move |record: SourceItem<S>| {
let handler = handler.clone();
async move { handler.handle(record.value()?).await.map(C::emit) }
})
}
}
impl<S, K, H, C> IntoHandler<S, K, (ByRecord, ByValue, C)> for H
where
S: Source,
H: Handler<SourceItem<S>>,
C: Cardinality<H::Output>,
SourceItem<S>: SamePlatform<C::Item>,
K: Sink<<SourceItem<S> as SamePlatform<C::Item>>::Publish>,
{
type Output = <SourceItem<S> as SamePlatform<C::Item>>::Publish;
fn into_subscription(
self,
name: String,
source: S,
sink: K,
) -> Subscription<S, K, Self::Output> {
let handler = Arc::new(self);
Subscription::with_handler(name, source, sink, move |input| {
let handler = handler.clone();
async move {
let result = handler.handle(input).await?;
Ok(C::emit(result).map(<SourceItem<S> as SamePlatform<C::Item>>::publish))
}
})
.middleware(<SourceItem<S> as SamePlatform<C::Item>>::Inherit::default())
}
}
impl<S, K, H, C> IntoHandler<S, K, (ByValue, ByValue, C)> for H
where
S: Source,
H: Handler<<SourceItem<S> as ValueRecord>::Value>,
C: Cardinality<H::Output>,
SourceItem<S>: SamePlatform<C::Item>,
K: Sink<<SourceItem<S> as SamePlatform<C::Item>>::Publish>,
{
type Output = <SourceItem<S> as SamePlatform<C::Item>>::Publish;
fn into_subscription(
self,
name: String,
source: S,
sink: K,
) -> Subscription<S, K, Self::Output> {
let handler = Arc::new(self);
Subscription::with_handler(name, source, sink, move |record: SourceItem<S>| {
let handler = handler.clone();
async move {
let result = handler.handle(record.value()?).await?;
Ok(C::emit(result).map(<SourceItem<S> as SamePlatform<C::Item>>::publish))
}
})
.middleware(<SourceItem<S> as SamePlatform<C::Item>>::Inherit::default())
}
}