2 个版本
0.1.1 | 2022年1月28日 |
---|---|
0.1.0 | 2022年1月27日 |
#23 in #radix
27KB
421 代码行,不包括注释
radixtree
为路由器实现的基数树,并提供 CRUD 操作。
Radixtree 是 treemux 的一部分,它基于 treemux 添加了更新和删除功能。
用法
将以下内容添加到您的 Cargo.toml
radixtree = "0.1.0"
插入
use radixtree::{Node, Method};
fn main() {
let mut tree = Node::new();
tree.insert(Method::GET, "/", "GET");
tree.insert(Method::POST, "/", "POST");
tree.insert(Method::PUT, "/", "PUT");
tree.insert(Method::DELETE, "/", "DELETE");
let result = tree.search(Method::GET, "/");
assert!(result.is_some());
assert_eq!(result.unwrap().value(), &"GET");
let result = tree.search(Method::POST, "/");
assert!(result.is_some());
assert_eq!(result.unwrap().value(), &"POST");
let result = tree.search(Method::PUT, "/");
assert!(result.is_some());
assert_eq!(result.unwrap().value(), &"PUT");
let result = tree.search(Method::DELETE, "/");
assert!(result.is_some());
assert_eq!(result.unwrap().value(), &"DELETE");
}
更新
use radixtree::{Node, Method};
fn main() {
let mut tree = Node::new();
tree.insert(Method::GET, "/", "GET");
tree.insert(Method::POST, "/", "POST");
tree.insert(Method::PUT, "/", "PUT");
tree.insert(Method::DELETE, "/", "DELETE");
tree.update(Method::GET, "/", "UPDATE GET");
tree.update(Method::POST, "/", "UPDATE POST");
tree.update(Method::PUT, "/", "UPDATE PUT");
tree.update(Method::DELETE, "/", "UPDATE DELETE");
let result = tree.search(Method::GET, "/");
assert!(result.is_some());
assert_eq!(result.unwrap().value(), &"UPDATE GET");
let result = tree.search(Method::POST, "/");
assert!(result.is_some());
assert_eq!(result.unwrap().value(), &"UPDATE POST");
let result = tree.search(Method::PUT, "/");
assert!(result.is_some());
assert_eq!(result.unwrap().value(), &"UPDATE PUT");
let result = tree.search(Method::DELETE, "/");
assert!(result.is_some());
assert_eq!(result.unwrap().value(), &"UPDATE DELETE");
}
删除
use radixtree::{Node, Method};
fn main() {
let mut tree = Node::new();
tree.insert(Method::GET, "/", "GET");
tree.insert(Method::POST, "/", "POST");
tree.insert(Method::PUT, "/", "PUT");
tree.insert(Method::DELETE, "/", "DELETE");
tree.remove("/");
let result = tree.search(Method::GET, "/");
assert!(result.is_none());
let result = tree.search(Method::POST, "/");
assert!(result.is_none());
let result = tree.search(Method::PUT, "/");
assert!(result.is_none());
let result = tree.search(Method::DELETE, "/");
assert!(result.is_none());
}
参数路径
use radixtree::{Node, Method};
fn main() {
let mut tree = Node::new();
tree.insert(Method::GET, "/user/$id", "GET");
let result = tree.search(Method::GET, "/user/1");
assert!(result.is_some());
assert_eq!(result.unwrap().value(), &"GET");
tree.update(Method::GET, "/user/$id", "UPDATE GET");
let result = tree.search(Method::GET, "/user/1");
assert!(result.is_some());
assert_eq!(result.unwrap().value(), &"UPDATE GET");
tree.remove("/user/$id");
let result = tree.search(Method::GET, "/user/1");
assert!(result.is_none());
}
通配符星号
use radixtree::{Node, Method};
fn main() {
let mut tree = Node::new();
tree.insert(Method::GET, "/image/*", "GET");
let result = tree.search(Method::GET, "/image/hello.jpeg");
assert!(result.is_some());
assert_eq!(result.unwrap().value(), &"GET");
let result = tree.search(Method::GET, "/image/jpg/hello.jpg");
assert!(result.is_some());
assert_eq!(result.unwrap().value(), &"GET");
let result = tree.search(Method::GET, "/image/png/hello.png");
assert!(result.is_some());
assert_eq!(result.unwrap().value(), &"GET");
tree.update(Method::GET, "/image/*", "UPDATE GET");
let result = tree.search(Method::GET, "/image/hello.jpeg");
assert!(result.is_some());
assert_eq!(result.unwrap().value(), &"UPDATE GET");
tree.remove("/image/*");
let result = tree.search(Method::GET, "/image/hello.jpeg");
assert!(result.is_none());
}
匹配规则
以下是一些有效的 URL 路径示例
- /abc/def
- /favicon.ico
- /user/$id
- /id/$id/name/$name
- /$year/$month
- /$year/$month/$day
- /images/*
- /images/$category/*
注意,上述所有 URL 路径可能同时存在于基数树中。
以 $
开头的路径表示参数路径。参数路径只匹配单个路径段。也就是说,路径 /user/$id
将匹配 /user/1
或 /user/2
,但不能匹配 /user/1/2
。
通配符 *
可以匹配任何路径。例如,路径 /image/*
将匹配 /image/png/hello.png
、/image/jpg/hello.jpg
或 image/hello.jpeg
。
匹配优先级
- 静态路径具有最高优先级。
- 参数路径具有第二优先级。
- 最后,通配符
*
匹配静态路径和参数路径都不匹配的路径。
作者
郭振伟 (Ilqjx)
许可证
本项目采用 MIT 许可证。