Skip to main content

openapi_trait/
lib.rs

1//! Generate typed Rust traits from `OpenAPI` specifications.
2//!
3//! This crate exposes the [`axum`] attribute macro, which reads an
4//! `OpenAPI` specification file at compile time and generates inside the
5//! annotated `mod`:
6//!
7//! - Rust structs for every schema defined in `components/schemas`
8//! - A `{OperationId}Request` struct per operation that bundles all parameters
9//!   and the request body
10//! - A `{OperationId}Response` enum per operation whose variants map to HTTP
11//!   status codes
12//! - `impl axum::response::IntoResponse` for every response enum
13//! - A `{Title}Api` trait with one `async fn` per operation, keyed by
14//!   `operationId`
15//! - A `router` method on the trait that wires all operations to an
16//!   [`axum::Router`](https://docs.rs/axum/latest/axum/struct.Router.html)
17//!
18//! # Quick start
19//!
20//! ```rust,ignore
21//! #[openapi_trait::axum("openapi/petstore.yaml")]
22//! pub mod petstore {}
23//!
24//! #[derive(Clone)]
25//! struct MyServer;
26//!
27//! impl petstore::PetstoreApi for MyServer {
28//!     type Error = std::convert::Infallible;
29//!
30//!     async fn list_pets(
31//!         &self,
32//!         req: petstore::ListPetsRequest,
33//!         state: axum::extract::State<()>,
34//!         headers: axum::http::HeaderMap,
35//!     ) -> Result<petstore::ListPetsResponse, Self::Error> {
36//!         Ok(petstore::ListPetsResponse::Status200(vec![]))
37//!     }
38//! }
39//!
40//! // Wire up an axum router.
41//! let app = MyServer.router().with_state(());
42//! ```
43//!
44//! See the [`axum`] macro documentation for the full list of generated items
45//! and compile-time error conditions.
46
47/// The `openapi_trait` attribute macro, wired to the axum backend.
48///
49/// Re-exported from [`openapi_trait_axum`]. Apply it to a `mod` block with a
50/// path to an `OpenAPI` YAML or JSON file. The path is resolved relative to
51/// `CARGO_MANIFEST_DIR`.
52#[doc(inline)]
53pub use openapi_trait_axum::openapi_trait as axum;