How hmathsan/rest_framework_rs works
Project Type and Purpose
• This is a small, modular Rust REST-framework and sample server application (not a distributed microservice).
• Its goal is educational: to provide a basic HTTP server with JSON (de)serialization, routing via macros, and a toy “mount” interface for handlers.
File Structure
• Top-level crates:
• macros/ — a procedural-macro crate generating request/response boilerplate
• server/ — the core HTTP server framework (routing, parsing, model types, utilities)
• src/main.rs — the example binary that ties everything together
• Inside server/:
• server.rs — TCP listener setup and event loop
• model/ — request/response entities and enums (status codes, parse errors, HTTP methods)
• macros.rs & macros/ — support for procedural routing macros (get!, post!, etc.)
• server_utils/ — default handlers and helpers
• Dependencies: serde (for JSON), std::net (TCP), std::collections (HashMap)
Extract Architectural Insights from README
• The framework uses derive-macros to auto-implement Serialize/Deserialize and routing traits
• Handlers have the signature (headers, params, typed-body) → ResponseEntity
• Mounting is macro-driven, e.g. get!["/path", handler_fn]
Main Components and Their Interactions
a. Components:
1. Client (e.g., Postman/Browser)
2. TCP Listener (std::net::TcpListener)
3. HTTP Parser & Router (server::Server)
4. Routing Macros (macros crate + server/src/macros)
5. Model Layer (server::model)
6. Handler Functions (user code in main.rs)
7. Response Serializer (serde via ResponseEntity)
b. Data Flow:
• Client → TCP Listener → HTTP Parser → Router matches path & method
• Router invokes macro-generated dispatch code → calls user handler
• Handler returns ResponseEntity → JSON serialized → TCP write back to client
c. Architectural Patterns:
• Layered design (Network I/O → HTTP/Core → Model → User Handlers)
• Code-generation via procedural macros separates routing boilerplate from core logic
• Loose coupling: server core doesn’t depend on any specific handler code until mount time
d. Key Technologies:
• Rust crates: serde, serde_derive, std::net, std::collections
• Procedural macros for #[derive(Request)]/#[derive(Response)] and get!/post! macros
• Modular crate structure (macros, server, binary)