1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use borrow_bag::{Handle, Lookup};
use futures::prelude::*;
use log::trace;
use std::panic::RefUnwindSafe;
use std::pin::Pin;
use crate::handler::HandlerFuture;
use crate::middleware::chain::NewMiddlewareChain;
use crate::pipeline::set::PipelineSet;
use crate::pipeline::Pipeline;
use crate::state::{request_id, State};
pub trait PipelineHandleChain<P>: RefUnwindSafe {
fn call<F>(&self, pipelines: &PipelineSet<P>, state: State, f: F) -> Pin<Box<HandlerFuture>>
where
F: FnOnce(State) -> Pin<Box<HandlerFuture>> + Send + 'static;
}
impl<'a, P, T, N, U> PipelineHandleChain<P> for (Handle<Pipeline<T>, N>, U)
where
T: NewMiddlewareChain,
T::Instance: Send + 'static,
U: PipelineHandleChain<P>,
P: Lookup<Pipeline<T>, N>,
N: RefUnwindSafe,
{
fn call<F>(&self, pipelines: &PipelineSet<P>, state: State, f: F) -> Pin<Box<HandlerFuture>>
where
F: FnOnce(State) -> Pin<Box<HandlerFuture>> + Send + 'static,
{
let (handle, ref chain) = *self;
match pipelines.borrow(handle).construct() {
Ok(p) => chain.call(pipelines, state, move |state| p.call(state, f)),
Err(e) => {
trace!("[{}] error borrowing pipeline", request_id(&state));
future::err((state, e.into())).boxed()
}
}
}
}
impl<P> PipelineHandleChain<P> for () {
fn call<F>(&self, _: &PipelineSet<P>, state: State, f: F) -> Pin<Box<HandlerFuture>>
where
F: FnOnce(State) -> Pin<Box<HandlerFuture>> + Send + 'static,
{
trace!("[{}] start pipeline", request_id(&state));
f(state)
}
}