| Crates.io | wildcard-trie |
| lib.rs | wildcard-trie |
| version | 0.1.0 |
| created_at | 2025-08-11 02:56:08.044897+00 |
| updated_at | 2025-08-11 02:56:08.044897+00 |
| description | A space-efficient router / radix-trie that supports wildcards. |
| homepage | https://github.com/AmitPr/wildcard-trie |
| repository | https://github.com/AmitPr/wildcard-trie |
| max_upload_size | |
| id | 1789498 |
| size | 21,311 |
A space-efficient radix trie implementation for URL routing with wildcard support in Rust. This crate provides fast path lookups with DoS attack resistance by compressing paths and preventing excessive node creation.
/* match any sub-pathO(path_length) instead of O(number_of_routes)/api/v1/users and /api/v1/posts share /api/v1/)debug feature)Add this to your Cargo.toml:
[dependencies]
wildcard-trie = "0.1.0"
Then use it in your code:
use wildcard_trie::Trie;
let mut trie = Trie::new();
// Insert routes
trie.insert("/api/*", "api_handler"); // Wildcard route
trie.insert("/api/users", "users_handler"); // Exact route (takes precedence)
trie.insert("/api/v1/posts", "posts_handler");
// Lookup routes
assert_eq!(trie.get("/api/users"), Some(&"users_handler")); // Exact match
assert_eq!(trie.get("/api/posts"), Some(&"api_handler")); // Wildcard match
assert_eq!(trie.get("/api/v1/posts"), Some(&"posts_handler"));
// Remove routes
trie.remove("/api/users");
assert_eq!(trie.get("/api/users"), Some(&"api_handler")); // Falls back to wildcard
Trie<T>new() -> Self - Creates an empty trieinsert(&mut self, path: &str, value: T) - Inserts a value at the given pathget(&self, path: &str) -> Option<&T> - Retrieves a value for the pathremove(&mut self, path: &str) -> Option<T> - Removes and returns a valueWhen compiled with the debug feature (enabled by default):
pretty_print(&self) -> String - Returns a tree visualization of the trie structurelet mut trie = Trie::new();
trie.insert("/api/*", "api_handler");
trie.insert("/api/users", "users_handler");
println!("{}", trie.pretty_print());
use wildcard_trie::Trie;
let mut router = Trie::new();
// Set up routes
router.insert("/", "home_page");
router.insert("/api/*", "api_fallback");
router.insert("/api/users", "list_users");
router.insert("/api/users/*", "user_operations");
router.insert("/static/*", "serve_static");
// Route requests
assert_eq!(router.get("/"), Some(&"home_page"));
assert_eq!(router.get("/api/users"), Some(&"list_users"));
assert_eq!(router.get("/api/users/123"), Some(&"user_operations"));
assert_eq!(router.get("/api/posts"), Some(&"api_fallback"));
assert_eq!(router.get("/static/css/main.css"), Some(&"serve_static"));
The trie automatically compresses common prefixes:
let mut trie = Trie::new();
// These routes share the "/api/v1/" prefix
trie.insert("/api/v1/users", "users");
trie.insert("/api/v1/posts", "posts");
trie.insert("/api/v1/comments", "comments");
// Only creates nodes for:
// - "/api/v1/" (shared prefix)
// - "users", "posts", "comments" (suffixes)
debug - Enables pretty-printing functionalityIt may be useful to disable the debug feature for code size:
[dependencies]
wildcard-trie = { version = "0.1.0", default-features = false }
The crate uses a radix trie (compressed trie) structure where:
Common prefixes are stored in single nodes
/api/v1/users and /api/v1/posts share the /api/v1/ prefixRoutes ending with /* act as fallbacks
Each node stores: