Skip to content

Commit

Permalink
Avoid collision in FromForm derive by using weird names.
Browse files Browse the repository at this point in the history
Fixes #265.
  • Loading branch information
SergioBenitez committed Apr 17, 2017
1 parent 9f0c8a8 commit 71878ff
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
16 changes: 8 additions & 8 deletions codegen/src/decorators/derive_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
};
Expand All @@ -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
}
};
Expand Down
11 changes: 11 additions & 0 deletions codegen/tests/run-pass/derive_form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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"
}));
}

0 comments on commit 71878ff

Please sign in to comment.