Derive Macro rocket::FromParam

source ·
#[derive(FromParam)]
Expand description

Derive for the FromParam trait.

This FromParam derive can be applied to C-like enums whose variants have no fields. The generated implementation case-sensitively matches each variant to its stringified field name. If there is no match, an error of type InvalidOption is returned.

§Example

use rocket::request::FromParam;

#[derive(FromParam, Debug, PartialEq)]
enum MyParam {
    A,
    Bob,
}

assert_eq!(MyParam::from_param("A").unwrap(), MyParam::A);
assert_eq!(MyParam::from_param("Bob").unwrap(), MyParam::Bob);
assert!(MyParam::from_param("a").is_err());
assert!(MyParam::from_param("bob").is_err());
assert!(MyParam::from_param("c").is_err());
assert!(MyParam::from_param("C").is_err());

// Now `MyParam` can be used in an route to accept either `A` or `B`.
#[get("/<param>")]
fn index(param: MyParam) -> &'static str {
    match param {
        MyParam::A => "A",
        MyParam::Bob => "Bob",
    }
}