Derive Macro rocket::FromFormValue

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

Derive for the FromFormValue trait.

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

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

The derive generates an implementation of the FromFormValue 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 (FromFormValue::Error) of type &RawStr is returned, the value of which is the raw form field value that failed to match.

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" would parse as MyValue::Second and MyValue::Third, respectively.

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

#[derive(FromFormValue)]
enum MyValue {
    First,
    Second,
    #[form(value = "fourth")]
    Third,
}

The #[form] attribute’s grammar is:

form := 'field' '=' 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" and so on would parse as MyValue::Third.