Derive Macro submillisecond::NamedParam

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

The NamedParam derive macro can be used to implement FromRequest for a struct.

If using with unnamed struct, then the #[param(name = "...")] attribute should be used.

If using with a struct with named fields, then each field name should match the ones defined in the router.

Struct with fields example

#[derive(NamedParam)]
struct Params {
    name: String,
    age: i32,
}

fn user_name_age(Params { name, age }: Params) -> String {
    format!("Hello {name}, you are {age} years old")
}

router! {
    GET "/user/:name/:age" => user_name_age
}

Unnamed struct example

#[derive(NamedParam)]
#[param(name = "age")]
struct AgeParam(i32);

fn age_param(AgeParam(age): AgeParam) -> String {
    format!("You are {age} years old")
}

router! {
    GET "/user/:age" => age_param
}