Skip to main content

api_description

Attribute Macro api_description 

Source
#[api_description]
Expand description

Defines an API as a trait, generating endpoint types and a Serves bridge.

Each method in the trait is annotated with an HTTP method attribute (#[get(...)], #[post(...)], etc.) that specifies the path. The macro generates:

  1. A type alias <TraitName>Spec — a tuple of endpoint types
  2. The original trait with async method signatures
  3. An into_handlers method that produces a handler tuple for Server::new

§Example

#[api_description]
trait UserAPI {
    #[get("users" / u32)]
    async fn get_user(path: Path<UserByIdPath>) -> Json<User>;

    #[post("users")]
    async fn create_user(body: Json<CreateUser>) -> Json<User>;
}

struct MyImpl { db: DbPool }
impl UserAPI for MyImpl {
    async fn get_user(path: Path<UserByIdPath>) -> Json<User> {
        let user = User { id: path.id, name: "Alice".into() };
        Json(user)
    }
    async fn create_user(body: Json<CreateUser>) -> Json<User> {
        let user = User { id: 1, name: body.0.name.clone() };
        Json(user)
    }
}

// Use: serve_user_api() bridges the trait impl to Server::new
Server::<UserAPISpec>::new(serve_user_api(MyImpl));