Derive Macro rocket::FromForm

source ·
#[derive(FromForm)]
{
    // Attributes available to this derive:
    #[form]
}
Expand description

Derive for the FromForm trait.

The FromForm derive can be applied to structures with named fields:

#[derive(FromForm)]
struct MyStruct {
    field: usize,
    other: String
}

Each field’s type is required to implement FromFormValue.

The derive generates an implementation of the FromForm trait. The implementation parses a form whose field names match the field names of the structure on which the derive was applied. Each field’s value is parsed with the FromFormValue implementation of the field’s type. The FromForm implementation succeeds only when all of the field parses succeed. If parsing fails, an error (FromForm::Error) of type FormParseError is returned.

The derive accepts one field attribute: form, with the following syntax:

form := 'field' '=' '"' IDENT '"'

IDENT := valid identifier, as defined by Rust

When applied, the attribute looks as follows:

#[derive(FromForm)]
struct MyStruct {
    field: usize,
    #[form(field = "renamed_field")]
    other: String
}

The field attribute directs that a different incoming field name is expected, and the value of the field attribute is used instead of the structure’s actual field name when parsing a form. In the example above, the value of the MyStruct::other struct field will be parsed from the incoming form’s renamed_field field.