From 677d7c49eac0a93792d259042000acbfacbe9cd9 Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Tue, 19 Jul 2016 20:09:35 -0700 Subject: [PATCH] Slightly cleaner form argument parsing. --- macros/src/route_decorator.rs | 43 +++++++++++++++++------------------ 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/macros/src/route_decorator.rs b/macros/src/route_decorator.rs index 0573a524..2455ccf7 100644 --- a/macros/src/route_decorator.rs +++ b/macros/src/route_decorator.rs @@ -207,7 +207,7 @@ fn get_fn_params<'a, T: Iterator>>(ecx: &ExtCtxt, fn get_form_stmt(ecx: &ExtCtxt, fn_args: &mut Vec, form_params: &[Spanned<&str>]) -> Option { if form_params.len() < 1 { - return None + return None; } else if form_params.len() > 1 { panic!("Allowed more than 1 form parameter!"); } @@ -232,20 +232,18 @@ fn get_form_stmt(ecx: &ExtCtxt, fn_args: &mut Vec, // The actual code we'll be inserting. quote_stmt!(ecx, - let $param_ident: $param_ty = { - let form_string = std::str::from_utf8(_req.data); - if form_string.is_err() { - return ::rocket::Response::server_error(); - }; - - match ::rocket::form::FromForm::from_form_string(form_string.unwrap()) { - Ok(v) => v, - Err(_) => { - println!("\t=> Form failed to parse."); - return rocket::Response::not_found() + let $param_ident: $param_ty = + if let Ok(form_string) = ::std::str::from_utf8(_req.data) { + match ::rocket::form::FromForm::from_form_string(form_string) { + Ok(v) => v, + Err(_) => { + println!("\t=> Form failed to parse."); + return ::rocket::Response::not_found(); + } } + } else { + return ::rocket::Response::server_error(); } - } ) } @@ -304,7 +302,7 @@ pub fn route_decorator(ecx: &mut ExtCtxt, sp: Span, meta_item: &MetaItem, let param_fn_item = quote_stmt!(ecx, let $param_ident: $param_ty = match _req.get_param($i) { Ok(v) => v, - Err(_) => return rocket::Response::forward() + Err(_) => return ::rocket::Response::forward() }; ).unwrap(); @@ -316,12 +314,12 @@ pub fn route_decorator(ecx: &mut ExtCtxt, sp: Span, meta_item: &MetaItem, let route_fn_name = prepend_ident(ROUTE_FN_PREFIX, &item.ident); let fn_name = item.ident; let route_fn_item = quote_item!(ecx, - fn $route_fn_name<'rocket>(_req: rocket::Request<'rocket>) - -> rocket::Response<'rocket> { + fn $route_fn_name<'rocket>(_req: ::rocket::Request<'rocket>) + -> ::rocket::Response<'rocket> { $form_stmt $fn_param_exprs let result = $fn_name($fn_param_idents); - rocket::Response::new(result) + ::rocket::Response::new(result) } ).unwrap(); @@ -333,11 +331,12 @@ pub fn route_decorator(ecx: &mut ExtCtxt, sp: Span, meta_item: &MetaItem, let method = method_variant_to_expr(ecx, route.method.node); push(Annotatable::Item(quote_item!(ecx, #[allow(non_upper_case_globals)] - pub static $struct_name: rocket::StaticRouteInfo = rocket::StaticRouteInfo { - method: $method, - path: $path, - handler: $route_fn_name - }; + pub static $struct_name: ::rocket::StaticRouteInfo = + ::rocket::StaticRouteInfo { + method: $method, + path: $path, + handler: $route_fn_name + }; ).unwrap())); }