Skip to content

Commit 70fc7c5

Browse files
committed
allow empty paths
1 parent 160bce7 commit 70fc7c5

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "routrie"
3-
version = "0.7.0"
3+
version = "0.8.0"
44
edition = "2021"
55
description = "Rust port of the Python stdlib routrie modules"
66
readme = "README.md"

src/lib.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,37 @@ use pyo3::prelude::*;
44
#[pyclass(module = "routrie._routrie")]
55
struct Router {
66
router: PathTree<Py<PyAny>>,
7+
// path-tree dropped support for empty values
8+
// so we implement it here as a special case
9+
empty: Option<Py<PyAny>>,
710
}
811

12+
type MatchedRoute<'a> = (&'a Py<PyAny>, Vec<(&'a str, &'a str)>);
13+
914
#[pymethods]
1015
impl Router {
1116
#[new]
1217
fn new() -> Self {
1318
Router {
1419
router: PathTree::new(),
20+
empty: None,
1521
}
1622
}
17-
fn insert(&mut self, path: &str, data: &PyAny, py: Python) -> () {
18-
self.router.insert(path, data.into_py(py));
23+
fn insert(&mut self, path: &str, data: &PyAny, py: Python) {
24+
match path.is_empty() {
25+
true => self.empty = Some(data.into()),
26+
false => {
27+
self.router.insert(path, data.into_py(py));
28+
}
29+
}
1930
}
20-
fn find<'m>(&'m self, path: &'m str) -> Option<(&'m Py<PyAny>, Vec<(&'m str, &'m str)>)> {
21-
match self.router.find(path) {
22-
None => None,
23-
Some(path) => Some((path.value, path.params())),
31+
fn find<'a>(&'a self, path: &'a str) -> Option<MatchedRoute<'a>> {
32+
match path.is_empty() {
33+
true => self.empty.as_ref().map(|v| (v, vec![])),
34+
false => match self.router.find(path) {
35+
None => None,
36+
Some(path) => Some((path.value, path.params())),
37+
},
2438
}
2539
}
2640
}

0 commit comments

Comments
 (0)