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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use futures::IntoFuture;
use std::sync::Arc;

use super::{BoxFutureResponse, BoxHandler, Context, Handler, Response};

// TODO: Discuss alternative designs to reduce allocation. We're knee deep in Boxes here.
//       I could just be missing something obvious that would keep the design and reduce Boxes.

/// A set of [`StackHandler`]s that are executed in sequence **around** a _root_ `Handler`.
///
/// A `Stack` is constructed by wrapping a _root_ `Handler`.
///
/// ```rust, ignore
/// # use salt::{Stack, Response, Status};
/// let stack = Stack::new(|_| {
///   // [...]
/// # Response::with(Status::NoContent)
/// });
/// ```
pub struct Stack {
    root: Arc<BoxHandler>,
    elements: Vec<BoxStackBoxHandler>,
}

impl Stack {
    pub fn new<H: Handler + 'static>(root: H) -> Self {
        Stack {
            root: Arc::new(root.boxed()),
            elements: Vec::new(),
        }
    }

    // Appends a new [`StackHandler`] to the end of this `Stack`.
    pub fn add<T: StackHandler + 'static>(&mut self, handler: T) {
        self.elements.push(boxed(handler));
    }
}

impl Handler for Stack {
    type Result = BoxFutureResponse;

    #[inline]
    fn call(&self, ctx: Context) -> Self::Result {
        // Define the initial 'next' fn that simply calls the root handler
        let root = self.root.clone();
        let mut next = Box::new(move |ctx| root.call(ctx)) as BoxHandler;

        // Iterate backwards through our stack to produce a forwards chain of 'next' fns
        for element in self.elements.iter().rev() {
            next = element.call(next);
        }

        // Kick off
        next.call(ctx)
    }
}

pub trait StackHandler: Send + Sync {
    type Handler: Handler + 'static;

    fn call(&self, next: BoxHandler) -> Self::Handler;
}

impl<TError, TFuture, THandler, TFn> StackHandler for TFn
where
    TFuture: IntoFuture<Item = Response, Error = TError>,
    THandler: Handler<Result = TFuture>,
    THandler: 'static,
    TFn: Send + Sync,
    TFn: Fn(BoxHandler) -> THandler,
{
    type Handler = THandler;

    #[inline]
    fn call(&self, next: BoxHandler) -> Self::Handler {
        (*self)(next)
    }
}

trait StackBoxHandler: Send + Sync {
    fn call(&self, next: BoxHandler) -> BoxHandler;
}

impl<TFn> StackBoxHandler for TFn
where
    TFn: Send + Sync,
    TFn: Fn(BoxHandler) -> BoxHandler,
{
    #[inline]
    fn call(&self, next: BoxHandler) -> BoxHandler {
        (*self)(next)
    }
}

type BoxStackBoxHandler = Box<StackBoxHandler>;

fn boxed<H: StackHandler + 'static>(handler: H) -> BoxStackBoxHandler {
    Box::new(move |next: BoxHandler| handler.call(next).boxed())
}