2个不稳定版本

新版本 0.2.0 2024年8月23日
0.1.0 2024年8月23日

#517 in 过程宏


用于 route_match

Apache-2.0

15KB
380

路由宏

这是一个用于在hyper中实现路由的过程宏。

用法


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

这将扩展为


  {
      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)
      }

  }

依赖项

~270–720KB
~17K SLoC