Slightly cleaner form argument parsing.

This commit is contained in:
Sergio Benitez 2016-07-19 20:09:35 -07:00
parent 92671a0cba
commit 677d7c49ea
1 changed files with 21 additions and 22 deletions

View File

@ -207,7 +207,7 @@ fn get_fn_params<'a, T: Iterator<Item=&'a Spanned<&'a str>>>(ecx: &ExtCtxt,
fn get_form_stmt(ecx: &ExtCtxt, fn_args: &mut Vec<SimpleArg>, fn get_form_stmt(ecx: &ExtCtxt, fn_args: &mut Vec<SimpleArg>,
form_params: &[Spanned<&str>]) -> Option<Stmt> { form_params: &[Spanned<&str>]) -> Option<Stmt> {
if form_params.len() < 1 { if form_params.len() < 1 {
return None return None;
} else if form_params.len() > 1 { } else if form_params.len() > 1 {
panic!("Allowed more than 1 form parameter!"); panic!("Allowed more than 1 form parameter!");
} }
@ -232,19 +232,17 @@ fn get_form_stmt(ecx: &ExtCtxt, fn_args: &mut Vec<SimpleArg>,
// The actual code we'll be inserting. // The actual code we'll be inserting.
quote_stmt!(ecx, quote_stmt!(ecx,
let $param_ident: $param_ty = { let $param_ident: $param_ty =
let form_string = std::str::from_utf8(_req.data); if let Ok(form_string) = ::std::str::from_utf8(_req.data) {
if form_string.is_err() { match ::rocket::form::FromForm::from_form_string(form_string) {
return ::rocket::Response::server_error();
};
match ::rocket::form::FromForm::from_form_string(form_string.unwrap()) {
Ok(v) => v, Ok(v) => v,
Err(_) => { Err(_) => {
println!("\t=> Form failed to parse."); println!("\t=> Form failed to parse.");
return rocket::Response::not_found() 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_fn_item = quote_stmt!(ecx,
let $param_ident: $param_ty = match _req.get_param($i) { let $param_ident: $param_ty = match _req.get_param($i) {
Ok(v) => v, Ok(v) => v,
Err(_) => return rocket::Response::forward() Err(_) => return ::rocket::Response::forward()
}; };
).unwrap(); ).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 route_fn_name = prepend_ident(ROUTE_FN_PREFIX, &item.ident);
let fn_name = item.ident; let fn_name = item.ident;
let route_fn_item = quote_item!(ecx, let route_fn_item = quote_item!(ecx,
fn $route_fn_name<'rocket>(_req: rocket::Request<'rocket>) fn $route_fn_name<'rocket>(_req: ::rocket::Request<'rocket>)
-> rocket::Response<'rocket> { -> ::rocket::Response<'rocket> {
$form_stmt $form_stmt
$fn_param_exprs $fn_param_exprs
let result = $fn_name($fn_param_idents); let result = $fn_name($fn_param_idents);
rocket::Response::new(result) ::rocket::Response::new(result)
} }
).unwrap(); ).unwrap();
@ -333,7 +331,8 @@ pub fn route_decorator(ecx: &mut ExtCtxt, sp: Span, meta_item: &MetaItem,
let method = method_variant_to_expr(ecx, route.method.node); let method = method_variant_to_expr(ecx, route.method.node);
push(Annotatable::Item(quote_item!(ecx, push(Annotatable::Item(quote_item!(ecx,
#[allow(non_upper_case_globals)] #[allow(non_upper_case_globals)]
pub static $struct_name: rocket::StaticRouteInfo = rocket::StaticRouteInfo { pub static $struct_name: ::rocket::StaticRouteInfo =
::rocket::StaticRouteInfo {
method: $method, method: $method,
path: $path, path: $path,
handler: $route_fn_name handler: $route_fn_name