Derive Macro rocket_codegen::FromFormField

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

Derive for the FromFormField trait.

The FromFormField derive can be applied to enums with nullary (zero-length) fields:

#[derive(FromFormField)]
enum MyValue {
    First,
    Second,
    Third,
}

The derive generates an implementation of the FromFormField trait for the decorated enum. The implementation returns successfully when the form value matches, case insensitively, the stringified version of a variant’s name, returning an instance of said variant. If there is no match, an error recording all of the available options is returned.

As an example, for the enum above, the form values "first", "FIRST", "fiRSt", and so on would parse as MyValue::First, while "second" and "third" (in any casing) would parse as MyValue::Second and MyValue::Third, respectively.

The field field attribute can be used to change the string value that is compared against for a given variant:

#[derive(FromFormField)]
enum MyValue {
    First,
    Second,
    #[field(value = "fourth")]
    #[field(value = "fifth")]
    Third,
}

When more than one value is specified, matching any value will result in parsing the decorated variant. Declaring any two values that are case-insensitively equal to any other value or variant name is a compile-time error.

The #[field] attribute’s grammar is:

field := 'value' '=' STRING_LIT

STRING_LIT := any valid string literal, as defined by Rust

The attribute accepts a single string parameter of name value corresponding to the string to use to match against for the decorated variant. In the example above, the the strings "fourth", "FOUrth", "fiFTH" and so on would parse as MyValue::Third.