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
use crate::RouterRef;
use routefinder::{Captures, Match, RouteSpec, Router as Routefinder};
use std::{
    collections::BTreeSet,
    convert::TryInto,
    fmt::{self, Debug, Display, Formatter},
    mem,
};
use trillium::{async_trait, Conn, Handler, Info, KnownHeaderName, Method, Upgrade};

const ALL_METHODS: [Method; 5] = [
    Method::Delete,
    Method::Get,
    Method::Patch,
    Method::Post,
    Method::Put,
];

#[derive(Debug)]
enum MethodSelection {
    Just(Method),
    All,
    Any(Vec<Method>),
}

impl Display for MethodSelection {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            MethodSelection::Just(m) => Display::fmt(m, f),
            MethodSelection::All => f.write_str("*"),
            MethodSelection::Any(v) => {
                f.write_str(&v.iter().map(|m| m.as_ref()).collect::<Vec<_>>().join(", "))
            }
        }
    }
}

impl PartialEq<Method> for MethodSelection {
    fn eq(&self, other: &Method) -> bool {
        match self {
            MethodSelection::Just(m) => m == other,
            MethodSelection::All => true,
            MethodSelection::Any(v) => v.contains(other),
        }
    }
}

impl From<()> for MethodSelection {
    fn from(_: ()) -> MethodSelection {
        Self::All
    }
}

impl From<Method> for MethodSelection {
    fn from(method: Method) -> Self {
        Self::Just(method)
    }
}

impl From<&[Method]> for MethodSelection {
    fn from(methods: &[Method]) -> Self {
        Self::Any(methods.to_vec())
    }
}
impl From<Vec<Method>> for MethodSelection {
    fn from(methods: Vec<Method>) -> Self {
        Self::Any(methods)
    }
}

#[derive(Debug, Default)]
struct MethodRoutefinder(Routefinder<(MethodSelection, Box<dyn Handler>)>);
impl MethodRoutefinder {
    fn add<R>(
        &mut self,
        method_selection: impl Into<MethodSelection>,
        path: R,
        handler: impl Handler,
    ) where
        R: TryInto<RouteSpec>,
        R::Error: Debug,
    {
        self.0
            .add(path, (method_selection.into(), Box::new(handler)))
            .expect("could not add route")
    }

    fn methods_matching(&self, path: &str) -> BTreeSet<Method> {
        let mut set = BTreeSet::new();

        fn extend(ms: &MethodSelection, set: &mut BTreeSet<Method>) {
            match ms {
                MethodSelection::All => {
                    set.extend(ALL_METHODS);
                }
                MethodSelection::Just(method) => {
                    set.insert(*method);
                }
                MethodSelection::Any(methods) => {
                    set.extend(methods);
                }
            }
        }

        if path == "*" {
            for ms in self.0.iter().map(|(_, (m, _))| m) {
                extend(ms, &mut set);
            }
        } else {
            for m in self.0.match_iter(path) {
                extend(&m.0, &mut set);
            }
        };

        set.remove(&Method::Options);
        set
    }

    fn best_match<'a, 'b>(
        &'a self,
        method: Method,
        path: &'b str,
    ) -> Option<Match<'a, 'b, (MethodSelection, Box<dyn Handler>)>> {
        self.0.match_iter(path).find(|m| m.0 == method)
    }
}

/**
# The Router handler

See crate level docs for more, as this is the primary type in this crate.

*/
pub struct Router {
    routefinder: MethodRoutefinder,
    handle_options: bool,
}

impl Default for Router {
    fn default() -> Self {
        Self {
            routefinder: MethodRoutefinder::default(),
            handle_options: true,
        }
    }
}

macro_rules! method {
    ($fn_name:ident, $method:ident) => {
        method!(
            $fn_name,
            $method,
            concat!(
                // yep, macro-generated doctests
                "Registers a handler for the ",
                stringify!($fn_name),
                " http method.

```
# use trillium::Conn;
# use trillium_router::Router;
let router = Router::new().",
                stringify!($fn_name),
                "(\"/some/route\", |conn: Conn| async move {
  conn.ok(\"success\")
});

use trillium_testing::{methods::",
                stringify!($fn_name),
                ", assert_ok, assert_not_handled};
assert_ok!(",
                stringify!($fn_name),
                "(\"/some/route\").on(&router), \"success\");
assert_not_handled!(",
                stringify!($fn_name),
                "(\"/other/route\").on(&router));
```
"
            )
        );
    };

    ($fn_name:ident, $method:ident, $doc_comment:expr) => {
        #[doc = $doc_comment]
        pub fn $fn_name<R>(mut self, path: R, handler: impl Handler) -> Self
        where
            R: TryInto<RouteSpec>,
            R::Error: Debug,
        {
            self.add(path, Method::$method, handler);
            self
        }
    };
}

impl Router {
    /**
    Constructs a new Router. This is often used with [`Router::get`],
    [`Router::post`], [`Router::put`], [`Router::delete`], and
    [`Router::patch`] chainable methods to build up an application.

    For an alternative way of constructing a Router, see [`Router::build`]

    ```
    # use trillium::Conn;
    # use trillium_router::Router;

    let router = Router::new()
        .get("/", |conn: Conn| async move { conn.ok("you have reached the index") })
        .get("/some/:param", |conn: Conn| async move { conn.ok("you have reached /some/:param") })
        .post("/", |conn: Conn| async move { conn.ok("post!") });

    use trillium_testing::prelude::*;
    assert_ok!(get("/").on(&router), "you have reached the index");
    assert_ok!(get("/some/route").on(&router), "you have reached /some/:param");
    assert_ok!(post("/").on(&router), "post!");
    ```
     */
    pub fn new() -> Self {
        Self::default()
    }

    /**
    Disable the default behavior of responding to OPTIONS requests
    with the supported methods at a given path
    */
    pub fn without_options_handling(mut self) -> Self {
        self.set_options_handling(false);
        self
    }

    /**
    enable or disable the router's behavior of responding to OPTIONS requests with the supported methods at given path.

    default: enabled
     */
    pub(crate) fn set_options_handling(&mut self, options_enabled: bool) {
        self.handle_options = options_enabled;
    }

    /**
    Another way to build a router, if you don't like the chainable
    interface described in [`Router::new`]. Note that the argument to
    the closure is a [`RouterRef`].

    ```
    # use trillium::Conn;
    # use trillium_router::Router;
    let router = Router::build(|mut router| {
        router.get("/", |conn: Conn| async move {
            conn.ok("you have reached the index")
        });

        router.get("/some/:paramroute", |conn: Conn| async move {
            conn.ok("you have reached /some/:param")
        });

        router.post("/", |conn: Conn| async move {
            conn.ok("post!")
        });
    });


    use trillium_testing::prelude::*;
    assert_ok!(get("/").on(&router), "you have reached the index");
    assert_ok!(get("/some/route").on(&router), "you have reached /some/:param");
    assert_ok!(post("/").on(&router), "post!");
    ```
    */
    pub fn build(builder: impl Fn(RouterRef)) -> Router {
        let mut router = Router::new();
        builder(RouterRef::new(&mut router));
        router
    }

    fn best_match<'a, 'b>(
        &'a self,
        method: Method,
        path: &'b str,
    ) -> Option<Match<'a, 'b, (MethodSelection, Box<dyn Handler>)>> {
        self.routefinder.best_match(method, path)
    }

    /**
    Registers a handler for a method other than get, put, post, patch, or delete.

    ```
    # use trillium::{Conn, Method};
    # use trillium_router::Router;
    let router = Router::new()
        .with_route("OPTIONS", "/some/route", |conn: Conn| async move { conn.ok("directly handling options") })
        .with_route(Method::Checkin, "/some/route", |conn: Conn| async move { conn.ok("checkin??") });

    use trillium_testing::{prelude::*, TestConn};
    assert_ok!(TestConn::build(Method::Options, "/some/route", ()).on(&router), "directly handling options");
    assert_ok!(TestConn::build("checkin", "/some/route", ()).on(&router), "checkin??");
    ```
    */
    pub fn with_route<M, R>(mut self, method: M, path: R, handler: impl Handler) -> Self
    where
        M: TryInto<Method>,
        <M as TryInto<Method>>::Error: Debug,
        R: TryInto<RouteSpec>,
        R::Error: Debug,
    {
        self.add(path, method.try_into().unwrap(), handler);
        self
    }

    pub(crate) fn add<R>(&mut self, path: R, method: Method, handler: impl Handler)
    where
        R: TryInto<RouteSpec>,
        R::Error: Debug,
    {
        self.routefinder.add(method, path, handler);
    }

    pub(crate) fn add_any<R>(&mut self, methods: &[Method], path: R, handler: impl Handler)
    where
        R: TryInto<RouteSpec>,
        R::Error: Debug,
    {
        self.routefinder.add(methods, path, handler)
    }

    pub(crate) fn add_all<R>(&mut self, path: R, handler: impl Handler)
    where
        R: TryInto<RouteSpec>,
        R::Error: Debug,
    {
        self.routefinder.add((), path, handler);
    }

    /**
    Appends the handler to all (get, post, put, delete, and patch) methods.
    ```
    # use trillium::Conn;
    # use trillium_router::Router;
    let router = Router::new().all("/any", |conn: Conn| async move {
        let response = format!("you made a {} request to /any", conn.method());
        conn.ok(response)
    });

    use trillium_testing::prelude::*;
    assert_ok!(get("/any").on(&router), "you made a GET request to /any");
    assert_ok!(post("/any").on(&router), "you made a POST request to /any");
    assert_ok!(delete("/any").on(&router), "you made a DELETE request to /any");
    assert_ok!(patch("/any").on(&router), "you made a PATCH request to /any");
    assert_ok!(put("/any").on(&router), "you made a PUT request to /any");

    assert_not_handled!(get("/").on(&router));
    ```
    */
    pub fn all<R>(mut self, path: R, handler: impl Handler) -> Self
    where
        R: TryInto<RouteSpec>,
        R::Error: Debug,
    {
        self.add_all(path, handler);
        self
    }

    /**
    Appends the handler to each of the provided http methods.
    ```
    # use trillium::Conn;
    # use trillium_router::Router;
    let router = Router::new().any(&["get", "post"], "/get_or_post", |conn: Conn| async move {
        let response = format!("you made a {} request to /get_or_post", conn.method());
        conn.ok(response)
    });

    use trillium_testing::prelude::*;
    assert_ok!(get("/get_or_post").on(&router), "you made a GET request to /get_or_post");
    assert_ok!(post("/get_or_post").on(&router), "you made a POST request to /get_or_post");
    assert_not_handled!(delete("/any").on(&router));
    assert_not_handled!(patch("/any").on(&router));
    assert_not_handled!(put("/any").on(&router));
    assert_not_handled!(get("/").on(&router));
    ```
    */
    pub fn any<IntoMethod, R>(
        mut self,
        methods: &[IntoMethod],
        path: R,
        handler: impl Handler,
    ) -> Self
    where
        IntoMethod: TryInto<Method> + Clone,
        <IntoMethod as TryInto<Method>>::Error: Debug,
        R: TryInto<RouteSpec>,
        R::Error: Debug,
    {
        let methods = methods
            .iter()
            .cloned()
            .map(|m| m.try_into().unwrap())
            .collect::<Vec<_>>();
        self.add_any(&methods, path, handler);
        self
    }

    method!(get, Get);
    method!(post, Post);
    method!(put, Put);
    method!(delete, Delete);
    method!(patch, Patch);
}

#[async_trait]
impl Handler for Router {
    async fn run(&self, mut conn: Conn) -> Conn {
        let method = conn.method();
        let original_captures = conn.take_state::<Captures>();
        let path = conn.path();
        let mut has_path = false;

        if let Some(m) = self.best_match(conn.method(), path) {
            let mut captures = m.captures().into_owned();

            let route = m.route().clone();

            if let Some(mut original_captures) = original_captures {
                original_captures.append(captures);
                captures = original_captures;
            }

            log::debug!("running {}: {}", m.route(), m.1.name());
            let mut new_conn = m
                .handler()
                .1
                .run({
                    if let Some(wildcard) = captures.wildcard() {
                        conn.push_path(String::from(wildcard));
                        has_path = true;
                    }

                    conn.with_state(captures).with_state(route)
                })
                .await;

            if has_path {
                new_conn.pop_path();
            }
            new_conn
        } else if method == Method::Options && self.handle_options {
            let allow = self
                .routefinder
                .methods_matching(path)
                .iter()
                .map(|m| m.as_ref())
                .collect::<Vec<_>>()
                .join(", ");

            return conn
                .with_header(KnownHeaderName::Allow, allow)
                .with_status(200)
                .halt();
        } else {
            log::debug!("{} did not match any route", conn.path());
            conn
        }
    }

    async fn before_send(&self, conn: Conn) -> Conn {
        let path = conn.path();
        if let Some(m) = self.best_match(conn.method(), path) {
            m.handler().1.before_send(conn).await
        } else {
            conn
        }
    }

    fn has_upgrade(&self, upgrade: &Upgrade) -> bool {
        if let Some(m) = self.best_match(*upgrade.method(), upgrade.path()) {
            m.1.has_upgrade(upgrade)
        } else {
            false
        }
    }

    async fn upgrade(&self, upgrade: Upgrade) {
        self.best_match(*upgrade.method(), upgrade.path())
            .unwrap()
            .handler()
            .1
            .upgrade(upgrade)
            .await
    }

    fn name(&self) -> std::borrow::Cow<'static, str> {
        "Router".into()
    }

    async fn init(&mut self, info: &mut Info) {
        // This code is not what a reader would expect, so here's a
        // brief explanation:
        //
        // Currently, the init trait interface must return a Send
        // future because that's the default for async-trait. We don't
        // actually need it to be Send, but changing that would be a
        // semver-minor trillium release.
        //
        // Mutable map iterators are not Send, and because we need to
        // hold that data across await boundaries, we cannot mutate in
        // place.
        //
        // However, because this is only called once at app boot, and
        // because we have &mut self, it is safe to move the router
        // contents into this future and then replace it, and the
        // performance impacts of doing so are unimportant as it is
        // part of app boot.
        let routefinder = mem::take(&mut self.routefinder);
        for (route, (methods, mut handler)) in routefinder.0 {
            handler.init(info).await;
            self.routefinder.add(methods, route, handler);
        }
    }
}

impl Debug for Router {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.write_str("Router ")?;
        let mut set = f.debug_set();

        for (route, (methods, handler)) in &self.routefinder.0 {
            set.entry(&format_args!("{} {} -> {}", methods, route, handler.name()));
        }
        set.finish()
    }
}