diff --git a/examples/forms/src/main.rs b/examples/forms/src/main.rs index 80c281f7..d60c5f6c 100644 --- a/examples/forms/src/main.rs +++ b/examples/forms/src/main.rs @@ -15,7 +15,7 @@ struct UserLogin<'r> { age: Result, } -#[route(POST, path = "/login", form = "")] +#[POST(path = "/login", form = "")] fn login(user: UserLogin) -> Result { if user.age.is_err() { let input = user.age.unwrap_err(); @@ -36,7 +36,7 @@ fn login(user: UserLogin) -> Result { } } -#[route(GET, path = "/user/")] +#[GET(path = "/user/")] fn user_page(username: &str) -> String { format!("This is {}'s page.", username) } diff --git a/examples/hello_person/src/main.rs b/examples/hello_person/src/main.rs index de507481..adb9e88c 100644 --- a/examples/hello_person/src/main.rs +++ b/examples/hello_person/src/main.rs @@ -4,7 +4,7 @@ extern crate rocket; use rocket::Rocket; -#[route(GET, path = "/hello//")] +#[GET(path = "/hello//")] fn hello(name: &str, age: i8) -> String { format!("Hello, {} year old named {}!", age, name) } diff --git a/examples/hello_world/src/main.rs b/examples/hello_world/src/main.rs index a1e7d9d5..f84f8dc2 100644 --- a/examples/hello_world/src/main.rs +++ b/examples/hello_world/src/main.rs @@ -4,7 +4,7 @@ extern crate rocket; use rocket::Rocket; -#[route(GET, path = "/")] +#[GET(path = "/")] fn root() -> &'static str { "Hello, world!" } diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 37b467dc..8dfa731b 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -21,7 +21,7 @@ use syntax::parse::token::intern; use routes_macro::routes_macro; use errors_macro::errors_macro; -use route_decorator::route_decorator; +use route_decorator::*; use error_decorator::error_decorator; use derive_form::from_form_derive; @@ -30,16 +30,32 @@ const CATCH_STRUCT_PREFIX: &'static str = "static_rocket_catch_info_for_"; const ROUTE_FN_PREFIX: &'static str = "rocket_route_fn_"; const CATCH_FN_PREFIX: &'static str = "rocket_catch_fn_"; +macro_rules! register_decorators { + ($registry:expr, $($name:expr => $func:expr),+) => ( + $($registry.register_syntax_extension(intern($name), + SyntaxExtension::MultiDecorator(Box::new($func))); + )+ + ) +} + #[plugin_registrar] pub fn plugin_registrar(reg: &mut Registry) { - reg.register_syntax_extension(intern("route"), - SyntaxExtension::MultiDecorator(Box::new(route_decorator))); - reg.register_syntax_extension(intern("error"), - SyntaxExtension::MultiDecorator(Box::new(error_decorator))); - reg.register_syntax_extension(intern("derive_FromForm"), - SyntaxExtension::MultiDecorator(Box::new(from_form_derive))); reg.register_macro("routes", routes_macro); reg.register_macro("errors", errors_macro); -// reg.register_macro("GET", get_macro); + register_decorators!(reg, + "derive_FromForm" => from_form_derive, + "route" => generic_route_decorator, + "error" => error_decorator, + + "GET" => get_decorator, + "PUT" => put_decorator, + "POST" => post_decorator, + "DELETE" => delete_decorator, + "OPTIONS" => options_decorator, + "HEAD" => head_decorator, + "TRACE" => trace_decorator, + "CONNECT" => connect_decorator, + "PATCH" => patch_decorator + ); } diff --git a/macros/src/route_decorator.rs b/macros/src/route_decorator.rs index 5bfea030..3feeb704 100644 --- a/macros/src/route_decorator.rs +++ b/macros/src/route_decorator.rs @@ -130,14 +130,15 @@ fn method_variant_to_expr(ecx: &ExtCtxt, method: Method) -> P { } // FIXME: Compilation fails when parameters have the same name as the function! -pub fn route_decorator(ecx: &mut ExtCtxt, sp: Span, meta_item: &MetaItem, - annotated: &Annotatable, push: &mut FnMut(Annotatable)) { +pub fn route_decorator(known_method: Option>, ecx: &mut ExtCtxt, + sp: Span, meta_item: &MetaItem, annotated: &Annotatable, + push: &mut FnMut(Annotatable)) { // Get the encompassing item and function declaration for the annotated func. let parser = MetaItemParser::new(ecx, meta_item, annotated, &sp); let (item, fn_decl) = (parser.expect_item(), parser.expect_fn_decl()); // Parse and retrieve all of the parameters of the route. - let route = parser.parse_route(None); + let route = parser.parse_route(known_method); // Get a list of the user declared parameters in `path` and `form`. let path_params = extract_params_from_kv(&parser, &route.path); @@ -221,3 +222,31 @@ pub fn route_decorator(ecx: &mut ExtCtxt, sp: Span, meta_item: &MetaItem, ).unwrap())); } + +pub fn generic_route_decorator(ecx: &mut ExtCtxt, + sp: Span, meta_item: &MetaItem, annotated: &Annotatable, + push: &mut FnMut(Annotatable)) { + route_decorator(None, ecx, sp, meta_item, annotated, push); +} + +macro_rules! method_decorator { + ($name:ident, $method:ident) => ( + pub fn $name(ecx: &mut ExtCtxt, sp: Span, meta_item: &MetaItem, + annotated: &Annotatable, push: &mut FnMut(Annotatable)) { + let mut i_sp = meta_item.span; + i_sp.hi = i_sp.lo + BytePos(meta_item.name().len() as u32); + let method = Some(span(Method::$method, i_sp)); + route_decorator(method, ecx, sp, meta_item, annotated, push); + } + ) +} + +method_decorator!(get_decorator, Get); +method_decorator!(put_decorator, Put); +method_decorator!(post_decorator, Post); +method_decorator!(delete_decorator, Delete); +method_decorator!(options_decorator, Options); +method_decorator!(head_decorator, Head); +method_decorator!(trace_decorator, Trace); +method_decorator!(connect_decorator, Connect); +method_decorator!(patch_decorator, Patch);