spin-sdk 5.2.0

The Spin Rust SDK makes it easy to build Spin components in Rust.
Documentation
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
// This router implementation is heavily inspired by the `Endpoint` type in the https://github.com/http-rs/tide project.

use super::conversions::{IntoResponse, TryFromRequest, TryIntoRequest};
use super::{responses, Method, Request, Response};
use async_trait::async_trait;
use routefinder::{Captures, Router as MethodRouter};
use std::future::Future;
use std::{collections::HashMap, fmt::Display};

/// An HTTP request handler.
///  
/// This trait is automatically implemented for `Fn` types, and so is rarely implemented
/// directly by Spin users.
#[async_trait(?Send)]
pub trait Handler {
    /// Invoke the handler.
    async fn handle(&self, req: Request, params: Params) -> Response;
}

#[async_trait(?Send)]
impl Handler for Box<dyn Handler> {
    async fn handle(&self, req: Request, params: Params) -> Response {
        self.as_ref().handle(req, params).await
    }
}

#[async_trait(?Send)]
impl<F, Fut> Handler for F
where
    F: Fn(Request, Params) -> Fut + 'static,
    Fut: Future<Output = Response> + 'static,
{
    async fn handle(&self, req: Request, params: Params) -> Response {
        let fut = (self)(req, params);
        fut.await
    }
}

/// Route parameters extracted from a URI that match a route pattern.
pub type Params = Captures<'static, 'static>;

/// Routes HTTP requests within a Spin component.
///
/// Routes may contain wildcards:
///
/// * `:name` is a single segment wildcard. The handler can retrieve it using
///   [Params::get()].
/// * `*` is a trailing wildcard (matches anything). The handler can retrieve it
///   using [Params::wildcard()].
///
/// If a request matches more than one route, the match is selected according to the follow criteria:
///
/// * An exact route takes priority over any wildcard.
/// * A single segment wildcard takes priority over a trailing wildcard.
///
/// (This is the same logic as overlapping routes in the Spin manifest.)
///
/// # Examples
///
/// Handle GET requests to a path with a wildcard, falling back to "not found":
///
/// ```no_run
/// # use spin_sdk::http::{IntoResponse, Params, Request, Response, Router};
/// fn handle_route(req: Request) -> Response {
///     let mut router = Router::new();
///     router.get("/hello/:planet", hello_planet);
///     router.any("/*", not_found);
///     router.handle(req)
/// }
///
/// fn hello_planet(req: Request, params: Params) -> anyhow::Result<Response> {
///     let planet = params.get("planet").unwrap_or("world");
///     Ok(Response::new(200, format!("hello, {planet}")))
/// }
///
/// fn not_found(req: Request, params: Params) -> anyhow::Result<Response> {
///     Ok(Response::new(404, "not found"))
/// }
/// ```
///
/// Handle requests using a mix of synchronous and asynchronous handlers:
///
/// ```no_run
/// # use spin_sdk::http::{IntoResponse, Params, Request, Response, Router};
/// fn handle_route(req: Request) -> Response {
///     let mut router = Router::new();
///     router.get("/hello/:planet", hello_planet);
///     router.get_async("/goodbye/:planet", goodbye_planet);
///     router.handle(req)
/// }
///
/// fn hello_planet(req: Request, params: Params) -> anyhow::Result<Response> {
///     todo!()
/// }
///
/// async fn goodbye_planet(req: Request, params: Params) -> anyhow::Result<Response> {
///     todo!()
/// }
/// ```
///
/// Route differently according to HTTP method:
///
/// ```no_run
/// # use spin_sdk::http::{IntoResponse, Params, Request, Response, Router};
/// fn handle_route(req: Request) -> Response {
///     let mut router = Router::new();
///     router.get("/user", list_users);
///     router.post("/user", create_user);
///     router.get("/user/:id", get_user);
///     router.put("/user/:id", update_user);
///     router.delete("/user/:id", delete_user);
///     router.any("/user", method_not_allowed);
///     router.any("/user/:id", method_not_allowed);
///     router.handle(req)
/// }
/// # fn list_users(req: Request, params: Params) -> anyhow::Result<Response> { todo!() }
/// # fn create_user(req: Request, params: Params) -> anyhow::Result<Response> { todo!() }
/// # fn get_user(req: Request, params: Params) -> anyhow::Result<Response> { todo!() }
/// # fn update_user(req: Request, params: Params) -> anyhow::Result<Response> { todo!() }
/// # fn delete_user(req: Request, params: Params) -> anyhow::Result<Response> { todo!() }
/// # fn method_not_allowed(req: Request, params: Params) -> anyhow::Result<Response> { todo!() }
/// ```
///
/// Run the handler asynchronously:
///
/// ```no_run
/// # use spin_sdk::http::{IntoResponse, Params, Request, Response, Router};
/// async fn handle_route(req: Request) -> Response {
///     let mut router = Router::new();
///     router.get_async("/user", list_users);
///     router.post_async("/user", create_user);
///     router.handle_async(req).await
/// }
/// # async fn list_users(req: Request, params: Params) -> anyhow::Result<Response> { todo!() }
/// # async fn create_user(req: Request, params: Params) -> anyhow::Result<Response> { todo!() }
/// ```
///
/// Priority when routes overlap:
///
/// ```no_run
/// # use spin_sdk::http::{IntoResponse, Params, Request, Response, Router};
/// fn handle_route(req: Request) -> Response {
///     let mut router = Router::new();
///     router.any("/*", handle_any);
///     router.any("/:seg", handle_single_segment);
///     router.any("/fie", handle_exact);
///
///     // '/fie' is routed to `handle_exact`
///     // '/zounds' is routed to `handle_single_segment`
///     // '/zounds/fie' is routed to `handle_any`
///     router.handle(req)
/// }
/// # fn handle_any(req: Request, params: Params) -> anyhow::Result<Response> { todo!() }
/// # fn handle_single_segment(req: Request, params: Params) -> anyhow::Result<Response> { todo!() }
/// # fn handle_exact(req: Request, params: Params) -> anyhow::Result<Response> { todo!() }
/// ```
///
/// Route based on the trailing segment of a Spin wildcard route, instead of on the full path
///
/// ```no_run
/// // spin.toml
/// //
/// // [[trigger.http]]
/// // route = "/shop/..."
///
/// // component
/// # use spin_sdk::http::{IntoResponse, Params, Request, Response, Router};
/// fn handle_route(req: Request) -> Response {
///     let mut router = Router::suffix();
///     router.any("/users/*", handle_users);
///     router.any("/products/*", handle_products);
///     router.handle(req)
/// }
///
/// // '/shop/users/1' is routed to `handle_users`
/// // '/shop/products/1' is routed to `handle_products`
/// # fn handle_users(req: Request, params: Params) -> anyhow::Result<Response> { todo!() }
/// # fn handle_products(req: Request, params: Params) -> anyhow::Result<Response> { todo!() }
/// ```
pub struct Router {
    methods_map: HashMap<Method, MethodRouter<Box<dyn Handler>>>,
    any_methods: MethodRouter<Box<dyn Handler>>,
    route_on: RouteOn,
}

/// Describes what part of the path the Router will route on.
enum RouteOn {
    /// The router will route on the full path.
    FullPath,
    /// The router expects the component to be handling a route with a trailing wildcard
    /// (e.g. `route = /shop/...`), and will route on the trailing segment.
    Suffix,
}

impl Default for Router {
    fn default() -> Router {
        Router::new()
    }
}

impl Display for Router {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "Registered routes:")?;
        for (method, router) in &self.methods_map {
            for route in router.iter() {
                writeln!(f, "- {}: {}", method, route.0)?;
            }
        }
        Ok(())
    }
}

struct RouteMatch<'a> {
    params: Captures<'static, 'static>,
    handler: &'a dyn Handler,
}

impl Router {
    /// Synchronously dispatches a request to the appropriate handler along with the URI parameters.
    pub fn handle<R>(&self, request: R) -> Response
    where
        R: TryIntoRequest,
        R::Error: IntoResponse,
    {
        crate::http::executor::run(self.handle_async(request))
    }

    /// Asynchronously dispatches a request to the appropriate handler along with the URI parameters.
    pub async fn handle_async<R>(&self, request: R) -> Response
    where
        R: TryIntoRequest,
        R::Error: IntoResponse,
    {
        let request = match R::try_into_request(request) {
            Ok(r) => r,
            Err(e) => return e.into_response(),
        };
        let method = request.method.clone();
        let path = match self.route_on {
            RouteOn::FullPath => request.path(),
            RouteOn::Suffix => match trailing_suffix(&request) {
                Some(path) => path,
                None => {
                    eprintln!("Internal error: Router configured with suffix routing but trigger route has no trailing wildcard");
                    return responses::internal_server_error();
                }
            },
        };
        let RouteMatch { params, handler } = self.find(path, method);
        handler.handle(request, params).await
    }

    fn find(&self, path: &str, method: Method) -> RouteMatch<'_> {
        let best_match = self
            .methods_map
            .get(&method)
            .and_then(|r| r.best_match(path));

        if let Some(m) = best_match {
            let params = m.captures().into_owned();
            let handler = m.handler();
            return RouteMatch { handler, params };
        }

        let best_match = self.any_methods.best_match(path);

        match best_match {
            Some(m) => {
                let params = m.captures().into_owned();
                let handler = m.handler();
                RouteMatch { handler, params }
            }
            None if method == Method::Head => {
                // If it is a HTTP HEAD request then check if there is a callback in the methods map
                // if not then fallback to the behavior of HTTP GET else proceed as usual
                self.find(path, Method::Get)
            }
            None => {
                // Handle the failure case where no match could be resolved.
                self.fail(path, method)
            }
        }
    }

    // Helper function to handle the case where a best match couldn't be resolved.
    fn fail(&self, path: &str, method: Method) -> RouteMatch<'_> {
        // First, filter all routers to determine if the path can match but the provided method is not allowed.
        let is_method_not_allowed = self
            .methods_map
            .iter()
            .filter(|(k, _)| **k != method)
            .any(|(_, r)| r.best_match(path).is_some());

        if is_method_not_allowed {
            // If this `path` can be handled by a callback registered with a different HTTP method
            // should return 405 Method Not Allowed
            RouteMatch {
                handler: &method_not_allowed,
                params: Captures::default(),
            }
        } else {
            // ... Otherwise, nothing matched so 404.
            RouteMatch {
                handler: &not_found,
                params: Captures::default(),
            }
        }
    }

    /// Register a handler at the path for all methods.
    pub fn any<F, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Resp + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        let handler = move |req, params| {
            let res = TryFromRequest::try_from_request(req).map(|r| handler(r, params));
            async move {
                match res {
                    Ok(res) => res.into_response(),
                    Err(e) => e.into_response(),
                }
            }
        };

        self.any_async(path, handler)
    }

    /// Register an async handler at the path for all methods.
    pub fn any_async<F, Fut, I, O>(&mut self, path: &str, handler: F)
    where
        F: Fn(I, Params) -> Fut + 'static,
        Fut: Future<Output = O> + 'static,
        I: TryFromRequest + 'static,
        I::Error: IntoResponse + 'static,
        O: IntoResponse + 'static,
    {
        let handler = move |req, params| {
            let res = TryFromRequest::try_from_request(req).map(|r| handler(r, params));
            async move {
                match res {
                    Ok(f) => f.await.into_response(),
                    Err(e) => e.into_response(),
                }
            }
        };

        self.any_methods.add(path, Box::new(handler)).unwrap();
    }

    /// Register a handler at the path for the specified HTTP method.
    pub fn add<F, Req, Resp>(&mut self, path: &str, method: Method, handler: F)
    where
        F: Fn(Req, Params) -> Resp + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        let handler = move |req, params| {
            let res = TryFromRequest::try_from_request(req).map(|r| handler(r, params));
            async move {
                match res {
                    Ok(res) => res.into_response(),
                    Err(e) => e.into_response(),
                }
            }
        };

        self.add_async(path, method, handler)
    }

    /// Register an async handler at the path for the specified HTTP method.
    pub fn add_async<F, Fut, I, O>(&mut self, path: &str, method: Method, handler: F)
    where
        F: Fn(I, Params) -> Fut + 'static,
        Fut: Future<Output = O> + 'static,
        I: TryFromRequest + 'static,
        I::Error: IntoResponse + 'static,
        O: IntoResponse + 'static,
    {
        let handler = move |req, params| {
            let res = TryFromRequest::try_from_request(req).map(|r| handler(r, params));
            async move {
                match res {
                    Ok(f) => f.await.into_response(),
                    Err(e) => e.into_response(),
                }
            }
        };

        self.methods_map
            .entry(method)
            .or_default()
            .add(path, Box::new(handler))
            .unwrap();
    }

    /// Register a handler at the path for the HTTP GET method.
    pub fn get<F, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Resp + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add(path, Method::Get, handler)
    }

    /// Register an async handler at the path for the HTTP GET method.
    pub fn get_async<F, Fut, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Fut + 'static,
        Fut: Future<Output = Resp> + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add_async(path, Method::Get, handler)
    }

    /// Register a handler at the path for the HTTP HEAD method.
    pub fn head<F, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Resp + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add(path, Method::Head, handler)
    }

    /// Register an async handler at the path for the HTTP HEAD method.
    pub fn head_async<F, Fut, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Fut + 'static,
        Fut: Future<Output = Resp> + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add_async(path, Method::Head, handler)
    }

    /// Register a handler at the path for the HTTP POST method.
    pub fn post<F, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Resp + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add(path, Method::Post, handler)
    }

    /// Register an async handler at the path for the HTTP POST method.
    pub fn post_async<F, Fut, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Fut + 'static,
        Fut: Future<Output = Resp> + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add_async(path, Method::Post, handler)
    }

    /// Register a handler at the path for the HTTP DELETE method.
    pub fn delete<F, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Resp + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add(path, Method::Delete, handler)
    }

    /// Register an async handler at the path for the HTTP DELETE method.
    pub fn delete_async<F, Fut, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Fut + 'static,
        Fut: Future<Output = Resp> + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add_async(path, Method::Delete, handler)
    }

    /// Register a handler at the path for the HTTP PUT method.
    pub fn put<F, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Resp + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add(path, Method::Put, handler)
    }

    /// Register an async handler at the path for the HTTP PUT method.
    pub fn put_async<F, Fut, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Fut + 'static,
        Fut: Future<Output = Resp> + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add_async(path, Method::Put, handler)
    }

    /// Register a handler at the path for the HTTP PATCH method.
    pub fn patch<F, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Resp + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add(path, Method::Patch, handler)
    }

    /// Register an async handler at the path for the HTTP PATCH method.
    pub fn patch_async<F, Fut, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Fut + 'static,
        Fut: Future<Output = Resp> + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add_async(path, Method::Patch, handler)
    }

    /// Register a handler at the path for the HTTP OPTIONS method.
    pub fn options<F, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Resp + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add(path, Method::Options, handler)
    }

    /// Register an async handler at the path for the HTTP OPTIONS method.
    pub fn options_async<F, Fut, Req, Resp>(&mut self, path: &str, handler: F)
    where
        F: Fn(Req, Params) -> Fut + 'static,
        Fut: Future<Output = Resp> + 'static,
        Req: TryFromRequest + 'static,
        Req::Error: IntoResponse + 'static,
        Resp: IntoResponse + 'static,
    {
        self.add_async(path, Method::Options, handler)
    }

    /// Construct a new Router that matches on the full path.
    pub fn new() -> Self {
        Router {
            methods_map: HashMap::default(),
            any_methods: MethodRouter::new(),
            route_on: RouteOn::FullPath,
        }
    }

    /// Construct a new Router that matches on the trailing wildcard
    /// component of the route.
    pub fn suffix() -> Self {
        Router {
            methods_map: HashMap::default(),
            any_methods: MethodRouter::new(),
            route_on: RouteOn::Suffix,
        }
    }
}

async fn not_found(_req: Request, _params: Params) -> Response {
    responses::not_found()
}

async fn method_not_allowed(_req: Request, _params: Params) -> Response {
    responses::method_not_allowed()
}

fn trailing_suffix(req: &Request) -> Option<&str> {
    req.header("spin-path-info")
        .and_then(|path_info| path_info.as_str())
}

/// A macro to help with constructing a [`Router`] from method-pattern-handler triples.
///
/// # Examples
///
/// ```no_run
/// # use spin_sdk::http_router;
/// # use spin_sdk::http::{IntoResponse, Params, Request, Response, Router};
/// fn handle_route(req: Request) -> Response {
///     let router = http_router! {
///         GET "/user" => list_users,
///         POST "/user" => create_user,
///         GET "/user/:id" => get_user,
///         PUT "/user/:id" => update_user,
///         DELETE "/user/:id" => delete_user,
///         _ "/user" => method_not_allowed,
///         _ "/user/:id" => method_not_allowed
///     };
///     router.handle(req)
/// }
/// # fn list_users(req: Request, params: Params) -> anyhow::Result<Response> { todo!() }
/// # fn create_user(req: Request, params: Params) -> anyhow::Result<Response> { todo!() }
/// # fn get_user(req: Request, params: Params) -> anyhow::Result<Response> { todo!() }
/// # fn update_user(req: Request, params: Params) -> anyhow::Result<Response> { todo!() }
/// # fn delete_user(req: Request, params: Params) -> anyhow::Result<Response> { todo!() }
/// # fn method_not_allowed(req: Request, params: Params) -> anyhow::Result<Response> { todo!() }
/// ```
#[macro_export]
macro_rules! http_router {
    ($($method:tt $path:literal => $h:expr),*) => {
        {
            let mut router = $crate::http::Router::new();
            $(
                $crate::http_router!(@build router $method $path => $h);
            )*
            router
        }
    };
    (@build $r:ident HEAD $path:literal => $h:expr) => {
        $r.head($path, $h);
    };
    (@build $r:ident GET $path:literal => $h:expr) => {
        $r.get($path, $h);
    };
    (@build $r:ident PUT $path:literal => $h:expr) => {
        $r.put($path, $h);
    };
    (@build $r:ident POST $path:literal => $h:expr) => {
        $r.post($path, $h);
    };
    (@build $r:ident PATCH $path:literal => $h:expr) => {
        $r.patch($path, $h);
    };
    (@build $r:ident DELETE $path:literal => $h:expr) => {
        $r.delete($path, $h);
    };
    (@build $r:ident OPTIONS $path:literal => $h:expr) => {
        $r.options($path, $h);
    };
    (@build $r:ident _ $path:literal => $h:expr) => {
        $r.any($path, $h);
    };
}

#[cfg(test)]
mod tests {
    use super::*;

    fn make_request(method: Method, path: &str) -> Request {
        Request::new(method, path)
    }

    fn make_wildcard_request(method: Method, path: &str, trailing: &str) -> Request {
        let mut req = Request::new(method, path);
        req.set_header("spin-path-info", trailing);
        req
    }

    fn echo_param(_req: Request, params: Params) -> Response {
        match params.get("x") {
            Some(path) => Response::new(200, path),
            None => responses::not_found(),
        }
    }

    #[test]
    fn test_method_not_allowed() {
        let mut router = Router::default();
        router.get("/:x", echo_param);

        let req = make_request(Method::Post, "/foobar");
        let res = router.handle(req);
        assert_eq!(res.status, hyperium::StatusCode::METHOD_NOT_ALLOWED);
    }

    #[test]
    fn test_not_found() {
        fn h1(_req: Request, _params: Params) -> anyhow::Result<Response> {
            Ok(Response::new(200, ()))
        }

        let mut router = Router::default();
        router.get("/h1/:param", h1);

        let req = make_request(Method::Get, "/h1/");
        let res = router.handle(req);
        assert_eq!(res.status, hyperium::StatusCode::NOT_FOUND);
    }

    #[test]
    fn test_multi_param() {
        fn multiply(_req: Request, params: Params) -> anyhow::Result<Response> {
            let x: i64 = params.get("x").unwrap().parse()?;
            let y: i64 = params.get("y").unwrap().parse()?;
            Ok(Response::new(200, format!("{result}", result = x * y)))
        }

        let mut router = Router::default();
        router.get("/multiply/:x/:y", multiply);

        let req = make_request(Method::Get, "/multiply/2/4");
        let res = router.handle(req);

        assert_eq!(res.body, "8".to_owned().into_bytes());
    }

    #[test]
    fn test_param() {
        let mut router = Router::default();
        router.get("/:x", echo_param);

        let req = make_request(Method::Get, "/y");
        let res = router.handle(req);

        assert_eq!(res.body, "y".to_owned().into_bytes());
    }

    #[test]
    fn test_wildcard() {
        fn echo_wildcard(_req: Request, params: Params) -> Response {
            match params.wildcard() {
                Some(path) => Response::new(200, path),
                None => responses::not_found(),
            }
        }

        let mut router = Router::default();
        router.get("/*", echo_wildcard);

        let req = make_request(Method::Get, "/foo/bar");
        let res = router.handle(req);
        assert_eq!(res.status, hyperium::StatusCode::OK);
        assert_eq!(res.body, "foo/bar".to_owned().into_bytes());
    }

    #[test]
    fn test_wildcard_last_segment() {
        let mut router = Router::default();
        router.get("/:x/*", echo_param);

        let req = make_request(Method::Get, "/foo/bar");
        let res = router.handle(req);
        assert_eq!(res.body, "foo".to_owned().into_bytes());
    }

    #[test]
    fn test_spin_trailing_wildcard() {
        let mut router = Router::suffix();
        router.get("/:x/*", echo_param);

        let req = make_wildcard_request(Method::Get, "/base/baser/foo/bar", "/foo/bar");
        let res = router.handle(req);
        assert_eq!(res.body, "foo".to_owned().into_bytes());
    }

    #[test]
    fn test_router_display() {
        let mut router = Router::default();
        router.get("/:x", echo_param);

        let expected = "Registered routes:\n- GET: /:x\n";
        let actual = format!("{}", router);

        assert_eq!(actual.as_str(), expected);
    }

    #[test]
    fn test_ambiguous_wildcard_vs_star() {
        fn h1(_req: Request, _params: Params) -> anyhow::Result<Response> {
            Ok(Response::new(200, "one/two"))
        }

        fn h2(_req: Request, _params: Params) -> anyhow::Result<Response> {
            Ok(Response::new(200, "posts/*"))
        }

        let mut router = Router::default();
        router.get("/:one/:two", h1);
        router.get("/posts/*", h2);

        let req = make_request(Method::Get, "/posts/2");
        let res = router.handle(req);

        assert_eq!(res.body, "posts/*".to_owned().into_bytes());
    }
}