rust_api/router.rs
1//! Router utilities for RustAPI framework
2//!
3//! Provides a builder API for creating routers without directly exposing Axum types.
4//! Users interact through the router module rather than importing Router directly.
5
6/// Re-export Axum's Router type
7///
8/// Note: In Axum's type system, `Router<S>` means a router that "needs" state of type S.
9/// - `Router<()>` = a stateless router (needs no state)
10/// - `Router<AppState>` = a router that needs AppState to be provided via `.with_state()`
11///
12/// Users should use `router::build()` to create routers rather than importing this type.
13pub type Router<S = ()> = axum::Router<S>;
14
15/// Create a new router builder
16///
17/// This is the recommended entry point for creating routers. Returns an Axum Router
18/// that can be configured using the fluent builder API.
19///
20/// # Example
21///
22/// ```ignore
23/// use rust_api_core::{router, routing};
24///
25/// let app = router::build()
26/// .route("/health", routing::get(health_check))
27/// .layer(TraceLayer::new_for_http())
28/// .finish();
29/// ```
30pub fn build() -> Router<()> {
31 axum::Router::new()
32}
33
34/// Extension trait to add a `finish()` method to Router
35///
36/// This provides a clear endpoint to router building, making the API more explicit.
37pub trait RouterExt<S> {
38 /// Finishes building the router and returns it
39 ///
40 /// This is a no-op that just returns self, but makes the builder API more explicit.
41 fn finish(self) -> Router<S>;
42}
43
44impl<S> RouterExt<S> for Router<S> {
45 fn finish(self) -> Router<S> {
46 self
47 }
48}
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn test_router_creation() {
56 let _router = build();
57 }
58
59 #[test]
60 fn test_router_finish() {
61 let _router = build().finish();
62 }
63}