Skip to main content
nestrs uses proc macros to map Rust structs and impl blocks onto Axum routes. The pattern mirrors NestJS decorators but compiles to zero-overhead Axum router registrations. Every macro described here is re-exported from nestrs::prelude::*. A typical controller looks like this:

#[controller]

Marks a struct as a nestrs controller. Sets the route prefix and optional API version for all routes registered in the associated #[routes] impl block.
prefix
&str
URL prefix applied to all routes in this controller. May include path segments with leading slash.
version
&str (optional)
Route version string used by route-level versioning (e.g., "v1"). Works together with #[ver] on individual handlers.

#[routes]

Expands an impl block on a controller struct into Axum route registrations. Must appear on the impl block directly following a #[controller]-annotated struct.
state
Type
The Axum application state type injected into handler parameters. Must implement Clone and be registered as a provider.

HTTP method macros

All HTTP method macros accept a path string as their argument and are applied to async methods inside a #[routes] impl block. Path segments follow Axum syntax: /:param for path parameters, /*wildcard for catch-all segments.

Parameter extraction attributes

Inside a #[routes] impl block, method parameters can be annotated to declare their extraction source:

Response shaping macros

#[http_code]

Sets the HTTP status code for a successful handler response.

#[response_header]

Adds a response header to the handler’s response.

#[redirect]

Redirects the request to a different URL.

Route versioning macros

#[ver] / #[version]

Overrides the version for an individual route handler. Works in combination with the version set on #[controller].

#[raw_body]

Marks a handler parameter to receive the raw request body bytes instead of a deserialized value.

#[sse]

Marks a handler as a Server-Sent Events endpoint. The handler return type must implement the SSE stream interface from nestrs::sse.

Cross-cutting handler attributes

These attributes apply pipeline steps to individual route handlers and can be combined:

impl_routes! macro

impl_routes! is the lower-level macro that #[routes] expands into. You can use it directly for explicit route registration without proc macro expansion on the impl block.
The impl_routes! macro also accepts controller_guards (G) to apply a guard to all routes in the block, running outside route-level guards in the pipeline order.