rou3-rs
A flexible and fast HTTP router for Rust, inspired by rou3-js.
rou3-rs provides a high-performance routing solution for Rust applications, drawing inspiration from the design principles of rou3-js. It uses a trie-based structure with efficient hash map lookups for dynamic path segments and an optimized map for purely static routes.
Features
- Versatile Route Matching:
- Static routes: e.g.,
/home,/about/contact - Parameterized routes: e.g.,
/users/:id,/posts/:category/:slug - Wildcard routes:
- Single segment wildcard:
/files/*(captures one segment) - Multi-segment (catch-all) wildcard:
/assets/**:filepath(must be at the end)
- Single segment wildcard:
- Optional parameters: e.g.,
/search/:query?(matches/search/and/search/term)
- Static routes: e.g.,
- Method-based Routing: Supports standard HTTP methods (GET, POST, PUT, DELETE, etc.) and an "ANY" method (empty string
"") to match any HTTP method. - Efficient:
- Trie structure with
AHashMapfor fast dynamic dispatch. - Dedicated
static_mapfor instant lookups of purely static paths.
- Trie structure with
- Dynamic Modification: Add and remove routes at runtime.
find_all_routes: Retrieve all routes that match a given path, useful for middleware or complex dispatch logic.- Thread-Safe: Core router operations are thread-safe using
parking_lot::RwLock. - Clear Error Handling: Provides a
RouterErrorenum for robust error management.
Installation
Add rou3-rs to your Cargo.toml:
[]
= "0.1.0"
Usage
Here's a quick overview of how to use rou3-rs:
use ;
use HashMap; // For easily checking params
Benchmarks
rou3-rs is designed with performance in mind. It includes benchmarks comparing it against other popular Rust routers. For detailed results, please see the benchmark files in the benches directory and run cargo bench.
Contributing
Contributions are welcome! Please feel free to submit issues, fork the repository, and create pull requests.
Ways to contribute:
- Reporting bugs
- Suggesting new features or enhancements
- Improving documentation
- Adding more test cases
- Optimizing performance
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgements
- The design and API are heavily inspired by h3js/rou3.