pub(crate) mod controller;
pub(crate) mod enhancer;
pub(crate) mod handler;
pub(crate) mod inputs;
pub(crate) mod metadata;
pub(crate) mod next;
pub(crate) mod pipeline;
pub(crate) mod server;
use std::any::type_name;
use std::future::Future;
use crate::timer::{BoxError, BoxFuture};
use crate::transport::inputs::Inputs;
use crate::transport::next::Next;
pub trait Transport: 'static {
const KEY: &'static str;
const READS_PREFIX: bool = false;
const BUILDS_CONTROLLER: bool = true;
type Cx: Clone + Send + Sync;
type Reply: Send;
fn inputs(_d: &mut Inputs) {}
}
#[diagnostic::on_unimplemented(
message = "`{Self}` is not a guard for `{T}`",
label = "a guard for `{T}` is needed here",
note = "implement `Guard<{T}>` for `{Self}`"
)]
pub trait Guard<T: Transport>: Send + Sync + 'static {
fn can_activate(&self, cx: &T::Cx) -> impl Future<Output = Result<bool, BoxError>> + Send;
}
#[diagnostic::on_unimplemented(
message = "`{Self}` is not an interceptor for `{T}`",
label = "an interceptor for `{T}` is needed here",
note = "implement `Interceptor<{T}>` for `{Self}`"
)]
pub trait Interceptor<T: Transport>: Send + Sync + 'static {
fn intercept(&self, cx: &T::Cx, next: Next<'_, T>) -> impl Future<Output = Result<T::Reply, BoxError>> + Send;
}
#[diagnostic::on_unimplemented(
message = "`{Self}` is not an error handler for `{T}`",
label = "an error handler for `{T}` is needed here",
note = "implement `ErrorHandler<{T}>` for `{Self}`"
)]
pub trait ErrorHandler<T: Transport>: Send + Sync + 'static {
fn handle(&self, err: BoxError, cx: &T::Cx) -> impl Future<Output = Result<T::Reply, BoxError>> + Send;
}
pub trait ErasedGuard<T: Transport>: Send + Sync + 'static {
fn can_activate<'a>(&'a self, cx: &'a T::Cx) -> BoxFuture<'a, Result<bool, BoxError>>;
fn name(&self) -> &'static str;
}
impl<T: Transport, G: Guard<T>> ErasedGuard<T> for G {
fn can_activate<'a>(&'a self, cx: &'a T::Cx) -> BoxFuture<'a, Result<bool, BoxError>> {
Box::pin(Guard::can_activate(self, cx))
}
fn name(&self) -> &'static str {
type_name::<G>()
}
}
pub trait ErasedInterceptor<T: Transport>: Send + Sync + 'static {
fn intercept<'a>(&'a self, cx: &'a T::Cx, next: Next<'a, T>) -> BoxFuture<'a, Result<T::Reply, BoxError>>;
}
impl<T: Transport, I: Interceptor<T>> ErasedInterceptor<T> for I {
fn intercept<'a>(&'a self, cx: &'a T::Cx, next: Next<'a, T>) -> BoxFuture<'a, Result<T::Reply, BoxError>> {
Box::pin(Interceptor::intercept(self, cx, next))
}
}
pub trait ErasedErrorHandler<T: Transport>: Send + Sync + 'static {
fn handle<'a>(&'a self, err: BoxError, cx: &'a T::Cx) -> BoxFuture<'a, Result<T::Reply, BoxError>>;
}
impl<T: Transport, E: ErrorHandler<T>> ErasedErrorHandler<T> for E {
fn handle<'a>(&'a self, err: BoxError, cx: &'a T::Cx) -> BoxFuture<'a, Result<T::Reply, BoxError>> {
Box::pin(ErrorHandler::handle(self, err, cx))
}
}
pub type AnyGuard<T> = dyn ErasedGuard<T>;
pub type AnyInterceptor<T> = dyn ErasedInterceptor<T>;
pub type AnyErrorHandler<T> = dyn ErasedErrorHandler<T>;
mod sealed {
use super::{AnyErrorHandler, AnyGuard, AnyInterceptor, Transport};
pub trait Sealed {}
impl<T: Transport> Sealed for AnyGuard<T> {}
impl<T: Transport> Sealed for AnyInterceptor<T> {}
impl<T: Transport> Sealed for AnyErrorHandler<T> {}
}
#[diagnostic::on_unimplemented(
message = "`{Self}` is not a role key",
label = "a global enhancer is contributed under a role key",
note = "the role keys are `AnyGuard<T>`, `AnyInterceptor<T>` and `AnyErrorHandler<T>` for a `T: Transport`"
)]
pub trait Role: sealed::Sealed + Send + Sync + 'static {}
impl<T: Transport> Role for AnyGuard<T> {}
impl<T: Transport> Role for AnyInterceptor<T> {}
impl<T: Transport> Role for AnyErrorHandler<T> {}