| Crates.io | hyperchad_router |
| lib.rs | hyperchad_router |
| version | 0.1.4 |
| created_at | 2025-05-07 15:34:56.515758+00 |
| updated_at | 2025-07-21 20:04:31.917937+00 |
| description | HyperChad Router package |
| homepage | |
| repository | https://github.com/MoosicBox/MoosicBox |
| max_upload_size | |
| id | 1664030 |
| size | 92,994 |
Async routing system for HyperChad applications with request handling and navigation.
The HyperChad Router package provides:
Add this to your Cargo.toml:
[dependencies]
hyperchad_router = { path = "../hyperchad/router" }
# Enable additional features
hyperchad_router = {
path = "../hyperchad/router",
features = ["serde", "form", "static-routes"]
}
use hyperchad_router::{Router, RouteRequest, RoutePath};
use hyperchad_renderer::Content;
// Create router
let router = Router::new()
.with_route("/", |_req| async {
Some(Content::Html("<h1>Home</h1>".to_string()))
})
.with_route("/about", |_req| async {
Some(Content::Html("<h1>About</h1>".to_string()))
});
// Navigate to route
let content = router.navigate("/").await?;
use hyperchad_router::RoutePath;
// Literal route
let home_route = RoutePath::Literal("/".to_string());
// Multiple literals
let api_routes = RoutePath::Literals(vec![
"/api/v1".to_string(),
"/api/v2".to_string(),
]);
// Prefix matching
let static_route = RoutePath::LiteralPrefix("/static/".to_string());
// From string slice arrays
let routes: RoutePath = &["/api", "/v1", "/users"][..].into();
use hyperchad_router::{RouteRequest, RequestInfo, ClientInfo, ClientOs};
// Create request with client info
let client_info = ClientInfo {
os: ClientOs {
name: "Windows".to_string(),
},
};
let request = RouteRequest::from(("/api/users", client_info));
// Access request properties
println!("Path: {}", request.path);
println!("Method: {:?}", request.method);
println!("OS: {}", request.info.client.os.name);
use serde::Deserialize;
#[derive(Deserialize)]
struct LoginForm {
username: String,
password: String,
}
let router = Router::new()
.with_route_result("/login", |req| async move {
if req.method == Method::Post {
let form: LoginForm = req.parse_form()?;
// Process login
Ok(Some(Content::Html("Login successful".to_string())))
} else {
Ok(Some(Content::Html(r#"
<form method="post">
<input name="username" type="text" required>
<input name="password" type="password" required>
<button type="submit">Login</button>
</form>
"#.to_string())))
}
});
use serde::{Deserialize, Serialize};
#[derive(Deserialize)]
struct ApiRequest {
name: String,
email: String,
}
#[derive(Serialize)]
struct ApiResponse {
id: u32,
message: String,
}
let router = Router::new()
.with_route_result("/api/users", |req| async move {
let user_data: ApiRequest = req.parse_body()?;
// Process user creation
let response = ApiResponse {
id: 123,
message: "User created".to_string(),
};
Ok(Some(Content::Json(serde_json::to_string(&response)?)))
});
// Static routes (compiled at build time)
let router = Router::new()
.with_static_route("/static/css/style.css", |_req| async {
Some(Content::Css(include_str!("../static/style.css").to_string()))
})
.with_static_route("/static/js/app.js", |_req| async {
Some(Content::JavaScript(include_str!("../static/app.js").to_string()))
});
// Spawn navigation in background
let handle = router.navigate_spawn("/api/data");
// Wait for navigation result
match handle.await {
Ok(Ok(())) => println!("Navigation successful"),
Ok(Err(e)) => println!("Navigation error: {}", e),
Err(e) => println!("Task error: {}", e),
}
// Send navigation result to receiver
router.navigate_send("/dashboard").await?;
// Wait for content on receiver
if let Some(content) = router.wait_for_navigation().await {
// Handle received content
}
use hyperchad_router::{NavigateError, ParseError};
match router.navigate("/api/endpoint").await {
Ok(Some(content)) => {
// Handle successful navigation
}
Ok(None) => {
// Route returned no content
}
Err(NavigateError::InvalidPath) => {
// Invalid path provided
}
Err(NavigateError::Handler(e)) => {
// Handler returned an error
}
Err(NavigateError::Sender) => {
// Channel sender error
}
}
pub struct ClientInfo {
pub os: ClientOs,
}
pub struct ClientOs {
pub name: String, // "Windows", "macOS", "Linux", etc.
}
Automatic OS detection using the os_info crate provides default client information.
serde: Enable JSON and form parsingform: Enable multipart form supportstatic-routes: Enable static route compilationThis package is designed for: