route_param_option

Attribute Macro route_param_option 

Source
#[route_param_option]
Expand description

Extracts a specific route parameter into a variable wrapped in Option type.

This attribute macro retrieves a specific route parameter by key and makes it available as an Option variable. Route parameters are extracted from the URL path segments and wrapped in an Option type to safely handle cases where the parameter may not exist.

§Usage

use hyperlane::*;
use hyperlane_macros::*;

#[route("/route_param_option/:test")]
struct RouteParam;

impl ServerHook for RouteParam {
    async fn new(_ctx: &Context) -> Self {
        Self
    }

    #[response_body(&format!("route param: {request_route_param:?}"))]
    #[route_param_option("test" => request_route_param)]
    async fn handle(self, ctx: &Context) {}
}

impl RouteParam {
    #[route_param_option("test" => request_route_param)]
    async fn route_param_with_ref_self(&self, ctx: &Context) {}
}

#[route_param_option("test" => request_route_param)]
async fn standalone_route_param_handler(ctx: &Context) {}

The macro accepts a key-to-variable mapping in the format "key" => variable_name. The variable will be available as an Option<String> in the function scope.

§Multi-Parameter Usage

use hyperlane::*;
use hyperlane_macros::*;

#[route("/multi_param/:id/:name")]
struct MultiParam;

impl ServerHook for MultiParam {
    async fn new(_ctx: &Context) -> Self {
        Self
    }

    #[response_body(&format!("id: {id:?}, name: {name:?}"))]
    #[route_param_option("id" => id, "name" => name)]
    async fn handle(self, ctx: &Context) {}
}

The macro accepts multiple "key" => variable_name pairs separated by commas.