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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//! Definition of `Endpoint`

use futures::{Future, IntoFuture};

use context::Context;
use super::combinator::{and_then, from_err, inspect, map, map_err, or, or_else, skip, then, with, AndThen, FromErr,
                        Inspect, Map, MapErr, Or, OrElse, Skip, Then, With};
use super::result::EndpointResult;


/// A HTTP endpoint, which provides the futures from incoming HTTP requests
pub trait Endpoint {
    /// The type of resolved value, created by this endpoint
    type Item;

    #[allow(missing_docs)]
    type Error;

    /// The type of future created by this endpoint
    type Future: Future<Item = Self::Item, Error = Self::Error>;

    /// Apply the incoming HTTP request, and return the future of its response
    fn apply(self, ctx: &mut Context) -> EndpointResult<Self::Future>;


    /// Combine itself and the other endpoint, and create a combinator which returns a pair of its
    /// `Item`s.
    fn join<E>(self, e: E) -> (Self, E)
    where
        Self: Sized,
        E: Endpoint<Error = Self::Error>,
    {
        (self, e)
    }

    /// Combine itself and two other endpoints, and create a combinator which returns a tuple of its
    /// `Item`s.
    fn join3<E1, E2>(self, e1: E1, e2: E2) -> (Self, E1, E2)
    where
        Self: Sized,
        E1: Endpoint<Error = Self::Error>,
        E2: Endpoint<Error = Self::Error>,
    {
        (self, e1, e2)
    }

    /// Combine itself and three other endpoints, and create a combinator which returns a tuple of its
    /// `Item`s.
    fn join4<E1, E2, E3>(self, e1: E1, e2: E2, e3: E3) -> (Self, E1, E2, E3)
    where
        Self: Sized,
        E1: Endpoint<Error = Self::Error>,
        E2: Endpoint<Error = Self::Error>,
        E3: Endpoint<Error = Self::Error>,
    {
        (self, e1, e2, e3)
    }

    /// Combine itself and four other endpoints, and create a combinator which returns a tuple of its
    /// `Item`s.
    fn join5<E1, E2, E3, E4>(self, e1: E1, e2: E2, e3: E3, e4: E4) -> (Self, E1, E2, E3, E4)
    where
        Self: Sized,
        E1: Endpoint<Error = Self::Error>,
        E2: Endpoint<Error = Self::Error>,
        E3: Endpoint<Error = Self::Error>,
        E4: Endpoint<Error = Self::Error>,
    {
        (self, e1, e2, e3, e4)
    }

    /// Combine itself and the other endpoint, and create a combinator which returns `E::Item`.
    fn with<E>(self, e: E) -> With<Self, E>
    where
        Self: Sized,
        E: Endpoint<Error = Self::Error>,
    {
        with(self, e)
    }

    /// Combine itself and the other endpoint, and create a combinator which returns `Self::Item`.
    fn skip<E>(self, e: E) -> Skip<Self, E>
    where
        Self: Sized,
        E: Endpoint<Error = Self::Error>,
    {
        skip(self, e)
    }

    /// Create an endpoint which attempts to apply `self`.
    /// If `self` failes, then revert the context and retry applying `e`.
    fn or<E>(self, e: E) -> Or<Self, E>
    where
        Self: Sized,
        E: Endpoint<Item = Self::Item, Error = Self::Error>,
    {
        or(self, e)
    }

    /// Combine itself and a function to change the return value to another type.
    fn map<F, U>(self, f: F) -> Map<Self, F>
    where
        Self: Sized,
        F: FnOnce(Self::Item) -> U,
    {
        map(self, f)
    }

    /// Combine itself and a function to change the error value to another type.
    fn map_err<F, U>(self, f: F) -> MapErr<Self, F>
    where
        Self: Sized,
        F: FnOnce(Self::Error) -> U,
    {
        map_err(self, f)
    }

    #[allow(missing_docs)]
    fn and_then<F, Fut>(self, f: F) -> AndThen<Self, F>
    where
        Self: Sized,
        F: FnOnce(Self::Item) -> Fut,
        Fut: IntoFuture<Error = Self::Error>,
    {
        and_then(self, f)
    }

    #[allow(missing_docs)]
    fn or_else<F, Fut>(self, f: F) -> OrElse<Self, F>
    where
        Self: Sized,
        F: FnOnce(Self::Error) -> Fut,
        Fut: IntoFuture<Item = Self::Item>,
    {
        or_else(self, f)
    }

    #[allow(missing_docs)]
    fn then<F, Fut>(self, f: F) -> Then<Self, F>
    where
        Self: Sized,
        F: FnOnce(Result<Self::Item, Self::Error>) -> Fut,
        Fut: IntoFuture,
    {
        then(self, f)
    }

    #[allow(missing_docs)]
    fn from_err<T>(self) -> FromErr<Self, T>
    where
        Self: Sized,
        T: From<Self::Error>,
    {
        from_err(self)
    }

    #[allow(missing_docs)]
    fn inspect<F>(self, f: F) -> Inspect<Self, F>
    where
        Self: Sized,
        F: FnOnce(&Self::Item),
    {
        inspect(self, f)
    }

    #[allow(missing_docs)]
    #[inline]
    fn with_type<T, E>(self) -> Self
    where
        Self: Sized + Endpoint<Item = T, Error = E>,
    {
        self
    }
}