| Crates.io | prefix_tree_map |
| lib.rs | prefix_tree_map |
| version | 0.2.1 |
| created_at | 2021-12-05 10:36:30.69527+00 |
| updated_at | 2022-02-28 18:15:20.209868+00 |
| description | A Rust implementation of generic prefix tree (trie) map with wildcard capture support |
| homepage | |
| repository | https://github.com/EAimTY/prefix_tree_map |
| max_upload_size | |
| id | 492635 |
| size | 60,330 |
A Rust implementation of generic prefix tree (trie) map with wildcard capture support.
Ord and the Clone trait.build() is called, Binary Heaps are converted into sorted Vecs. We can't push any item to the Vec without a sort.word can only be matched by w**d, not w*d.use prefix_tree_map::PrefixTreeMapBuilder;
let mut map_builder = PrefixTreeMapBuilder::new();
// To insert an exact key path, call `insert_exact()`
map_builder.insert_exact(["path", "to", "value"], "value0");
// Insert into a existed key path could overwrite the value in it
map_builder.insert_exact(["path", "to", "value"], "value1");
// To insert an key path with wildcards, mark key parts using `prefix_tree_map::KeyPart` and call `insert()`
use prefix_tree_map::KeyPart;
map_builder.insert(
[
KeyPart::Exact("path"),
KeyPart::Wildcard("to"),
KeyPart::Exact("value"),
],
"value2",
);
// Anything implemented trait `FromIterator` can be inserted as a key path:
let path = "path/to/anothor/value";
map_builder.insert_exact(path.split('/'), "value3");
let anothor_path = "path/to/:some/value";
map_builder.insert(
anothor_path.split('/').map(|part| {
if part.starts_with(':') {
KeyPart::Wildcard(part)
} else {
KeyPart::Exact(part)
}
}),
"value4",
);
// Then build the map
let map = map_builder.build();
// Find a value without matching any wildcard part
assert_eq!(
Some(&"value3"),
map.find_exact(&["path", "to", "anothor", "value"])
);
// Find a value with matching wildcard part
assert_eq!(Some(&"value4"), map.find(&["path", "to", "a", "value"]));
// `KeyPart::Exact` has a higher match priority than `KeyPart::Wildcard`
assert_eq!(Some(&"value3"), map.find(&["path", "to", "anothor", "value"]));
// Find a value with matching wildcard part, and store captured matched wildcard parts in a map
use std::collections::HashMap;
let mut captures = HashMap::new();
assert_eq!(
Some(&"value4"),
map.find_and_capture(&["path", "to", "a", "value"], &mut captures)
);
assert_eq!(Some(&"a"), captures.get(&":some"));
Customizing Capture map:
struct Map {
pub data: [Option<String>; 2],
}
impl Map {
fn new() -> Self {
Self { data: [None, None] }
}
}
use prefix_tree_map::Captures;
impl Captures<&str, &str> for Map {
fn insert(&mut self, key: &str, value: &str) {
match key {
":user_id" => self.data[0] = Some(value.to_string()),
":product_id" => self.data[1] = Some(value.to_string()),
_ => (),
}
}
}
fn capture() {
use prefix_tree_map::{KeyPart, PrefixTreeMapBuilder};
let mut builder = PrefixTreeMapBuilder::new();
builder.insert(
[
KeyPart::Exact("user"),
KeyPart::Wildcard(":user_id"),
KeyPart::Exact("home"),
],
"user",
);
builder.insert(
[
KeyPart::Exact("product"),
KeyPart::Wildcard(":product_id"),
KeyPart::Exact("info"),
],
"product",
);
let map = builder.build();
let mut captures = Map::new();
map.find_and_capture(
&"/user/00000/home".split('/').collect::<Vec<_>>(),
&mut captures,
);
assert_eq!("00000", captures.data[0].as_ref().unwrap());
}
For more infomation, check out examples/router.rs
no_stdOpt out the std feature by disabling default-features in Cargo.toml to remove the Rust standard library dependency.
Check examples.
GNU General Public License v3.0