Expand description
Derive macros and attribute macros for the Chopin framework.
| Macro | Kind | Purpose |
|---|---|---|
#[get("/path")], #[post], #[put], #[delete], #[patch] | attribute | Register a handler function as a route at link time via inventory |
#[derive(IntoResponse)] | derive | Generate From<MyError> for Response for an error enum |
§Route registration
Route attribute macros register handlers globally. Call
Chopin::new().mount_all_routes() to load all registered routes at startup:
ⓘ
use chopin_macros::{get, post, delete};
use chopin_core::{Context, Response, Chopin};
#[get("/users/:id")]
fn show_user(ctx: Context) -> Response { Response::text("hello") }
#[post("/users")]
fn create_user(ctx: Context) -> Response { Response::text("created") }
fn main() {
Chopin::new().mount_all_routes().serve("0.0.0.0:8080").unwrap();
}§#[derive(IntoResponse)]
Generates From<YourError> for Response so handlers can use ?:
ⓘ
use chopin_macros::IntoResponse;
#[derive(IntoResponse)]
enum ApiError {
#[status(404)] NotFound,
#[status(422)] Validation(String),
#[status(500)] Internal,
}Attribute Macros§
- connect
- delete
- get
- head
- options
- patch
- post
- put
- require_
role - Inline role guard that wraps a Chopin handler with a JWT + RBAC check.
- require_
scope - Inline OAuth 2.0 scope guard that wraps a Chopin handler with a JWT + scope check.
- trace
Derive Macros§
- Into
Response - Derive
From<YourError> for chopin_core::http::Responsefor an error enum.