rust_api/lib.rs
1//! RustAPI: FastAPI-inspired REST framework for Rust
2//!
3//! RustAPI brings the developer experience of FastAPI and NestJS to Rust,
4//! with automatic OpenAPI generation, built-in validation, and dependency injection.
5//!
6//! # Features
7//!
8//! - **Route Macros**: Define endpoints with `#[get]`, `#[post]`, etc.
9//! - **Dependency Injection**: Type-safe DI container for services
10//! - **Type-Driven**: Leverage Rust's type system for validation and docs
11//! - **Zero-Cost**: Built on Axum and Tokio for production performance
12//!
13//! # Quick Start
14//!
15//! ```ignore
16//! use rust_api::prelude::*;
17//!
18//! #[get("/users/{id}")]
19//! async fn get_user(Path(id): Path<String>) -> Json<User> {
20//! // handler code
21//! }
22//!
23//! #[tokio::main]
24//! async fn main() {
25//! let app = Router::new()
26//! .route(__get_user_route, routing::get(get_user));
27//!
28//! RustAPI::new(app)
29//! .port(3000)
30//! .serve()
31//! .await
32//! .unwrap();
33//! }
34//! ```
35//!
36//! # Examples
37//!
38//! See the `examples/` directory for complete working examples:
39//!
40//! - `basic-api`: Complete example with controllers, services, and DI
41
42// Core modules
43pub mod di;
44pub mod app;
45pub mod error;
46pub mod server;
47pub mod router;
48
49// Re-export core types
50pub use di::{Container, Injectable};
51pub use app::App;
52pub use error::{Error, Result};
53pub use server::RustAPI;
54pub use router::{Router, RouterExt};
55
56// Re-export routing methods from Axum
57// These are used to define route handlers (get, post, put, delete, etc.)
58pub mod routing {
59 pub use axum::routing::*;
60}
61
62// Re-export common middleware layers
63pub use tower_http::cors::CorsLayer;
64pub use tower_http::trace::TraceLayer;
65
66// Re-export macros
67pub use rust_api_macros::{
68 get,
69 post,
70 put,
71 delete,
72 patch,
73};
74
75// Re-export commonly used axum types
76pub use axum::{
77 Json,
78 extract::{Path, Query, State},
79 http::StatusCode,
80 response::{IntoResponse, Response},
81};
82
83// Re-export serde for user convenience
84pub use serde::{Serialize, Deserialize};
85
86/// Prelude module for convenient imports
87///
88/// Import everything you need with:
89/// ```ignore
90/// use rust_api::prelude::*;
91/// ```
92pub mod prelude {
93 pub use super::{
94 // Core
95 Container,
96 Injectable,
97 App,
98 Error,
99 Result,
100 Router,
101 RouterExt,
102 RustAPI,
103 router,
104 routing,
105
106 // Macros
107 get,
108 post,
109 put,
110 delete,
111 patch,
112
113 // Axum
114 Json,
115 Path,
116 Query,
117 State,
118 StatusCode,
119 IntoResponse,
120 Response,
121
122 // Middleware
123 CorsLayer,
124 TraceLayer,
125
126 // Serde
127 Serialize,
128 Deserialize,
129 };
130
131 // Also re-export tokio for async runtime
132 pub use tokio;
133}