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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
//! Components for building an `App`.

use std::fmt;
use std::sync::Arc;

use bytes::BytesMut;
use failure::{Error, Fail};
use http::header::HeaderValue;
use http::{header, HttpTryFrom, Method, Response};
use indexmap::map::IndexMap;

use error::handler::{DefaultErrorHandler, ErrorHandler};
use handler::{self, Handler};
use modifier::Modifier;

use super::recognizer::Recognizer;
use super::scoped_map;
use super::uri::{self, Uri};
use super::{App, AppState, Config, ModifierId, RouteData, RouteId, ScopeData, ScopeId};

/// A builder object for constructing an instance of `App`.
pub struct AppBuilder {
    routes: Vec<RouteBuilder>,
    scopes: Vec<ScopeBuilder>,
    config: Config,
    error_handler: Option<Box<dyn ErrorHandler + Send + Sync + 'static>>,
    modifiers: Vec<Box<dyn Modifier + Send + Sync + 'static>>,
    globals: scoped_map::Builder,
    prefix: Option<Uri>,

    result: Result<(), Error>,
}

struct ScopeBuilder {
    id: ScopeId,
    parent: ScopeId,
    modifiers: Vec<Box<dyn Modifier + Send + Sync + 'static>>,
    prefix: Option<Uri>,
    chain: Vec<ScopeId>,
}

#[cfg_attr(tarpaulin, skip)]
impl fmt::Debug for ScopeBuilder {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("ScopeBuilder")
            .field("parent", &self.parent)
            .field("prefix", &self.prefix)
            .field("chain", &self.chain)
            .finish()
    }
}

struct RouteBuilder {
    scope_id: ScopeId,
    uri: Uri,
    method: Method,
    modifiers: Vec<Box<dyn Modifier + Send + Sync + 'static>>,
    handler: Option<Box<dyn Handler + Send + Sync + 'static>>,
}

#[cfg_attr(tarpaulin, skip)]
impl fmt::Debug for RouteBuilder {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("RouteBuilder")
            .field("scope_id", &self.scope_id)
            .field("uri", &self.uri)
            .field("method", &self.method)
            .finish()
    }
}

#[cfg_attr(tarpaulin, skip)]
impl fmt::Debug for AppBuilder {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("AppBuilder")
            .field("routes", &self.routes)
            .field("scopes", &self.scopes)
            .field("config", &self.config)
            .field("prefix", &self.prefix)
            .field("result", &self.result)
            .finish()
    }
}

impl AppBuilder {
    pub(super) fn new() -> AppBuilder {
        AppBuilder {
            routes: vec![],
            scopes: vec![],
            config: Default::default(),
            error_handler: None,
            modifiers: vec![],
            globals: Default::default(),
            prefix: None,

            result: Ok(()),
        }
    }

    fn modify(&mut self, f: impl FnOnce(&mut Self) -> Result<(), Error>) {
        if self.result.is_ok() {
            self.result = f(self);
        }
    }

    /// Adds a route into the global scope.
    ///
    /// # Examples
    ///
    /// ```
    /// # extern crate tsukuyomi;
    /// # extern crate http;
    /// # use tsukuyomi::app::App;
    /// use tsukuyomi::app::builder::Route;
    /// # use tsukuyomi::input::Input;
    /// # use tsukuyomi::handler::Handle;
    /// # use http::Method;
    ///
    /// fn handler(_: &mut Input) -> Handle {
    ///     // ...
    /// # unimplemented!()
    /// }
    /// fn submit(_: &mut Input) -> Handle {
    ///     // ...
    /// # unimplemented!()
    /// }
    ///
    /// # fn main() -> tsukuyomi::AppResult<()> {
    /// let app = App::builder()
    ///     .route(("/", handler))
    ///     .route(("/", Method::POST, submit))
    ///     .route(|r: &mut Route| {
    ///         r.uri("/submit");
    ///         r.method(Method::POST);
    ///         r.handler(submit);
    ///     })
    ///     .finish()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn route(mut self, config: impl RouteConfig) -> Self {
        self.new_route(ScopeId::Global, config);
        self
    }

    fn new_route(&mut self, scope_id: ScopeId, config: impl RouteConfig) {
        let mut route = RouteBuilder {
            scope_id,
            uri: Uri::root(),
            method: Method::GET,
            modifiers: vec![],
            handler: None,
        };
        config.configure(&mut Route {
            builder: self,
            route: &mut route,
        });
        self.routes.push(route);
    }

    /// Creates a new scope with the provided configuration.
    ///
    /// # Examples
    ///
    /// ```
    /// # use tsukuyomi::app::App;
    /// use tsukuyomi::app::builder::Scope;
    /// # use tsukuyomi::input::Input;
    /// # use tsukuyomi::handler::Handle;
    ///
    /// fn get_post(_: &mut Input) -> Handle {
    ///     // ...
    /// # unimplemented!()
    /// }
    /// fn add_post(_: &mut Input) -> Handle {
    ///     // ...
    /// # unimplemented!()
    /// }
    ///
    /// # fn main() -> tsukuyomi::AppResult<()> {
    /// let app = App::builder()
    ///     .scope(|s: &mut Scope| {
    ///         s.prefix("/api/v1");
    ///         s.route(("/posts/:id", get_post));
    ///         s.route(("/posts", "POST", add_post));
    ///     })
    ///     .finish()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn scope(mut self, config: impl ScopeConfig) -> Self {
        self.new_scope(ScopeId::Global, config);
        self
    }

    fn new_scope(&mut self, parent: ScopeId, config: impl ScopeConfig) {
        let id = ScopeId::Local(self.scopes.len());
        let mut chain = parent
            .local_id()
            .map_or_else(Default::default, |id| self.scopes[id].chain.clone());
        chain.push(id);
        self.scopes.push(ScopeBuilder {
            id,
            parent,
            prefix: None,
            modifiers: vec![],
            chain,
        });

        config.configure(&mut Scope { builder: self, id });
    }

    /// Create a new scope mounted to the certain URI.
    ///
    /// This method is a shortcut of `AppBuilder::scope(Mount(prefix, f))`
    ///
    /// # Examples
    ///
    /// ```
    /// # use tsukuyomi::app::App;
    /// # use tsukuyomi::input::Input;
    /// # use tsukuyomi::handler::Handle;
    /// fn get_post(_: &mut Input) -> Handle {
    ///     // ...
    /// # unimplemented!()
    /// }
    /// fn add_post(_: &mut Input) -> Handle {
    ///     // ...
    /// # unimplemented!()
    /// }
    ///
    /// # fn main() -> tsukuyomi::AppResult<()> {
    /// let app = App::builder()
    ///     .mount("/api/v1", |s| {
    ///         s.route(("/posts/:id", get_post));
    ///         s.route(("/posts", "POST", add_post));
    ///     })
    ///     .finish()?;
    /// # Ok(())
    /// # }
    /// ```
    #[inline(always)]
    pub fn mount(self, prefix: &str, f: impl FnOnce(&mut Scope)) -> Self {
        self.scope(Mount(prefix, f))
    }

    /// Sets whether the fallback to GET if the handler for HEAD is not registered is enabled or not.
    ///
    /// The default value is `true`.
    pub fn fallback_head(mut self, enabled: bool) -> Self {
        self.modify(move |self_| {
            self_.config.fallback_head = enabled;
            Ok(())
        });
        self
    }

    /// Specifies whether to use the default OPTIONS handlers.
    ///
    /// If `enabled`, it creates the default OPTIONS handlers by collecting the registered
    /// methods from the router and then adds them to the global scope.
    pub fn default_options(mut self, enabled: bool) -> Self {
        self.modify(move |self_| {
            self_.config.fallback_options = enabled;
            Ok(())
        });
        self
    }

    /// Sets the instance to an error handler into this builder.
    pub fn error_handler(mut self, error_handler: impl ErrorHandler + Send + Sync + 'static) -> Self {
        self.error_handler = Some(Box::new(error_handler));
        self
    }

    /// Register a `Modifier` into the global scope.
    pub fn modifier(mut self, modifier: impl Modifier + Send + Sync + 'static) -> Self {
        self.add_modifier(ScopeId::Global, modifier);
        self
    }

    fn add_modifier(&mut self, id: ScopeId, modifier: impl Modifier + Send + Sync + 'static) {
        match id {
            ScopeId::Global => self.modifiers.push(Box::new(modifier)),
            ScopeId::Local(id) => self.scopes[id].modifiers.push(Box::new(modifier)),
        }
    }

    /// Sets a value of `T` to the global storage.
    pub fn set<T>(mut self, value: T) -> Self
    where
        T: Send + Sync + 'static,
    {
        self.set_value(value, ScopeId::Global);
        self
    }

    fn set_value<T>(&mut self, value: T, id: ScopeId)
    where
        T: Send + Sync + 'static,
    {
        self.globals.set(value, id);
    }

    /// Sets the prefix of URIs.
    pub fn prefix(mut self, prefix: &str) -> Self {
        self.set_prefix(prefix, ScopeId::Global);
        self
    }

    fn set_prefix(&mut self, prefix: &str, id: ScopeId) {
        if self.result.is_err() {
            return;
        }
        match prefix.parse() {
            Ok(prefix) => match id {
                ScopeId::Local(id) => self.scopes[id].prefix = Some(prefix),
                ScopeId::Global => self.prefix = Some(prefix),
            },
            Err(err) => self.result = Err(err),
        }
    }

    /// Creates a configured `App` using the current settings.
    pub fn finish(self) -> Result<App, Error> {
        let AppBuilder {
            routes,
            config,
            result,
            error_handler,
            modifiers,
            globals,
            scopes,
            prefix,
        } = self;

        result?;

        // finalize endpoints based on the created scope information.
        let mut routes: Vec<RouteData> = routes
            .into_iter()
            .enumerate()
            .map(|(route_id, route)| -> Result<RouteData, Error> {
                // build absolute URI.
                let mut uris = vec![&route.uri];
                let mut current = route.scope_id.local_id();
                while let Some(scope) = current.and_then(|i| scopes.get(i)) {
                    uris.extend(&scope.prefix);
                    current = scope.parent.local_id();
                }
                uris.extend(prefix.as_ref());
                let uri = uri::join_all(uris.into_iter().rev());

                let handler = route
                    .handler
                    .ok_or_else(|| format_err!("default handler is not supported"))?;

                // calculate the modifier identifiers.
                let mut modifier_ids: Vec<_> = (0..modifiers.len()).map(ModifierId::Global).collect();
                if let Some(scope) = route.scope_id.local_id().and_then(|id| scopes.get(id)) {
                    for (id, scope) in scope
                        .chain
                        .iter()
                        .filter_map(|&id| id.local_id().and_then(|id| scopes.get(id).map(|scope| (id, scope))))
                    {
                        modifier_ids.extend((0..scope.modifiers.len()).map(|pos| ModifierId::Scope(id, pos)));
                    }
                }
                modifier_ids.extend((0..route.modifiers.len()).map(|pos| ModifierId::Route(route_id, pos)));

                let id = RouteId(route.scope_id, route_id);

                Ok(RouteData {
                    id,
                    uri,
                    method: route.method,
                    modifiers: route.modifiers,
                    handler,
                    modifier_ids,
                })
            })
            .collect::<Result<_, _>>()?;

        // create a router
        let (recognizer, route_ids) = {
            let mut collected_routes = IndexMap::<Uri, IndexMap<Method, usize>>::new();
            for (i, route) in routes.iter().enumerate() {
                let methods = collected_routes
                    .entry(route.uri.clone())
                    .or_insert_with(IndexMap::<Method, usize>::new);

                if methods.contains_key(&route.method) {
                    bail!("Adding routes with duplicate URI and method is currenly not supported.");
                }

                methods.insert(route.method.clone(), i);
            }

            let mut recognizer = Recognizer::builder();
            let mut route_ids = vec![];
            for (uri, mut methods) in collected_routes {
                if config.fallback_options {
                    let m = methods.keys().cloned().chain(Some(Method::OPTIONS)).collect();
                    methods.entry(Method::OPTIONS).or_insert_with(|| {
                        let id = routes.len();
                        routes.push(RouteData {
                            id: RouteId(ScopeId::Global, id),
                            uri: uri.clone(),
                            method: Method::OPTIONS,
                            modifiers: vec![],
                            handler: default_options_handler(m),
                            modifier_ids: (0..modifiers.len()).map(ModifierId::Global).collect(),
                        });
                        id
                    });
                }

                recognizer.push(uri.as_ref())?;
                route_ids.push(methods);
            }

            (recognizer.finish(), route_ids)
        };

        // finalize error handler.
        let error_handler = error_handler.unwrap_or_else(|| Box::new(DefaultErrorHandler::new()));

        // finalize global/scope-local storages.
        let parents: Vec<_> = scopes.iter().map(|scope| scope.parent).collect();
        let globals = globals.finish(&parents[..]);

        let scopes = scopes
            .into_iter()
            .map(|scope| ScopeData {
                id: scope.id,
                parent: scope.parent,
                prefix: scope.prefix,
                modifiers: scope.modifiers,
            })
            .collect();

        Ok(App {
            inner: Arc::new(AppState {
                routes,
                scopes,
                recognizer,
                route_ids,
                config,
                error_handler,
                modifiers,
                globals,
            }),
        })
    }
}

// ==== Scope ====

/// A proxy object for configuration of a scope.
#[derive(Debug)]
pub struct Scope<'a> {
    builder: &'a mut AppBuilder,
    id: ScopeId,
}

impl<'a> Scope<'a> {
    /// Adds a route into the current scope, with the provided configuration.
    pub fn route(&mut self, config: impl RouteConfig) -> &mut Self {
        self.builder.new_route(self.id, config);
        self
    }

    /// Create a new sub-scope with the provided configuration.
    pub fn scope(&mut self, config: impl ScopeConfig) -> &mut Self {
        self.builder.new_scope(self.id, config);
        self
    }

    /// Create a new scope mounted to the certain URI.
    ///
    /// This method is a shortcut of `Scope::scope(Mount(prefix, f))`.
    #[inline(always)]
    pub fn mount(&mut self, prefix: &str, f: impl FnOnce(&mut Scope)) -> &mut Self {
        self.scope(Mount(prefix, f))
    }

    /// Adds a *scope-local* variable into the application.
    pub fn set<T>(&mut self, value: T) -> &mut Self
    where
        T: Send + Sync + 'static,
    {
        self.builder.set_value(value, self.id);
        self
    }

    /// Modifies the prefix URI of current scope.
    pub fn prefix(&mut self, prefix: &str) -> &mut Self {
        self.builder.set_prefix(prefix, self.id);
        self
    }

    /// Register a `Modifier` into the current scope.
    pub fn modifier(&mut self, modifier: impl Modifier + Send + Sync + 'static) -> &mut Self {
        self.builder.add_modifier(self.id, modifier);
        self
    }
}

/// Trait representing a set of configuration for setting a scope.
pub trait ScopeConfig {
    /// Applies this configuration to the provided `Scope`.
    fn configure(self, scope: &mut Scope);
}

impl<F> ScopeConfig for F
where
    F: FnOnce(&mut Scope),
{
    fn configure(self, scope: &mut Scope) {
        self(scope)
    }
}

/// A helper struct for instantiating a `ScopeConfig` from a prefix URI and a function.
#[derive(Debug)]
pub struct Mount<P, F>(pub P, pub F);

impl<P, F> ScopeConfig for Mount<P, F>
where
    P: AsRef<str>,
    F: FnOnce(&mut Scope),
{
    #[inline(always)]
    fn configure(self, scope: &mut Scope) {
        scope.prefix(self.0.as_ref());
        (self.1)(scope);
    }
}

// ==== Route ====

/// A proxy object for creating an endpoint.
#[derive(Debug)]
pub struct Route<'a> {
    builder: &'a mut AppBuilder,
    route: &'a mut RouteBuilder,
}

impl<'a> Route<'a> {
    /// Modifies the HTTP method of this route.
    pub fn method<M>(&mut self, method: M) -> &mut Self
    where
        Method: HttpTryFrom<M>,
        <Method as HttpTryFrom<M>>::Error: Fail,
    {
        if self.builder.result.is_ok() {
            match Method::try_from(method) {
                Ok(method) => self.route.method = method,
                Err(err) => self.builder.result = Err(Error::from(err.into())),
            }
        }
        self
    }

    /// Modifies the URI of this route.
    pub fn uri(&mut self, uri: &str) -> &mut Self {
        if self.builder.result.is_ok() {
            match uri.parse() {
                Ok(uri) => self.route.uri = uri,
                Err(err) => self.builder.result = Err(err),
            }
        }
        self
    }

    /// Register a `Modifier` to this route.
    pub fn modifier(&mut self, modifier: impl Modifier + Send + Sync + 'static) -> &mut Self {
        self.route.modifiers.push(Box::new(modifier));
        self
    }

    /// Sets a `Handler` to this route.
    pub fn handler(&mut self, handler: impl Handler + Send + Sync + 'static) -> &mut Self {
        self.route.handler = Some(Box::new(handler));
        self
    }
}

/// Trait representing a set of configuration for setting a route.
pub trait RouteConfig {
    /// Applies this configuration to the provided `Route`.
    fn configure(self, route: &mut Route);
}

impl<F> RouteConfig for F
where
    F: FnOnce(&mut Route),
{
    fn configure(self, route: &mut Route) {
        self(route)
    }
}

impl<A, B> RouteConfig for (A, B)
where
    A: AsRef<str>,
    B: Handler + Send + Sync + 'static,
{
    fn configure(self, route: &mut Route) {
        route.uri(self.0.as_ref());
        route.handler(self.1);
    }
}

impl<A, B, C> RouteConfig for (A, B, C)
where
    A: AsRef<str>,
    Method: HttpTryFrom<B>,
    C: Handler + Send + Sync + 'static,
    <Method as HttpTryFrom<B>>::Error: Fail,
{
    fn configure(self, route: &mut Route) {
        route.uri(self.0.as_ref());
        route.method(self.1);
        route.handler(self.2);
    }
}

// ====

fn default_options_handler(methods: Vec<Method>) -> Box<dyn Handler + Send + Sync + 'static> {
    let allowed_methods = {
        let bytes = methods
            .into_iter()
            .enumerate()
            .fold(BytesMut::new(), |mut acc, (i, m)| {
                if i > 0 {
                    acc.extend_from_slice(b", ");
                }
                acc.extend_from_slice(m.as_str().as_bytes());
                acc
            });
        unsafe { HeaderValue::from_shared_unchecked(bytes.freeze()) }
    };

    Box::new(handler::wrap_ready(move |_| {
        let mut response = Response::new(());
        response.headers_mut().insert(header::ALLOW, allowed_methods.clone());
        response
    }))
}