4 releases

0.2.2 Nov 18, 2024
0.2.1 Nov 18, 2024
0.2.0 Aug 23, 2024
0.1.0 Aug 23, 2024

#2075 in Procedural macros

Download history 24/week @ 2024-08-26 40/week @ 2024-09-16 44/week @ 2024-09-23 81/week @ 2024-09-30 1/week @ 2024-10-28 355/week @ 2024-11-18 6/week @ 2024-11-25 9/week @ 2024-12-02 8/week @ 2024-12-09

378 downloads per month
Used in route_match

Apache-2.0

16KB
381 lines

Route Macros

This is a procedural macro for implementing routing in hyper.

Usage


route! {
    match request {
        GET   /foo/bar => get_fubar(request)
        POST   /foo/:id => post_foo(request, id)
    }
}

This expands to:


  {
      let method = request.method.clone();
      let path: Vec<&str> = request.path.clone().split('/').collect();
      if let Some(()) = {
          if &method != &http::Method::GET {
              None
          } else if path.len() != PATH_LENGTH {
              None
          } else if path[0] != "foo" || path[1] != "bar" {
              None
          } else {
              Some(())
          }
      } {
          get_fubar(request)
      } else if let Some(id) = {
          if &method != &http::Method::POST {
              None
          } else if path.len() != PATH_LENGTH {
              None
          } else if path[0] != "foo" {
              None
          } else {
              let id = path[1];
              Some(id)
          }
      } {
          post_foo(request, id)
      }

  }

Dependencies

~230–680KB
~16K SLoC