Skip to content

Commit

Permalink
merge into master
Browse files Browse the repository at this point in the history
  • Loading branch information
ibraheemdev committed Feb 3, 2024
2 parents 6d703bf + 7766d45 commit eff698a
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 65 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
name = "matchit"
version = "0.7.1"
version = "0.7.3"
license = "MIT AND BSD-3-Clause"
authors = ["Ibraheem Ahmed <ibraheem@ibraheem.ca>"]
edition = "2021"
description = "A blazing fast URL router."
description = "A high performance, zero-copy URL router."
categories = ["network-programming", "algorithms"]
keywords = ["router", "path", "tree", "match", "url"]
repository = "https://github.com/ibraheemdev/matchit"
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# `matchit`

[![Documentation](https://img.shields.io/badge/docs-0.7.1-4d76ae?style=for-the-badge)](https://docs.rs/matchit)
[![Documentation](https://img.shields.io/badge/docs-0.7.3-4d76ae?style=for-the-badge)](https://docs.rs/matchit)
[![Version](https://img.shields.io/crates/v/matchit?style=for-the-badge)](https://crates.io/crates/matchit)
[![License](https://img.shields.io/crates/l/matchit?style=for-the-badge)](https://crates.io/crates/matchit)

A blazing fast URL router.
A high performance, zero-copy URL router.

```rust
use matchit::Router;
Expand Down Expand Up @@ -41,7 +41,7 @@ assert!(m.at("/users").is_err());

### Catch-all Parameters

Catch-all parameters start with `*` and match everything after the `/`. They must always be at the **end** of the route:
Catch-all parameters start with `*` and match anything until the end of the path. They must always be at the **end** of the route:

```rust,ignore
let mut m = Router::new();
Expand Down
18 changes: 17 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::tree::Node;
use crate::tree::{denormalize_params, Node};

use std::fmt;

Expand Down Expand Up @@ -43,18 +43,34 @@ impl std::error::Error for InsertError {}

impl InsertError {
pub(crate) fn conflict<T>(route: &[u8], prefix: &[u8], current: &Node<T>) -> Self {
// The new route would have had to replace the current node in the tree.
if prefix == current.prefix {
let mut route = route.to_owned();
denormalize_params(&mut route, &current.param_remapping);
return InsertError::Conflict {

Check warning on line 50 in src/error.rs

View workflow job for this annotation

GitHub Actions / Clippy Lints

unnecessary structure name repetition

Check warning on line 50 in src/error.rs

View workflow job for this annotation

GitHub Actions / Clippy Lints

unnecessary structure name repetition
with: String::from_utf8(route).unwrap(),
};
}

let mut route = route[..route.len() - prefix.len()].to_owned();

if !route.ends_with(&current.prefix) {
route.extend_from_slice(&current.prefix);
}

let mut last = current;
while let Some(node) = last.children.first() {
last = node;
}

let mut current = current.children.first();
while let Some(node) = current {
route.extend_from_slice(&node.prefix);
current = node.children.first();
}

denormalize_params(&mut route, &last.param_remapping);

InsertError::Conflict {

Check warning on line 74 in src/error.rs

View workflow job for this annotation

GitHub Actions / Clippy Lints

unnecessary structure name repetition

Check warning on line 74 in src/error.rs

View workflow job for this annotation

GitHub Actions / Clippy Lints

unnecessary structure name repetition
with: String::from_utf8(route).unwrap(),
}
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! # `matchit`
//!
//! [![Documentation](https://img.shields.io/badge/docs-0.7.1-4d76ae?style=for-the-badge)](https://docs.rs/matchit)
//! [![Documentation](https://img.shields.io/badge/docs-0.7.3-4d76ae?style=for-the-badge)](https://docs.rs/matchit)
//! [![Version](https://img.shields.io/crates/v/matchit?style=for-the-badge)](https://crates.io/crates/matchit)
//! [![License](https://img.shields.io/crates/l/matchit?style=for-the-badge)](https://crates.io/crates/matchit)
//!
Expand Down Expand Up @@ -45,7 +45,8 @@
//!
//! ### Catch-all Parameters
//!
//! Catch-all parameters start with `*` and match everything after the `/`. They must always be at the **end** of the route:
//! Catch-all parameters start with `*` and match anything until the end of the path.
//! They must always be at the **end** of the route:
//!
//! ```rust
//! # use matchit::Router;
Expand Down
18 changes: 18 additions & 0 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,24 @@ impl<'k, 'v> Params<'k, 'v> {
ParamsKind::Large(vec) => vec.push(param),
}
}

// Transform each key.
pub(crate) fn for_each_key_mut(&mut self, f: impl Fn((usize, &mut &'k [u8]))) {
match &mut self.kind {
ParamsKind::None => {}
ParamsKind::Small(arr, len) => arr
.iter_mut()
.take(*len)
.map(|param| &mut param.key)
.enumerate()
.for_each(f),
ParamsKind::Large(vec) => vec
.iter_mut()
.map(|param| &mut param.key)
.enumerate()
.for_each(f),
}
}
}

/// An iterator over the keys and values of a route's [parameters](crate::Params).
Expand Down
Loading

0 comments on commit eff698a

Please sign in to comment.