Avoid collision in FromForm derive by using weird names.

Fixes #265.
This commit is contained in:
Sergio Benitez 2017-04-17 15:54:44 -07:00
parent 9f0c8a80ae
commit 71878ffebf
2 changed files with 19 additions and 8 deletions

View File

@ -178,11 +178,11 @@ fn from_form_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substruct
let id_str = ident_string.as_str();
arms.push(quote_tokens!(cx,
$id_str => {
$ident = match ::rocket::request::FromFormValue::from_form_value(v) {
Ok(v) => Some(v),
Err(e) => {
$ident = match ::rocket::request::FromFormValue::from_form_value(__v) {
Ok(__v) => Some(__v),
Err(__e) => {
println!(" => Error parsing form val '{}': {:?}",
$id_str, e);
$id_str, __e);
$return_err_stmt
}
};
@ -193,17 +193,17 @@ fn from_form_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substruct
// The actual match statement. Iterate through all of the fields in the form
// and use the $arms generated above.
stmts.push(quote_stmt!(cx,
for (k, v) in $arg {
match k {
for (__k, __v) in $arg {
match __k {
$arms
field if field == "_method" => {
__field if __field == "_method" => {
/* This is a Rocket-specific field. If the user hasn't asked
* for it, just let it go by without error. This should stay
* in sync with Rocket::preprocess. */
}
_ => {
println!(" => {}={} has no matching field in struct.",
k, v);
__k, __v);
$return_err_stmt
}
};

View File

@ -64,6 +64,11 @@ struct UnpresentCheckboxTwo<'r> {
something: &'r str
}
#[derive(Debug, PartialEq, FromForm)]
struct FieldNamedV<'r> {
v: &'r str,
}
fn parse<'f, T: FromForm<'f>>(string: &'f str) -> Option<T> {
let mut items = FormItems::from(string);
let result = T::from_form_items(items.by_ref());
@ -140,4 +145,10 @@ fn main() {
checkbox: false,
something: "hello"
}));
// Check that a structure with one field `v` parses correctly.
let manual: Option<FieldNamedV> = parse("v=abc");
assert_eq!(manual, Some(FieldNamedV {
v: "abc"
}));
}