use std::sync::Arc;
use crate::error::DispatchStage;
use crate::redact::SecretRegistry;
use crate::timer::{BoxError, BoxFuture};
use crate::transport::pipeline::caught;
use crate::transport::{AnyInterceptor, ErasedInterceptor, Transport};
pub struct Next<'a, T: Transport> {
pub(crate) cx: &'a T::Cx,
pub(crate) rest: &'a [Arc<AnyInterceptor<T>>],
pub(crate) handler: Box<dyn FnOnce(&'a T::Cx) -> BoxFuture<'a, Result<T::Reply, BoxError>> + Send + 'a>,
pub(crate) secrets: &'a SecretRegistry,
}
impl<'a, T: Transport> Next<'a, T> {
pub fn run(self) -> BoxFuture<'a, Result<T::Reply, BoxError>> {
let Next { cx, rest, handler, secrets } = self;
match rest.split_first() {
Some((first, rest)) => {
let next = Next { cx, rest, handler, secrets };
Box::pin(caught(DispatchStage::Interceptor, secrets, async move {
<AnyInterceptor<T> as ErasedInterceptor<T>>::intercept(&**first, cx, next).await
}))
}
None => Box::pin(caught(DispatchStage::Handler, secrets, async move { handler(cx).await })),
}
}
}