Skip to main content

Crate rust_api

Crate rust_api 

Source
Expand description

RustAPI: FastAPI-inspired REST framework for Rust

RustAPI brings the developer experience of FastAPI and NestJS to Rust, with automatic OpenAPI generation, built-in validation, and dependency injection.

§Features

  • Route Macros: Define endpoints with #[get], #[post], etc. The HTTP verb from the annotation is a binding contract — enforced at registration time via the mount_handlers! macro.
  • Kleisli Pipeline: RouterPipeline composes Controller Kleisli arrows with and_then (>>=), short-circuiting on any error.
  • Direct DI: Pass Arc<Service> directly to pipeline.mount::<C>(svc). No type-map registry required for the primary use case.
  • Type-Driven: Leverage Rust’s type system for validation and docs.
  • Zero-Cost: Built on Axum and Tokio for production performance.

§Quick Start

use rust_api::prelude::*;

pub struct UserController;

#[get("/users")]
pub async fn list_users(State(svc): State<Arc<UserService>>) -> Json<Vec<User>> {
    Json(svc.list())
}

mount_handlers!(UserController, UserService, [(__list_users_route, list_users)]);

#[tokio::main]
async fn main() {
    let svc = Arc::new(UserService::new());

    let app = RouterPipeline::new()
        .mount::<UserController>(svc)
        .map(|r| r.layer(TraceLayer::new_for_http()))
        .build()
        .unwrap();

    RustAPI::new(app).port(3000).serve().await.unwrap();
}

Re-exports§

pub use app::App;
pub use controller::Controller;
pub use di::Container;
pub use di::Injectable;
pub use error::Error;
pub use error::Result;
pub use middleware::guard;
pub use middleware::require_bearer;
pub use pipeline::RouterPipeline;
pub use pipeline::RouterTransform;
pub use router::method_filter_from_str;
pub use router::ApiRoute;
pub use router::Router;
pub use router::RouterExt;
pub use server::RustAPI;

Modules§

app
Application builder for rust-api framework
controller
Controller trait — pure descriptor for composable route groups.
di
Dependency Injection Container (optional utility)
error
Error types for rust-api framework
header
HTTP header types
middleware
Request-lifecycle middleware utilities and Tower layer factories.
pipeline
Monadic router pipeline for composable, error-propagating route registration.
prelude
Prelude module for convenient imports.
router
Router utilities for RustAPI framework
routing
server
Server runtime for RustAPI framework

Macros§

mount_handlers
Generate a Controller implementation for a type from a handler list.

Structs§

Arc
A thread-safe reference-counting pointer. ‘Arc’ stands for ‘Atomically Reference Counted’.
CorsLayer
Layer that applies the Cors middleware which adds headers for CORS.
Json
JSON Extractor / Response.
Parts
Component parts of an HTTP Request
Path
Extractor that will get captures from the URL and parse them using serde.
Query
Extractor that deserializes query strings into some type.
State
Extractor for state.
StatusCode
An HTTP status code (status-code in RFC 9110 et al.).
TraceLayer
Layer that adds high level tracing to a Service.

Traits§

Deserialize
A data structure that can be deserialized from any data format supported by Serde.
FromRequestParts
Types that can be created from request parts.
IntoResponse
Trait for generating responses.
Serialize
A data structure that can be serialized into any data format supported by Serde.

Type Aliases§

Response
Type alias for http::Response whose body type defaults to Body, the most common body type used with axum.

Attribute Macros§

delete
Define a DELETE route handler
get
Define a GET route handler
patch
Define a PATCH route handler
post
Define a POST route handler
put
Define a PUT route handler

Derive Macros§

Deserialize
Serialize