mirror of https://github.com/rwf2/Rocket.git
Added method specific macros.
This commit is contained in:
parent
c7b1eebd20
commit
eabb5169de
|
@ -15,7 +15,7 @@ struct UserLogin<'r> {
|
|||
age: Result<isize, &'r str>,
|
||||
}
|
||||
|
||||
#[route(POST, path = "/login", form = "<user>")]
|
||||
#[POST(path = "/login", form = "<user>")]
|
||||
fn login(user: UserLogin) -> Result<Redirect, String> {
|
||||
if user.age.is_err() {
|
||||
let input = user.age.unwrap_err();
|
||||
|
@ -36,7 +36,7 @@ fn login(user: UserLogin) -> Result<Redirect, String> {
|
|||
}
|
||||
}
|
||||
|
||||
#[route(GET, path = "/user/<username>")]
|
||||
#[GET(path = "/user/<username>")]
|
||||
fn user_page(username: &str) -> String {
|
||||
format!("This is {}'s page.", username)
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
extern crate rocket;
|
||||
use rocket::Rocket;
|
||||
|
||||
#[route(GET, path = "/hello/<name>/<age>")]
|
||||
#[GET(path = "/hello/<name>/<age>")]
|
||||
fn hello(name: &str, age: i8) -> String {
|
||||
format!("Hello, {} year old named {}!", age, name)
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
extern crate rocket;
|
||||
use rocket::Rocket;
|
||||
|
||||
#[route(GET, path = "/")]
|
||||
#[GET(path = "/")]
|
||||
fn root() -> &'static str {
|
||||
"Hello, world!"
|
||||
}
|
||||
|
|
|
@ -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
|
||||
);
|
||||
}
|
||||
|
|
|
@ -130,14 +130,15 @@ fn method_variant_to_expr(ecx: &ExtCtxt, method: Method) -> P<Expr> {
|
|||
}
|
||||
|
||||
// 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<Spanned<Method>>, 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);
|
||||
|
|
Loading…
Reference in New Issue