mirror of https://github.com/rwf2/Rocket.git
Allow named parameters to be ignored.
This commit is contained in:
parent
0b7d9f4602
commit
86c7a67b02
|
@ -74,6 +74,7 @@ impl RouteGenerateExt for RouteParams {
|
||||||
let name = arg.ident().expect("form param identifier").prepend(PARAM_PREFIX);
|
let name = arg.ident().expect("form param identifier").prepend(PARAM_PREFIX);
|
||||||
let ty = strip_ty_lifetimes(arg.ty.clone());
|
let ty = strip_ty_lifetimes(arg.ty.clone());
|
||||||
Some(quote_stmt!(ecx,
|
Some(quote_stmt!(ecx,
|
||||||
|
#[allow(non_snake_case)]
|
||||||
let $name: $ty = {
|
let $name: $ty = {
|
||||||
let mut items = ::rocket::request::FormItems::from($form_string);
|
let mut items = ::rocket::request::FormItems::from($form_string);
|
||||||
let form = ::rocket::request::FromForm::from_form(items.by_ref(), true);
|
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 name = arg.ident().expect("form param identifier").prepend(PARAM_PREFIX);
|
||||||
let ty = strip_ty_lifetimes(arg.ty.clone());
|
let ty = strip_ty_lifetimes(arg.ty.clone());
|
||||||
Some(quote_stmt!(ecx,
|
Some(quote_stmt!(ecx,
|
||||||
|
#[allow(non_snake_case, unreachable_patterns)]
|
||||||
let $name: $ty =
|
let $name: $ty =
|
||||||
match ::rocket::data::FromData::from_data(__req, __data) {
|
match ::rocket::data::FromData::from_data(__req, __data) {
|
||||||
::rocket::Outcome::Success(d) => d,
|
::rocket::Outcome::Success(d) => d,
|
||||||
|
@ -163,7 +165,7 @@ impl RouteGenerateExt for RouteParams {
|
||||||
|
|
||||||
let original_ident = param.ident();
|
let original_ident = param.ident();
|
||||||
fn_param_statements.push(quote_stmt!(ecx,
|
fn_param_statements.push(quote_stmt!(ecx,
|
||||||
#[allow(unreachable_patterns)]
|
#[allow(non_snake_case, unreachable_patterns)]
|
||||||
let $ident: $ty = match $expr {
|
let $ident: $ty = match $expr {
|
||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
@ -196,7 +198,7 @@ impl RouteGenerateExt for RouteParams {
|
||||||
let ident = arg.ident().unwrap().prepend(PARAM_PREFIX);
|
let ident = arg.ident().unwrap().prepend(PARAM_PREFIX);
|
||||||
let ty = strip_ty_lifetimes(arg.ty.clone());
|
let ty = strip_ty_lifetimes(arg.ty.clone());
|
||||||
fn_param_statements.push(quote_stmt!(ecx,
|
fn_param_statements.push(quote_stmt!(ecx,
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case, unreachable_patterns)]
|
||||||
let $ident: $ty = match
|
let $ident: $ty = match
|
||||||
::rocket::request::FromRequest::from_request(__req) {
|
::rocket::request::FromRequest::from_request(__req) {
|
||||||
::rocket::Outcome::Success(v) => v,
|
::rocket::Outcome::Success(v) => v,
|
||||||
|
|
|
@ -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")
|
ecx.struct_span_err(span, "parameter names must be valid identifiers")
|
||||||
.note(&format!("{:?} is not a valid identifier", param))
|
.note(&format!("{:?} is not a valid identifier", param))
|
||||||
.emit();
|
.emit();
|
||||||
} else if param.starts_with('_') {
|
} else if param == "_" {
|
||||||
ecx.struct_span_err(span, "parameters cannot be ignored")
|
ecx.struct_span_err(span, "parameters must be named")
|
||||||
.note(&format!("{:?} is being ignored", param))
|
.help("use a name such as `_guard` or `_param`")
|
||||||
.emit();
|
.emit();
|
||||||
} else {
|
} else {
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -10,4 +10,7 @@ fn get1() -> &'static str { "hi" } //~ ERROR isn't in the function
|
||||||
#[post("/a", data = "<test>")] //~ ERROR 'test' is declared
|
#[post("/a", data = "<test>")] //~ ERROR 'test' is declared
|
||||||
fn post() -> &'static str { "hi" } //~ ERROR isn't in the function
|
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() { }
|
fn main() { }
|
||||||
|
|
|
@ -13,7 +13,7 @@ fn get2(id: usize) -> &'static str { "hi" }
|
||||||
#[get("/<!>")] //~ ERROR identifiers
|
#[get("/<!>")] //~ ERROR identifiers
|
||||||
fn get3() -> &'static str { "hi" }
|
fn get3() -> &'static str { "hi" }
|
||||||
|
|
||||||
#[get("/<_>")] //~ ERROR ignored
|
#[get("/<_>")] //~ ERROR named
|
||||||
fn get4() -> &'static str { "hi" }
|
fn get4() -> &'static str { "hi" }
|
||||||
|
|
||||||
#[get("/<1>")] //~ ERROR identifiers
|
#[get("/<1>")] //~ ERROR identifiers
|
||||||
|
|
|
@ -12,9 +12,9 @@ struct User<'a> {
|
||||||
nickname: String,
|
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,
|
fn get<'r>(name: &RawStr,
|
||||||
query: User<'r>,
|
_query: User<'r>,
|
||||||
user: Form<'r, User<'r>>,
|
user: Form<'r, User<'r>>,
|
||||||
cookies: Cookies)
|
cookies: Cookies)
|
||||||
-> &'static str {
|
-> &'static str {
|
||||||
|
|
|
@ -6,6 +6,9 @@ extern crate rocket;
|
||||||
#[get("/test/<one>/<two>/<three>")]
|
#[get("/test/<one>/<two>/<three>")]
|
||||||
fn get(one: String, two: usize, three: isize) -> &'static str { "hi" }
|
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() {
|
fn main() {
|
||||||
let _ = routes![get];
|
let _ = routes![get, ignored];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue