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

View File

@ -64,6 +64,11 @@ struct UnpresentCheckboxTwo<'r> {
something: &'r str something: &'r str
} }
#[derive(Debug, PartialEq, FromForm)]
struct FieldNamedV<'r> {
v: &'r str,
}
fn parse<'f, T: FromForm<'f>>(string: &'f str) -> Option<T> { fn parse<'f, T: FromForm<'f>>(string: &'f str) -> Option<T> {
let mut items = FormItems::from(string); let mut items = FormItems::from(string);
let result = T::from_form_items(items.by_ref()); let result = T::from_form_items(items.by_ref());
@ -140,4 +145,10 @@ fn main() {
checkbox: false, checkbox: false,
something: "hello" 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"
}));
} }