HyperChad Router
Async routing system for HyperChad applications with request handling and navigation.
Overview
The HyperChad Router package provides:
- Async Routing: Full async request routing and handling
- Route Matching: Flexible route path matching with literals and prefixes
- Request Processing: Comprehensive request parsing and handling
- Form Support: Multipart form and JSON body parsing
- Client Detection: OS and client information detection
- Navigation System: Programmatic navigation and content delivery
Features
Route Matching
- Literal Routes: Exact path matching
- Multiple Literals: Match against multiple possible paths
- Prefix Matching: Match paths with specific prefixes
- Flexible Patterns: Support for various route patterns
Request Handling
- HTTP Methods: Support for GET, POST, PUT, DELETE, PATCH
- Query Parameters: Automatic query string parsing
- Headers: Complete header access and manipulation
- Cookies: Cookie parsing and management
- Body Parsing: JSON and form data parsing
Form Processing
- Multipart Forms: Complete multipart form support
- File Uploads: File upload handling with base64 encoding
- JSON Bodies: JSON request body parsing
- URL Encoded: URL-encoded form data support
- Content-Type Detection: Automatic content type handling
Client Information
- OS Detection: Automatic operating system detection
- Client Info: Structured client information
- Request Context: Rich request context and metadata
Installation
Add this to your Cargo.toml:
[]
# Default features include: form, format, json, serde, static-routes
= "0.1.4"
# Disable default features and select specific ones
= {
version = "0.1.4",
= false,
= ["serde", "json"]
}
# Enable additional features beyond defaults
= {
version = "0.1.4",
= ["syntax-highlighting", "simd"]
}
Usage
Basic Router Setup
use ;
use Content;
// Create router
let router = new
.with_route
.with_route;
// Navigate to route
let content = router.navigate.await?;
Route Patterns
use RoutePath;
// Literal route
let home_route = Literal;
// Multiple literals
let api_routes = Literals;
// Prefix matching
let static_route = LiteralPrefix;
// From string slice arrays
let routes: RoutePath = &.into;
Request Information
use ;
// Create request with client info
let client_info = ClientInfo ;
let request = from;
// Access request properties
println!;
println!;
println!;
Form Handling
use Deserialize;
use Method;
let router = new
.with_route_result;
JSON Body Parsing
use ;
let router = new
.with_route_result;
Static Routes
use Content;
use Bytes;
// Static routes (compiled at build time)
let router = new
.with_static_route
.with_static_route;
Navigation and Content Delivery
// Spawn navigation in background
let handle = router.navigate_spawn;
// Wait for navigation result
match handle.await
// Send navigation result to receiver
router.navigate_send.await?;
// Wait for content on receiver
if let Some = router.wait_for_navigation.await
Error Handling
use ;
match router.navigate.await
Route Types
RoutePath Variants
- Literal(String): Exact string match
- Literals(Vec): Match any of multiple strings
- LiteralPrefix(String): Match strings with specific prefix
Content Types
The Content enum (from hyperchad_renderer) supports:
- View(Box<View>): HTML view content (created from strings, Containers, or Views)
- Json(serde_json::Value): JSON responses (requires
jsonfeature) - Raw { data, content_type }: Raw content with custom MIME type
Client Information
ClientInfo Structure
Default Client Detection
Automatic OS detection using the os_info crate provides default client information.
Feature Flags
serde: Enable JSON and form parsing (enabled by default)form: Enable multipart form support (enabled by default)static-routes: Enable static route compilation (enabled by default)json: Enable JSON content support (enabled by default)format: Enable HTML formatting (enabled by default)syntax-highlighting: Enable syntax highlighting supportsimd: Enable SIMD optimizations
Dependencies
- Switchy/Switchy Async: Async runtime abstraction with Tokio support
- Futures: Future utilities
- Bytes: Efficient byte handling
- Flume: Channel communication
- QString: Query string parsing
- OS Info: Operating system detection
- Serde: Optional serialization support
- Mime Multipart: Optional form parsing
- Base64: Optional base64 encoding for file uploads
Integration
This package is designed for:
- Web Applications: Full-featured web application routing
- API Servers: RESTful API endpoint handling
- Form Processing: Web form and file upload handling
- SPA Routing: Single-page application routing
- Content Management: Dynamic content delivery systems