Skip to main content

Crate chopin_macros

Crate chopin_macros 

Source
Expand description

Derive macros and attribute macros for the Chopin framework.

MacroKindPurpose
#[get("/path")], #[post], #[put], #[delete], #[patch]attributeRegister a handler function as a route at link time via inventory
#[derive(IntoResponse)]deriveGenerate 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§

IntoResponse
Derive From<YourError> for chopin_core::http::Response for an error enum.