|
| 1 | +# AGENTS.md - RustAPI AI Agent Guide |
| 2 | + |
| 3 | +> This file helps AI coding agents understand the RustAPI codebase quickly. |
| 4 | +
|
| 5 | +## Project Overview |
| 6 | + |
| 7 | +**RustAPI** is a FastAPI-like web framework for Rust that combines Rust's performance and memory safety with FastAPI's developer experience. |
| 8 | + |
| 9 | +**Core Philosophy:** "DX-First" - endpoint = 1 function + 1 attribute macro |
| 10 | + |
| 11 | +**Goals:** |
| 12 | +- Make Rust API development feel as easy as FastAPI |
| 13 | +- Eliminate boilerplate: handler function + attribute macro = endpoint |
| 14 | +- Automatic OpenAPI documentation (planned) |
| 15 | +- Type-safe compile-time validation |
| 16 | + |
| 17 | +**Status:** Phase 1 MVP Complete ✅ | Phase 2 (Validation + OpenAPI) In Progress 🔄 |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## Crate Structure |
| 22 | + |
| 23 | +``` |
| 24 | +rustapi/ |
| 25 | +├── rustapi-rs/ # Public facade - published to crates.io |
| 26 | +├── rustapi-core/ # Core implementation |
| 27 | +├── rustapi-macros/ # Procedural macros |
| 28 | +└── examples/ |
| 29 | + └── hello-world/ # Example application |
| 30 | +``` |
| 31 | + |
| 32 | +| Crate | Purpose | |
| 33 | +|-------|---------| |
| 34 | +| `rustapi-rs` | **Public facade** - the crate users depend on, re-exports core + macros | |
| 35 | +| `rustapi-core` | **Core implementation** - router, extractors, handlers, server, errors | |
| 36 | +| `rustapi-macros` | **Procedural macros** - `#[get]`, `#[post]`, etc. (planned) | |
| 37 | + |
| 38 | +--- |
| 39 | + |
| 40 | +## Key Modules (rustapi-core) |
| 41 | + |
| 42 | +| Module | Responsibility | |
| 43 | +|--------|----------------| |
| 44 | +| `app.rs` | `RustApi` builder - main entry point with `new()`, `route()`, `get()`, `post()`, `state()`, `run()` | |
| 45 | +| `router.rs` | Radix tree router using `matchit`, method-based routing (GET, POST, PUT, DELETE, PATCH) | |
| 46 | +| `handler.rs` | `Handler` trait with implementations for 0-5 argument handlers | |
| 47 | +| `extract.rs` | Extractors: `Json<T>`, `Path<T>`, `Query<T>`, `State<T>`, `Body` | |
| 48 | +| `response.rs` | `IntoResponse` trait, helpers: `json()`, `created()`, `no_content()`, `text()` | |
| 49 | +| `error.rs` | `ApiError` struct, `Result<T>` type alias, JSON error format | |
| 50 | +| `server.rs` | Hyper 1.x server with graceful shutdown (Ctrl+C) | |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +## Coding Conventions |
| 55 | + |
| 56 | +### Import Pattern |
| 57 | +```rust |
| 58 | +use rustapi_rs::prelude::*; |
| 59 | +``` |
| 60 | + |
| 61 | +### Handler Signatures |
| 62 | +Handlers should be readable like documentation: |
| 63 | +```rust |
| 64 | +async fn get_user(Path(id): Path<u32>, State(db): State<Database>) -> Json<User> { |
| 65 | + // Business logic only |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +### Extractor Patterns |
| 70 | +| Extractor | Usage | Route Example | |
| 71 | +|-----------|-------|---------------| |
| 72 | +| `Path<T>` | URL path parameters | `/users/{id}` | |
| 73 | +| `Query<T>` | Query string params | `?page=1&limit=10` | |
| 74 | +| `Json<T>` | JSON request body | POST/PUT requests | |
| 75 | +| `State<T>` | Shared application state | Database connections | |
| 76 | +| `Body` | Raw request body | File uploads | |
| 77 | + |
| 78 | +### Response Patterns |
| 79 | +```rust |
| 80 | +Json(data) // 200 OK with JSON |
| 81 | +created(data) // 201 Created with JSON |
| 82 | +no_content() // 204 No Content |
| 83 | +text("hello") // 200 OK with plain text |
| 84 | +ApiError::not_found("User not found") // 404 JSON error |
| 85 | +``` |
| 86 | + |
| 87 | +### Path Parameter Syntax |
| 88 | +- Use `{param}` format in routes (converted to `:param` internally) |
| 89 | +- Example: `.get("/users/{id}", get_user)` extracts `id` parameter |
| 90 | + |
| 91 | +### Wrapper Philosophy |
| 92 | +**All dependencies are wrapped** - never exposed directly in public API: |
| 93 | +- `matchit` → Router abstraction |
| 94 | +- `hyper` → Server abstraction |
| 95 | +- `validator` (planned) → Validation abstraction |
| 96 | +- `utoipa` (planned) → Schema abstraction |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +## Standard JSON Error Format |
| 101 | + |
| 102 | +```json |
| 103 | +{ |
| 104 | + "error": { |
| 105 | + "type": "validation_error", |
| 106 | + "message": "Request validation failed", |
| 107 | + "fields": [ |
| 108 | + {"field": "email", "code": "email", "message": "Invalid email format"} |
| 109 | + ] |
| 110 | + } |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +--- |
| 115 | + |
| 116 | +## Build & Test Commands |
| 117 | + |
| 118 | +```powershell |
| 119 | +# Build the workspace |
| 120 | +cargo build |
| 121 | +
|
| 122 | +# Run the hello-world example |
| 123 | +cargo run -p hello-world |
| 124 | +
|
| 125 | +# Run all tests |
| 126 | +cargo test --workspace |
| 127 | +
|
| 128 | +# Check for errors without building |
| 129 | +cargo check --workspace |
| 130 | +
|
| 131 | +# Format code |
| 132 | +cargo fmt --all |
| 133 | +
|
| 134 | +# Lint |
| 135 | +cargo clippy --workspace |
| 136 | +``` |
| 137 | + |
| 138 | +### Testing Endpoints (PowerShell) |
| 139 | +```powershell |
| 140 | +Invoke-RestMethod -Uri http://127.0.0.1:8080/ |
| 141 | +Invoke-RestMethod -Uri http://127.0.0.1:8080/health |
| 142 | +Invoke-RestMethod -Uri http://127.0.0.1:8080/users/42 |
| 143 | +``` |
| 144 | + |
| 145 | +--- |
| 146 | + |
| 147 | +## Handler Trait System |
| 148 | + |
| 149 | +```rust |
| 150 | +pub trait Handler<T>: Clone + Send + Sync + 'static { |
| 151 | + fn call(self, req: Request) -> impl Future<Output = Response> + Send; |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +- Implemented for function pointers with **0-5 arguments** |
| 156 | +- Each argument must implement `FromRequest` or `FromRequestParts` |
| 157 | +- `FromRequestParts` - extractors that don't consume body (Path, Query, State) |
| 158 | +- `FromRequest` - extractors that consume body (Json, Body) |
| 159 | + |
| 160 | +--- |
| 161 | + |
| 162 | +## Development Phases |
| 163 | + |
| 164 | +### ✅ Phase 1 - MVP (Complete) |
| 165 | +- Workspace structure with 3 crates |
| 166 | +- Hyper 1.x server with graceful shutdown |
| 167 | +- Radix tree router (matchit) |
| 168 | +- Handler trait (0-5 args) |
| 169 | +- All core extractors: Json, Path, Query, State, Body |
| 170 | +- IntoResponse + response helpers |
| 171 | +- ApiError + Result type |
| 172 | +- Tracing integration |
| 173 | + |
| 174 | +### 🔄 Phase 2 - Validation + OpenAPI (Current) |
| 175 | +- `rustapi-validate` crate - wrapper for `validator` |
| 176 | +- `#[derive(Validate)]` macro |
| 177 | +- Validation rules: email, length, range, regex, non_empty |
| 178 | +- 422 JSON error format |
| 179 | +- `rustapi-openapi` crate - wrapper for `utoipa` |
| 180 | +- `/openapi.json` endpoint |
| 181 | +- Swagger UI at `/docs` |
| 182 | + |
| 183 | +### 📋 Phase 3-4 - Extras (Planned) |
| 184 | +- Tower middleware integration |
| 185 | +- JWT, CORS, rate limiting (`rustapi-extras`) |
| 186 | +- Additional extractors: Headers, Cookies, IpAddr |
| 187 | +- TestClient helper |
| 188 | +- Benchmark suite |
| 189 | + |
| 190 | +--- |
| 191 | + |
| 192 | +## Key Files to Read |
| 193 | + |
| 194 | +| Purpose | File | |
| 195 | +|---------|------| |
| 196 | +| Example usage | `examples/hello-world/src/main.rs` | |
| 197 | +| Core exports | `crates/rustapi-core/src/lib.rs` | |
| 198 | +| App builder | `crates/rustapi-core/src/app.rs` | |
| 199 | +| Router impl | `crates/rustapi-core/src/router.rs` | |
| 200 | +| Extractors | `crates/rustapi-core/src/extract.rs` | |
| 201 | +| Error handling | `crates/rustapi-core/src/error.rs` | |
| 202 | +| Project context | `memories/rustapi_memory_bank.md` | |
| 203 | +| Task list | `memories/TASKLIST.md` | |
| 204 | + |
| 205 | +--- |
| 206 | + |
| 207 | +## Quick Reference |
| 208 | + |
| 209 | +```rust |
| 210 | +use rustapi_rs::prelude::*; |
| 211 | + |
| 212 | +#[tokio::main] |
| 213 | +async fn main() { |
| 214 | + RustApi::new() |
| 215 | + .get("/", index) |
| 216 | + .get("/users/{id}", get_user) |
| 217 | + .post("/users", create_user) |
| 218 | + .state(AppState::new()) |
| 219 | + .run("127.0.0.1:8080") |
| 220 | + .await; |
| 221 | +} |
| 222 | + |
| 223 | +async fn index() -> &'static str { |
| 224 | + "Hello, RustAPI!" |
| 225 | +} |
| 226 | + |
| 227 | +async fn get_user(Path(id): Path<u32>) -> Json<User> { |
| 228 | + Json(User { id, name: "Alice".into() }) |
| 229 | +} |
| 230 | + |
| 231 | +async fn create_user(Json(payload): Json<CreateUser>) -> impl IntoResponse { |
| 232 | + created(User { id: 1, name: payload.name }) |
| 233 | +} |
| 234 | +``` |
| 235 | + |
| 236 | +--- |
| 237 | + |
| 238 | +## MSRV & License |
| 239 | + |
| 240 | +- **Minimum Supported Rust Version:** 1.75+ |
| 241 | +- **License:** MIT OR Apache-2.0 |
0 commit comments