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
use std::{collections::HashMap, convert::Infallible};

use hyper::{
    body::Incoming,
    http::{Method, StatusCode},
};
use motore::{layer::Layer, service::Service};

use crate::{
    handler::{DynHandler, Handler},
    response::IntoResponse,
    DynService, HttpContext, Response,
};

// The `matchit::Router` cannot be converted to `Iterator`, so using
// `matchit::Router<MethodRouter>` is not convenient enough.
//
// To solve the problem, we refer to the implementation of `axum` and introduce a `RouteId` as a
// bridge, the `matchit::Router` only handles some IDs and each ID corresponds to a `MethodRouter`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct RouteId(u32);

impl RouteId {
    fn next() -> Self {
        use std::sync::atomic::{AtomicU32, Ordering};
        // `AtomicU64` isn't supported on all platforms
        static ID: AtomicU32 = AtomicU32::new(0);
        let id = ID.fetch_add(1, Ordering::Relaxed);
        if id == u32::MAX {
            panic!("Over `u32::MAX` routes created. If you need this, please file an issue.");
        }
        Self(id)
    }
}

pub struct Router<S = ()> {
    matcher: Matcher,
    routes: HashMap<RouteId, MethodRouter<S>>,
    fallback: Fallback<S>,
    is_default_fallback: bool,
}

impl<S> Router<S>
where
    S: Clone + Send + Sync + 'static,
{
    pub fn new() -> Self {
        Self {
            matcher: Default::default(),
            routes: Default::default(),
            fallback: Fallback::from_status_code(StatusCode::NOT_FOUND),
            is_default_fallback: true,
        }
    }

    pub fn route<R>(mut self, uri: R, route: MethodRouter<S>) -> Self
    where
        R: Into<String>,
    {
        let route_id = self
            .matcher
            .insert(uri)
            .expect("Insert routing rule failed");

        self.routes.insert(route_id, route);

        self
    }

    pub fn fallback_for_all(mut self, fallback: Fallback<S>) -> Self {
        self.fallback = fallback;
        self
    }

    pub fn merge(mut self, other: Router<S>) -> Self {
        let Router {
            mut matcher,
            mut routes,
            fallback,
            is_default_fallback,
        } = other;

        for (path, route_id) in matcher.matches.drain() {
            self.matcher
                .insert_with_id(path, route_id)
                .expect("Insert routing rule failed during merging router");
        }
        for (route_id, method_router) in routes.drain() {
            if self.routes.insert(route_id, method_router).is_some() {
                // Infallible
                panic!(
                    "Insert routes failed during merging router: Conflicting `RouteId`: \
                     {route_id:?}"
                );
            }
        }

        match (self.is_default_fallback, is_default_fallback) {
            (_, true) => {}
            (true, false) => {
                self.fallback = fallback;
                self.is_default_fallback = false;
            }
            (false, false) => {
                panic!("Merge `Router` failed because both `Router` have customized `fallback`")
            }
        }

        self
    }

    pub fn layer<L>(self, l: L) -> Self
    where
        L: Layer<DynService> + Clone + Send + Sync + 'static,
        L::Service: Service<HttpContext, Incoming, Response = Response, Error = Infallible>
            + Clone
            + Send
            + Sync
            + 'static,
        <L::Service as Service<HttpContext, Incoming>>::Response: Send + 'static,
        <L::Service as Service<HttpContext, Incoming>>::Error: Send + 'static,
    {
        let routes = self
            .routes
            .into_iter()
            .map(|(id, route)| {
                let route = route.layer(l.clone());
                (id, route)
            })
            .collect();

        let fallback = self.fallback.layer(l.clone());

        Router {
            matcher: self.matcher,
            routes,
            fallback,
            is_default_fallback: self.is_default_fallback,
        }
    }

    #[allow(dead_code)]
    fn with_state<S2>(self, s: S) -> Router<S2> {
        let routes = self
            .routes
            .into_iter()
            .map(|(id, route)| {
                let route = route.with_state(s.clone());
                (id, route)
            })
            .collect();

        let fallback = self.fallback.with_state(&s);

        Router {
            matcher: self.matcher,
            routes,
            fallback,
            is_default_fallback: self.is_default_fallback,
        }
    }
}

impl Service<HttpContext, Incoming> for Router<()> {
    type Response = Response;

    type Error = Infallible;

    async fn call<'s, 'cx>(
        &'s self,
        cx: &'cx mut HttpContext,
        req: Incoming,
    ) -> Result<Self::Response, Self::Error> {
        if let Ok(matched) = self.matcher.at(cx.uri.path()) {
            if let Some(srv) = self.routes.get(matched.value) {
                cx.params = matched.params.into();
                return srv.call_with_state(cx, req, ()).await;
            }
        }

        self.fallback.call_with_state(cx, req, ()).await
    }
}

#[derive(Default)]
struct Matcher {
    matches: HashMap<String, RouteId>,
    router: matchit::Router<RouteId>,
}

impl Matcher {
    fn insert<R>(&mut self, uri: R) -> Result<RouteId, MatcherError>
    where
        R: Into<String>,
    {
        let route_id = RouteId::next();
        self.insert_with_id(uri, route_id)?;
        Ok(route_id)
    }

    fn insert_with_id<R>(&mut self, uri: R, route_id: RouteId) -> Result<(), MatcherError>
    where
        R: Into<String>,
    {
        let uri = uri.into();
        if self.matches.insert(uri.clone(), route_id).is_some() {
            return Err(MatcherError::UriConflict(uri));
        }
        let _ = self
            .router
            .insert(uri, route_id)
            .map_err(MatcherError::RouterInsertError)?;
        Ok(())
    }

    fn at<'a>(&'a self, path: &'a str) -> Result<matchit::Match<&RouteId>, MatcherError> {
        self.router.at(path).map_err(MatcherError::RouterMatchError)
    }
}

#[derive(Debug)]
enum MatcherError {
    UriConflict(String),
    RouterInsertError(matchit::InsertError),
    RouterMatchError(matchit::MatchError),
}

pub struct MethodRouter<S = ()> {
    options: MethodEndpoint<S>,
    get: MethodEndpoint<S>,
    post: MethodEndpoint<S>,
    put: MethodEndpoint<S>,
    delete: MethodEndpoint<S>,
    head: MethodEndpoint<S>,
    trace: MethodEndpoint<S>,
    connect: MethodEndpoint<S>,
    patch: MethodEndpoint<S>,
    fallback: Fallback<S>,
}

impl<S> MethodRouter<S>
where
    S: Clone + Send + Sync + 'static,
{
    pub fn new() -> Self {
        Self {
            options: MethodEndpoint::None,
            get: MethodEndpoint::None,
            post: MethodEndpoint::None,
            put: MethodEndpoint::None,
            delete: MethodEndpoint::None,
            head: MethodEndpoint::None,
            trace: MethodEndpoint::None,
            connect: MethodEndpoint::None,
            patch: MethodEndpoint::None,
            fallback: Fallback::from_status_code(StatusCode::METHOD_NOT_ALLOWED),
        }
    }

    pub fn builder() -> MethodRouterBuilder<S> {
        MethodRouterBuilder {
            router: Self::new(),
        }
    }

    pub fn layer<L>(self, l: L) -> Self
    where
        L: Layer<DynService> + Clone + Send + Sync + 'static,
        L::Service: Service<HttpContext, Incoming, Response = Response, Error = Infallible>
            + Clone
            + Send
            + Sync
            + 'static,
    {
        let Self {
            options,
            get,
            post,
            put,
            delete,
            head,
            trace,
            connect,
            patch,
            fallback,
        } = self;

        let layer_fn = move |route: DynService| DynService::new(l.clone().layer(route));

        let options = options.map(layer_fn.clone());
        let get = get.map(layer_fn.clone());
        let post = post.map(layer_fn.clone());
        let put = put.map(layer_fn.clone());
        let delete = delete.map(layer_fn.clone());
        let head = head.map(layer_fn.clone());
        let trace = trace.map(layer_fn.clone());
        let connect = connect.map(layer_fn.clone());
        let patch = patch.map(layer_fn.clone());

        let fallback = fallback.map(layer_fn);

        Self {
            options,
            get,
            post,
            put,
            delete,
            head,
            trace,
            connect,
            patch,
            fallback,
        }
    }

    pub fn with_state<S2>(self, state: S) -> MethodRouter<S2> {
        MethodRouter {
            options: self.options.with_state(&state),
            get: self.get.with_state(&state),
            post: self.post.with_state(&state),
            put: self.put.with_state(&state),
            delete: self.delete.with_state(&state),
            head: self.head.with_state(&state),
            trace: self.trace.with_state(&state),
            connect: self.connect.with_state(&state),
            patch: self.patch.with_state(&state),
            fallback: self.fallback.with_state(&state),
        }
    }

    pub(crate) async fn call_with_state<'s, 'cx>(
        &'s self,
        cx: &'cx mut HttpContext,
        req: Incoming,
        state: S,
    ) -> Result<Response, Infallible>
    where
        S: 'cx,
    {
        let handler = match cx.method {
            Method::OPTIONS => Some(&self.options),
            Method::GET => Some(&self.get),
            Method::POST => Some(&self.post),
            Method::PUT => Some(&self.put),
            Method::DELETE => Some(&self.delete),
            Method::HEAD => Some(&self.head),
            Method::TRACE => Some(&self.trace),
            Method::CONNECT => Some(&self.connect),
            Method::PATCH => Some(&self.patch),
            _ => None,
        };

        match handler {
            Some(MethodEndpoint::Route(route)) => route.call(cx, req).await,
            Some(MethodEndpoint::Handler(handler)) => {
                handler.clone().call_with_state(cx, req, state).await
            }
            _ => self.fallback.call_with_state(cx, req, state).await,
        }
    }
}

macro_rules! for_all_methods {
    ($name:ident) => {
        $name!(options, get, post, put, delete, head, trace, connect, patch);
    };
}

pub struct MethodRouterBuilder<S> {
    router: MethodRouter<S>,
}

macro_rules! impl_method_register_for_builder {
    ($( $method:ident ),*) => {
        $(
        pub fn $method(mut self, ep: MethodEndpoint<S>) -> Self {
            self.router.$method = ep;
            self
        }
        )+
    };
}

impl<S> MethodRouterBuilder<S>
where
    S: Clone + Send + Sync + 'static,
{
    pub fn new() -> Self {
        Self {
            router: MethodRouter::new(),
        }
    }

    for_all_methods!(impl_method_register_for_builder);

    pub fn fallback<H, T>(mut self, handler: H) -> Self
    where
        for<'a> H: Handler<T, S> + Clone + Send + Sync + 'a,
        for<'a> T: 'a,
    {
        self.router.fallback = Fallback::Handler(DynHandler::new(handler));
        self
    }

    pub fn build(self) -> MethodRouter<S> {
        self.router
    }
}

macro_rules! impl_method_register {
    ($( $method:ident ),*) => {
        $(
        pub fn $method<H, T, S>(h: H) -> MethodRouter<S>
        where
            for<'a> H: Handler<T, S> + Clone + Send + Sync + 'a,
            for<'a> T: 'a,
            S: Clone + Send + Sync + 'static,
        {
            MethodRouterBuilder::new().$method(MethodEndpoint::from_handler(h)).build()
        }
        )+
    };
}

for_all_methods!(impl_method_register);

pub fn any<H, T, S>(h: H) -> MethodRouter<S>
where
    for<'a> H: Handler<T, S> + Clone + Send + Sync + 'a,
    for<'a> T: 'a,
    S: Clone + Send + Sync + 'static,
{
    MethodRouterBuilder::new().fallback(h).build()
}

#[derive(Clone, Default)]
pub enum MethodEndpoint<S> {
    #[default]
    None,
    Route(DynService),
    Handler(DynHandler<S>),
}

impl<S> MethodEndpoint<S>
where
    S: Clone + Send + Sync + 'static,
{
    pub fn from_handler<H, T>(h: H) -> MethodEndpoint<S>
    where
        for<'a> H: Handler<T, S> + Clone + Send + Sync + 'a,
        for<'a> T: 'a,
        S: Clone + Send + Sync + 'static,
    {
        MethodEndpoint::Handler(DynHandler::new(h))
    }

    pub fn from_service<Srv>(srv: Srv) -> MethodEndpoint<S>
    where
        Srv: Service<HttpContext, Incoming, Response = Response, Error = Infallible>
            + Clone
            + Send
            + Sync
            + 'static,
    {
        MethodEndpoint::Route(DynService::new(srv))
    }

    pub(crate) fn map<F>(self, f: F) -> MethodEndpoint<S>
    where
        F: FnOnce(DynService) -> DynService + Clone + 'static,
    {
        match self {
            MethodEndpoint::None => MethodEndpoint::None,
            MethodEndpoint::Route(route) => MethodEndpoint::Route(f(route)),
            MethodEndpoint::Handler(handler) => MethodEndpoint::Handler(handler.map(f)),
        }
    }

    pub(crate) fn with_state<S2>(self, state: &S) -> MethodEndpoint<S2> {
        match self {
            MethodEndpoint::None => MethodEndpoint::None,
            MethodEndpoint::Route(route) => MethodEndpoint::Route(route),
            MethodEndpoint::Handler(handler) => {
                MethodEndpoint::Route(handler.into_route(state.clone()))
            }
        }
    }
}

#[derive(Clone)]
pub enum Fallback<S> {
    Route(DynService),
    Handler(DynHandler<S>),
}

impl<S> Fallback<S>
where
    S: Clone + Send + Sync + 'static,
{
    pub(crate) fn from_status_code(status: StatusCode) -> Fallback<S> {
        Fallback::Route(DynService::new(RouteForStatusCode(status)))
    }

    pub fn from_handler<H, T>(h: H) -> Fallback<S>
    where
        for<'a> H: Handler<T, S> + Clone + Send + Sync + 'a,
        for<'a> T: 'a,
        S: Clone + Send + Sync + 'static,
    {
        Fallback::Handler(DynHandler::new(h))
    }

    pub fn from_service<Srv>(srv: Srv) -> Fallback<S>
    where
        Srv: Service<HttpContext, Incoming, Response = Response, Error = Infallible>
            + Clone
            + Send
            + Sync
            + 'static,
    {
        Fallback::Route(DynService::new(srv))
    }

    pub(crate) fn map<F>(self, f: F) -> Fallback<S>
    where
        F: FnOnce(DynService) -> DynService + Clone + 'static,
    {
        match self {
            Fallback::Route(route) => Fallback::Route(f(route)),
            Fallback::Handler(handler) => Fallback::Handler(handler.map(f)),
        }
    }

    pub(crate) fn layer<L>(self, l: L) -> Self
    where
        L: Layer<DynService> + Clone + Send + Sync + 'static,
        L::Service: Service<HttpContext, Incoming, Response = Response, Error = Infallible>
            + Clone
            + Send
            + Sync
            + 'static,
    {
        self.map(move |route: DynService| DynService::new(l.clone().layer(route)))
    }

    pub(crate) fn with_state<S2>(self, state: &S) -> Fallback<S2> {
        match self {
            Fallback::Route(route) => Fallback::Route(route),
            Fallback::Handler(handler) => Fallback::Route(handler.into_route(state.clone())),
        }
    }

    pub(crate) async fn call_with_state<'s, 'cx>(
        &'s self,
        cx: &'cx mut HttpContext,
        req: Incoming,
        state: S,
    ) -> Result<Response, Infallible>
    where
        S: 'cx,
    {
        match self {
            Fallback::Route(route) => route.call(cx, req).await,
            Fallback::Handler(handler) => handler.clone().call_with_state(cx, req, state).await,
        }
    }
}

pub fn from_handler<H, T, S>(h: H) -> MethodEndpoint<S>
where
    for<'a> H: Handler<T, S> + Clone + Send + Sync + 'a,
    for<'a> T: 'a,
    S: Clone + Send + Sync + 'static,
{
    MethodEndpoint::from_handler(h)
}

pub fn from_service<Srv, S>(srv: Srv) -> MethodEndpoint<S>
where
    Srv: Service<HttpContext, Incoming, Response = Response, Error = Infallible>
        + Clone
        + Send
        + Sync
        + 'static,
    S: Clone + Send + Sync + 'static,
{
    MethodEndpoint::from_service(srv)
}

pub fn service_fn<F>(f: F) -> MethodEndpoint<()>
where
    F: for<'r> crate::service_fn::Callback<'r> + Clone + Send + Sync + 'static,
{
    MethodEndpoint::from_service(crate::service_fn::service_fn(f))
}

#[derive(Clone)]
struct RouteForStatusCode(StatusCode);

impl Service<HttpContext, Incoming> for RouteForStatusCode {
    type Response = Response;
    type Error = Infallible;

    async fn call<'s, 'cx>(
        &'s self,
        _cx: &'cx mut HttpContext,
        _req: Incoming,
    ) -> Result<Self::Response, Self::Error> {
        Ok(self.0.into_response())
    }
}