Allow named parameters to be ignored.

This commit is contained in:
Sergio Benitez 2017-08-02 18:44:31 -07:00
parent 0b7d9f4602
commit 86c7a67b02
6 changed files with 17 additions and 9 deletions

View File

@ -74,6 +74,7 @@ impl RouteGenerateExt for RouteParams {
let name = arg.ident().expect("form param identifier").prepend(PARAM_PREFIX);
let ty = strip_ty_lifetimes(arg.ty.clone());
Some(quote_stmt!(ecx,
#[allow(non_snake_case)]
let $name: $ty = {
let mut items = ::rocket::request::FormItems::from($form_string);
let form = ::rocket::request::FromForm::from_form(items.by_ref(), true);
@ -107,6 +108,7 @@ impl RouteGenerateExt for RouteParams {
let name = arg.ident().expect("form param identifier").prepend(PARAM_PREFIX);
let ty = strip_ty_lifetimes(arg.ty.clone());
Some(quote_stmt!(ecx,
#[allow(non_snake_case, unreachable_patterns)]
let $name: $ty =
match ::rocket::data::FromData::from_data(__req, __data) {
::rocket::Outcome::Success(d) => d,
@ -163,7 +165,7 @@ impl RouteGenerateExt for RouteParams {
let original_ident = param.ident();
fn_param_statements.push(quote_stmt!(ecx,
#[allow(unreachable_patterns)]
#[allow(non_snake_case, unreachable_patterns)]
let $ident: $ty = match $expr {
Ok(v) => v,
Err(e) => {
@ -196,7 +198,7 @@ impl RouteGenerateExt for RouteParams {
let ident = arg.ident().unwrap().prepend(PARAM_PREFIX);
let ty = strip_ty_lifetimes(arg.ty.clone());
fn_param_statements.push(quote_stmt!(ecx,
#[allow(non_snake_case)]
#[allow(non_snake_case, unreachable_patterns)]
let $ident: $ty = match
::rocket::request::FromRequest::from_request(__req) {
::rocket::Outcome::Success(v) => v,

View File

@ -60,9 +60,9 @@ fn valid_segments(ecx: &ExtCtxt, uri: &URI, sp: Span) -> bool {
ecx.struct_span_err(span, "parameter names must be valid identifiers")
.note(&format!("{:?} is not a valid identifier", param))
.emit();
} else if param.starts_with('_') {
ecx.struct_span_err(span, "parameters cannot be ignored")
.note(&format!("{:?} is being ignored", param))
} else if param == "_" {
ecx.struct_span_err(span, "parameters must be named")
.help("use a name such as `_guard` or `_param`")
.emit();
} else {
continue

View File

@ -10,4 +10,7 @@ fn get1() -> &'static str { "hi" } //~ ERROR isn't in the function
#[post("/a", data = "<test>")] //~ ERROR 'test' is declared
fn post() -> &'static str { "hi" } //~ ERROR isn't in the function
#[get("/<_r>")] //~ ERROR '_r' is declared
fn get2(r: usize) -> &'static str { "hi" } //~ ERROR isn't in the function
fn main() { }

View File

@ -13,7 +13,7 @@ fn get2(id: usize) -> &'static str { "hi" }
#[get("/<!>")] //~ ERROR identifiers
fn get3() -> &'static str { "hi" }
#[get("/<_>")] //~ ERROR ignored
#[get("/<_>")] //~ ERROR named
fn get4() -> &'static str { "hi" }
#[get("/<1>")] //~ ERROR identifiers

View File

@ -12,9 +12,9 @@ struct User<'a> {
nickname: String,
}
#[post("/<name>?<query>", format = "application/json", data = "<user>", rank = 2)]
#[post("/<name>?<_query>", format = "application/json", data = "<user>", rank = 2)]
fn get<'r>(name: &RawStr,
query: User<'r>,
_query: User<'r>,
user: Form<'r, User<'r>>,
cookies: Cookies)
-> &'static str {

View File

@ -6,6 +6,9 @@ extern crate rocket;
#[get("/test/<one>/<two>/<three>")]
fn get(one: String, two: usize, three: isize) -> &'static str { "hi" }
#[get("/test/<_one>/<_two>/<__three>")]
fn ignored(_one: String, _two: usize, __three: isize) -> &'static str { "hi" }
fn main() {
let _ = routes![get];
let _ = routes![get, ignored];
}