Skip to main content

Crate tako_rs_macros

Crate tako_rs_macros 

Source
Expand description

Proc macros for the tako-rs framework.

Provides route, an attribute macro placed directly above an async handler function. Given an HTTP method and a path with {name: Type} placeholders, it generates a sibling pub struct whose fields exactly mirror the placeholders, plus:

  • pub const METHOD: tako::Method and pub const PATH: &'static str
  • an impl TypedParamsStruct that pulls each field from the request’s PathParams extension and parses it via core::str::FromStr

The struct name is auto-derived from the handler function’s name (snake_casePascalCase + Params). For example, get_user produces GetUserParams. Override the default with name = "..." if you need a different identifier.

Method-specific shortcuts (get, post, put, delete, patch) take only the path and an optional name = "...".

Usage:

use tako::{get, route};
use tako::extractors::typed_params::TypedParams;
use tako::responder::Responder;

#[route(GET, "/users/{id: u64}/posts/{post_id: u64}")]
async fn get_user(TypedParams(p): TypedParams<GetUserParams>) -> impl Responder {
    format!("user {} post {}", p.id, p.post_id)
}

#[get("/health")]
async fn health() -> impl Responder { "ok" }

// …in build_router:
// router.route(GetUserParams::METHOD, GetUserParams::PATH, get_user);
// router.route(HealthParams::METHOD, HealthParams::PATH, health);

The macro must be attached to a free async fn at module scope — Rust scopes structs declared inside fn bodies to that fn, so the generated type wouldn’t be reachable from the handler signature otherwise.

Attribute Macros§

delete
#[delete("/path", [name = "Foo"])] — shorthand for #[route(DELETE, ...)].
get
#[get("/path", [name = "Foo"])] — shorthand for #[route(GET, ...)].
patch
#[patch("/path", [name = "Foo"])] — shorthand for #[route(PATCH, ...)].
post
#[post("/path", [name = "Foo"])] — shorthand for #[route(POST, ...)].
put
#[put("/path", [name = "Foo"])] — shorthand for #[route(PUT, ...)].
route