Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: handle empty paths #19

Merged
merged 2 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

- **Fast**: See benchmark

- **Micro**: The [src/lib.rs](src/lib.rs) file is ~407 lines of code (Includes comments)
- **Micro**: The [src/lib.rs](src/lib.rs) file is ~405 lines of code (Includes comments)

- **Flexible**:

Expand Down
6 changes: 2 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ impl<T> Node<T> {
NodeKind::Static(ref s) => {
let l = loc(s, p);

if l == 0 || l < s.len() {
if l < s.len() {
None
} else if l == s.len() && l == p.len() {
Some(
Expand Down Expand Up @@ -297,7 +297,7 @@ impl<T> PathTree<T> {
#[inline]
pub fn new() -> Self {
Self {
root: Node::new(NodeKind::Static("/".to_owned())),
root: Node::new(NodeKind::Static("".to_owned())),
params: 0,
}
}
Expand All @@ -310,8 +310,6 @@ impl<T> PathTree<T> {

let mut most = 0;

path = path.trim_start_matches('/');

if path.is_empty() {
node.data.replace(data);
return self;
Expand Down
7 changes: 5 additions & 2 deletions tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use rand::seq::SliceRandom;
fn new_tree() {
let mut tree: PathTree<usize> = PathTree::default();

const ROUTES: [&str; 13] = [
const ROUTES: [&str; 14] = [
"",
"/",
"/users",
"/users/:id",
Expand All @@ -21,7 +22,8 @@ fn new_tree() {
"/users/repos/*any",
];

const VALID_URLS: [&str; 13] = [
const VALID_URLS: [&str; 14] = [
"",
"/",
"/users",
"/users/fundon",
Expand All @@ -38,6 +40,7 @@ fn new_tree() {
];

let valid_res = vec![
vec![],
vec![],
vec![],
vec![("id", "fundon")],
Expand Down