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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//! A router that selects a secondary handler on error.

use context::hypermedia::Link;
use handler::{HandleRequest, Environment, Build, BuilderContext, ApplyContext, Merge};

/// A router that selects a secondary handler on error.
///
/// ```
/// use rustful::{OrElse, Context, Response};
///
/// fn error_handler(_: Context, response: Response) {
///     let status = response.status();
///     response.send(format!("Status: {}", status));
/// }
///
/// //Every request will end up at error_handler
/// let handler = OrElse::<Option<fn(Context, Response)>, _>::new(None, error_handler);
/// ```
#[derive(Clone)]
pub struct OrElse<A, B> {
    ///The primary handler that will have the first chance to process the
    ///request.
    pub primary: A,

    ///The secondary handler that will take over if the first handler returns
    ///an error.
    pub secondary: B,
}

impl<A, B> OrElse<A, B> {
    ///Create a new `OrElse` with a primary and secondary handler.
    pub fn new(primary: A, secondary: B) -> OrElse<A, B> {
        OrElse {
            primary: primary,
            secondary: secondary,
        }
    }

    /// Build the router and its children using a chaninable API.
    ///
    /// ```
    /// use rustful::{Context, Response, OrElse};
    /// use rustful::handler::MethodRouter;
    ///
    /// type Inner = MethodRouter<fn(Context, Response)>;
    ///
    /// fn get(_context: Context, response: Response) {
    ///     response.send("A GET request.");
    /// }
    ///
    /// fn post(_context: Context, response: Response) {
    ///     response.send("A POST request.");
    /// }
    ///
    /// fn or_else(_context: Context, response: Response) {
    ///     response.send("Hello world!");
    /// }
    ///
    /// let mut router = OrElse::<Inner, _>::with_secondary(or_else as fn(Context, Response));
    ///
    /// router.build().primary().many(|mut inner_router|{
    ///     inner_router.on_get(get as fn(Context, Response));
    ///     inner_router.on_post(post);
    /// });
    /// ```
    pub fn build(&mut self) -> Builder<A, B> {
        self.get_builder(BuilderContext::new())
    }
}

impl<A, B: Default> OrElse<A, B> {
    ///Create a new `OrElse` with a given primary and a default secondary handler.
    pub fn with_primary(primary: A) -> OrElse<A, B> {
        OrElse {
            primary: primary,
            secondary: B::default(),
        }
    }
}

impl<A: Default, B> OrElse<A, B> {
    ///Create a new `OrElse` with a given secondary and a default primary handler.
    pub fn with_secondary(secondary: B) -> OrElse<A, B> {
        OrElse {
            primary: A::default(),
            secondary: secondary,
        }
    }
}

impl<A: HandleRequest, B: HandleRequest> HandleRequest for OrElse<A, B> {
    fn handle_request<'a, 'b, 'l, 'g>(&self, environment: Environment<'a, 'b, 'l, 'g>) -> Result<(), Environment<'a, 'b, 'l, 'g>> {
        self.primary.handle_request(environment).or_else(|e| self.secondary.handle_request(e))
    }

    fn hyperlinks<'a>(&'a self, base: Link<'a>) -> Vec<Link<'a>> {
        let mut links = self.primary.hyperlinks(base.clone());
        links.extend(self.secondary.hyperlinks(base.clone()));
        links
    }
}

impl<A: Default, B: Default> Default for OrElse<A, B> {
    fn default() -> OrElse<A, B> {
        OrElse {
            primary: A::default(),
            secondary: B::default(),
        }
    }
}

impl<'a, A: 'a, B: 'a> Build<'a> for OrElse<A, B> {
    type Builder = Builder<'a, A, B>;

    fn get_builder(&'a mut self, context: BuilderContext) -> Builder<'a, A, B> {
        Builder {
            router: self,
            context: context
        }
    }
}

impl<A: ApplyContext, B: ApplyContext> ApplyContext for OrElse<A, B> {
    fn apply_context(&mut self, context: BuilderContext) {
        self.primary.apply_context(context.clone());
        self.secondary.apply_context(context);
    }

    fn prepend_context(&mut self, context: BuilderContext) {
        self.primary.prepend_context(context.clone());
        self.secondary.prepend_context(context);
    }
}

impl<A: Merge, B: Merge> Merge for OrElse<A, B> {
    fn merge(&mut self, other: OrElse<A, B>) {
        self.primary.merge(other.primary);
        self.secondary.merge(other.secondary);
    }
}

/// A builder for an `OrElse`.
pub struct Builder<'a, A: 'a, B: 'a> {
    router: &'a mut OrElse<A, B>,
    context: BuilderContext
}

impl<'a, A, B> Builder<'a, A, B> {
    /// Perform more than one operation on this builder.
    ///
    /// ```
    /// use rustful::{Context, Response, OrElse};
    /// use rustful::handler::MethodRouter;
    ///
    /// type Inner = MethodRouter<fn(Context, Response)>;
    ///
    /// fn get(_context: Context, response: Response) {
    ///     response.send("A GET request.");
    /// }
    ///
    /// fn post(_context: Context, response: Response) {
    ///     response.send("A POST request.");
    /// }
    ///
    /// let mut router = OrElse::<Inner, Inner>::default();
    ///
    /// router.build().many(|mut router|{
    ///     router.primary().on_get(get as fn(Context, Response));
    ///     router.secondary().on_post(post);
    /// });
    /// ```
    pub fn many<F: FnOnce(&mut Builder<'a, A, B>)>(&mut self, build: F) -> &mut Builder<'a, A, B> {
        build(self);
        self
    }
}

impl<'a: 'b, 'b, A: Build<'b>, B> Builder<'a, A, B> {
    /// Build the primary router and its children.
    pub fn primary(&'b mut self) -> A::Builder {
        self.router.primary.get_builder(self.context.clone())
    }
}

impl<'a: 'b, 'b, A, B: Build<'b>> Builder<'a, A, B> {
    /// Build the secondary router and its children.
    pub fn secondary(&'b mut self) -> B::Builder {
        self.router.secondary.get_builder(self.context.clone())
    }
}