This module provides a flexible routing system for interactive applications. It defines core types and functionality for managing navigation state and route configuration, designed for use with modern UI frameworks.
The router system consists of three main components:
- Route Definition (
route.rs) - Defines individual routes with metadata - Router State Management (
router.rs) - Manages navigation state and history - Router Configuration (
router.rs) - Configures available routes and settings - UI Integration (
integration.rs) - Provides UI components and hooks for seamless integration
- Structured Error Handling:
RouterErrorenum andRouterResult<T>type alias for better error handling - Ergonomic Navigation API:
router.navigate("route_id")accepts&strdirectly - UI-Friendly Router Handle:
RouterHandlefor shared navigation control in UI components - Streamlined Builder API: Methods like
route()anddefault()for less boilerplate - Type Safety:
RouteIdimplementsDisplay,AsRef<str>, andBorrow<str> - Clean Interface: Focused API design for common routing patterns
Represents a single route with metadata:
use trae_agent_rs_core::interactive::router::Route;
let route = Route::new("home", "Home Page");Manages the current navigation state:
use trae_agent_rs_core::interactive::router::{RouterState, RouteId};
let mut state = RouterState::new("home".into());
// Navigate to a new route
state.navigate_to("about".into());
// Go back to previous route
if state.can_go_back() {
state.go_back();
}Configures the router with available routes:
use trae_agent_rs_core::interactive::router::{RouterConfig, Route};
let config = RouterConfig::new()
.add_route(Route::new("home", "Home"))
.add_route(Route::new("about", "About"))
.with_default_route("home".into());The main router that combines configuration and state:
use trae_agent_rs_core::interactive::router::{Router, RouterConfig, Route, RouterResult};
let config = RouterConfig::new()
.add_route(Route::new("home", "Home"))
.with_default_route("home".into());
// Create router
let mut router = Router::new(config)?;
// Navigate to routes
router.navigate("about")?;
// Get current route information
if let Some(current_route) = router.current_route() {
println!("Current route: {}", current_route.name);
}- Route Management: Define routes with IDs, names, descriptions, and metadata
- Navigation History: Automatic history tracking with configurable limits
- Default Routes: Support for default route selection
- Route Validation: Ensures navigation only to existing routes
- Flexible Configuration: Builder pattern for easy router setup
This router module is designed for modern UI framework applications. It provides UI-specific components for easy integration:
use crate::interactive::router::{UIRouterBuilder, RouterHandle, use_router};
// Build router
let build_result = UIRouterBuilder::new()
.route("home", "Home", |_hooks| element! { HomePage() }.into())
.route("settings", "Settings", |_hooks| element! { SettingsPage() }.into())
.default("home")
.build()?;
// Use in UI component
element! {
UIRouter(
handle: build_result.handle.clone(),
pages: build_result.props.pages,
fallback_page: build_result.props.fallback_page,
)
}
// Navigate from any UI component
#[component]
fn NavigationButton(handle: &RouterHandle) -> impl Into<AnyElement<'static>> {
let current = use_router(handle);
element! {
Button(on_press: move || {
let _ = handle.navigate("settings");
}) {
Text(content: "Go to Settings")
}
}
}The router system provides clear error messages for common issues:
- Route not found during navigation
- No routes configured
- Invalid initial route configuration
All operations that can fail return Result<T, String> with descriptive error messages.
All router types are designed to be thread-safe when needed:
RouteandRouterConfigimplementClonefor easy sharingRoutercan be wrapped inArc<Mutex<>>for shared mutable access- Navigation operations are atomic and consistent
The router system is designed to be extensible:
- Add custom metadata to routes for application-specific needs
- Implement custom navigation logic by extending
RouterState - Create specialized router configurations for different use cases
This module provides seamless integration with modern UI frameworks:
The integration module provides:
UIRouter: UI component for rendering pages based on routesUIRouterProps: Component properties containing router state and page renderersUIRouterBuilder: Builder pattern for creating router configurationsPageRenderer: Type alias for page render functions
use crate::interactive::router::{
UIRouter, UIRouterBuilder, Route
};
// Create router with pages
let build_result = UIRouterBuilder::new()
.route("home", "Home", |_hooks| element! { Text(content: "Home Page") }.into())
.route("settings", "Settings", |_hooks| element! { Text(content: "Settings Page") }.into())
.default("home")
.build()?;
// Use in UI component
element! {
UIRouter(
handle: build_result.handle,
pages: build_result.props.pages,
fallback_page: build_result.props.fallback_page
)
}This router implementation is designed as a reusable routing solution. The modular design allows it to be extracted and used in other applications.
use crate::interactive::router::{Router, RouterConfig, Route, RouterResult};
let config = RouterConfig::new()
.add_route(Route::new("home", "Home"))
.with_default_route("home".into());
// Create router
let mut router = Router::new(config)?;
// Navigate to routes
router.navigate("about")?;
// Get current route information
if let Some(current_route) = router.current_route() {
println!("Current route: {}", current_route.name);
}let build = UIRouterBuilder::new()
.route("home", "Home", |_| element!{...}.into())
.route("settings", "Settings", |_| element!{...}.into())
.default("home")
.build()?;
element! {
UIRouter(
handle: build.handle,
pages: build.props.pages,
fallback_page: build.props.fallback_page
)
}// Get current route
let current = use_router(&handle);
// Navigate from UI components
handle.navigate("new_route")?;The core routing functionality (Route, Router, RouterState, RouterConfig) can be extracted and used independently with other UI frameworks or even in non-UI contexts.