pub struct Router { /* private fields */ }Expand description
Main router
Implementations§
Source§impl Router
impl Router
Sourcepub fn route(self, path: &str, method_router: MethodRouter) -> Router
pub fn route(self, path: &str, method_router: MethodRouter) -> Router
Add a route
Sourcepub fn state_type_ids(&self) -> &[TypeId]
pub fn state_type_ids(&self) -> &[TypeId]
Get state type IDs (for testing and debugging)
Sourcepub fn nest(self, prefix: &str, router: Router) -> Router
pub fn nest(self, prefix: &str, router: Router) -> Router
Nest another router under a prefix
All routes from the nested router will be registered with the prefix prepended to their paths. State from the nested router is merged into the parent router (parent state takes precedence for type conflicts).
§State Merging
When nesting routers with state:
- If the parent router has state of type T, it is preserved (parent wins)
- If only the nested router has state of type T, it is added to the parent
- State type tracking is merged to enable proper conflict detection
Note: Due to limitations of http::Extensions, automatic state merging
requires using the merge_state method for specific types.
§Example
use rustapi_core::{Router, get};
async fn list_users() -> &'static str { "List users" }
async fn get_user() -> &'static str { "Get user" }
let users_router = Router::new()
.route("/", get(list_users))
.route("/{id}", get(get_user));
let app = Router::new()
.nest("/api/users", users_router);
// Routes are now:
// GET /api/users/
// GET /api/users/{id}Sourcepub fn merge_state<S>(self, other: &Router) -> Router
pub fn merge_state<S>(self, other: &Router) -> Router
Merge state from another router into this one
This method allows explicit state merging when nesting routers. Parent state takes precedence - if the parent already has state of type S, the nested state is ignored.
§Example
#[derive(Clone)]
struct DbPool(String);
let nested = Router::new().state(DbPool("nested".to_string()));
let parent = Router::new()
.merge_state::<DbPool>(&nested); // Adds DbPool from nestedSourcepub fn registered_routes(&self) -> &HashMap<String, RouteInfo>
pub fn registered_routes(&self) -> &HashMap<String, RouteInfo>
Get registered routes (for testing and debugging)
Sourcepub fn method_routers(&self) -> &HashMap<String, MethodRouter>
pub fn method_routers(&self) -> &HashMap<String, MethodRouter>
Get method routers (for OpenAPI integration during nesting)