Skip to content

Commit

Permalink
Add method router.
Browse files Browse the repository at this point in the history
  • Loading branch information
CathalMullan committed Dec 11, 2024
1 parent 650395c commit 7636a2e
Show file tree
Hide file tree
Showing 62 changed files with 62,563 additions and 9,176 deletions.
3 changes: 3 additions & 0 deletions ARCHITECTURE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Architecture

TODO.
53 changes: 53 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# https://doc.rust-lang.org/cargo/reference/workspaces.html
[workspace]
resolver = "2"
members = [".", "examples/*", "fuzz"]
members = [".", "crates/*", "examples/*", "fuzz"]

[workspace.package]
version = "0.7.0"
Expand Down Expand Up @@ -60,10 +60,12 @@ name = "wayfind"
description = "A speedy, flexible router."
include = [
"/benches",
"/crates",
"/examples",
"/fuzz",
"/src",
"/tests",
"/ARCHITECTURE.md",
"/CHANGELOG.md",
"/LICENSE-APACHE",
"/LICENSE-MIT",
Expand All @@ -87,6 +89,8 @@ workspace = true
smallvec = { version = "1.13", features = ["const_generics", "union"] }

[dev-dependencies]
wayfind-rails-macro = { path = "crates/rails-macro" }

# Testing
# NOTE: Keep in sync with `cargo-insta` Nix package.
insta = "=1.41.1"
Expand All @@ -95,6 +99,13 @@ similar-asserts = "1.6"
# Encoding
percent-encoding = "2.3"

# Serde
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

# Regex
fancy-regex = "0.14"

# Benchmarking
divan = "0.1"
criterion = { version = "0.5", features = ["html_reports"] }
Expand Down
111 changes: 55 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,20 +324,6 @@ Where possible, we try to provide user-friendly error messages.
use std::error::Error;
use wayfind::{PathConstraint, Router};

const ERROR_DISPLAY: &str = "
duplicate constraint name
The constraint name 'my_constraint' is already in use:
- existing constraint type: 'rust_out::ConstraintA'
- new constraint type: 'rust_out::ConstraintB'
help: each constraint must have a unique name
try:
- Check if you have accidentally added the same constraint twice
- Ensure different constraints have different names
";

struct ConstraintA;
impl PathConstraint for ConstraintA {
const NAME: &'static str = "my_constraint";
Expand All @@ -361,7 +347,19 @@ fn main() -> Result<(), Box<dyn Error>> {
router.path.constraint::<ConstraintA>()?;

let error = router.path.constraint::<ConstraintB>().unwrap_err();
assert_eq!(error.to_string(), ERROR_DISPLAY.trim());
insta::assert_snapshot!(error, @r"
duplicate constraint name
The constraint name 'my_constraint' is already in use:
- existing constraint type: 'rust_out::ConstraintA'
- new constraint type: 'rust_out::ConstraintB'
help: each constraint must have a unique name
try:
- Check if you have accidentally added the same constraint twice
- Ensure different constraints have different names
");

Ok(())
}
Expand All @@ -381,30 +379,6 @@ Currenty, this doesn't handle split multi-byte characters well.
use std::error::Error;
use wayfind::{Router, RouteBuilder};

const ROUTER_DISPLAY: &str = "
/
├─ user [*]
│ ╰─ /
│ ├─ createWithList [*]
│ ├─ log
│ │ ├─ out [*]
│ │ ╰─ in [*]
│ ╰─ {username} [*]
├─ pet [*]
│ ╰─ /
│ ├─ findBy
│ │ ├─ Status [*]
│ │ ╰─ Tags [*]
│ ╰─ {petId} [*]
│ ╰─ /uploadImage [*]
├─ store/
│ ├─ inventory [*]
│ ╰─ order [*]
│ ╰─ /
│ ╰─ {orderId} [*]
╰─ {*catch_all} [*]
";

fn main() -> Result<(), Box<dyn Error>> {
let mut router = Router::new();

Expand Down Expand Up @@ -478,7 +452,32 @@ fn main() -> Result<(), Box<dyn Error>> {
.build()?;
router.insert(&route, 14)?;

assert_eq!(router.path.to_string(), ROUTER_DISPLAY.trim());
insta::assert_snapshot!(router, @r"
=== Path
/
├─ user [9]
│ ╰─ /
│ ├─ createWithList [10]
│ ├─ log
│ │ ├─ out [12]
│ │ ╰─ in [11]
│ ╰─ {username} [13]
├─ pet [1]
│ ╰─ /
│ ├─ findBy
│ │ ├─ Status [2]
│ │ ╰─ Tags [3]
│ ╰─ {petId} [4]
│ ╰─ /uploadImage [5]
├─ store/
│ ├─ inventory [6]
│ ╰─ order [7]
│ ╰─ /
│ ╰─ {orderId} [8]
╰─ {*catch_all} [14]
=== Method
");

Ok(())
}
```
Expand Down Expand Up @@ -511,29 +510,29 @@ In a router of 130 routes, benchmark matching 4 paths.

| Library | Time | Alloc Count | Alloc Size | Dealloc Count | Dealloc Size |
|:-----------------|----------:|------------:|-----------:|--------------:|-------------:|
| wayfind | 398.23 ns | 5 | 329 B | 5 | 329 B |
| path-tree | 579.66 ns | 5 | 480 B | 5 | 512 B |
| matchit | 582.72 ns | 5 | 480 B | 5 | 512 B |
| xitca-router | 660.95 ns | 8 | 864 B | 8 | 896 B |
| ntex-router | 2.2411 µs | 19 | 1.312 KB | 19 | 1.344 KB |
| route-recognizer | 3.2239 µs | 161 | 8.569 KB | 161 | 8.601 KB |
| routefinder | 6.2734 µs | 68 | 5.088 KB | 68 | 5.12 KB |
| actix-router | 21.459 µs | 215 | 14 KB | 215 | 14.03 KB |
| wayfind | 341.71 ns | 4 | 265 B | 4 | 265 B |
| matchit | 376.67 ns | 4 | 416 B | 4 | 448 B |
| xitca-router | 415.14 ns | 7 | 800 B | 7 | 832 B |
| path-tree | 442.06 ns | 4 | 416 B | 4 | 448 B |
| ntex-router | 1.7614 µs | 18 | 1.248 KB | 18 | 1.28 KB |
| route-recognizer | 2.0531 µs | 160 | 8.505 KB | 160 | 8.537 KB |
| routefinder | 4.7332 µs | 67 | 5.024 KB | 67 | 5.056 KB |
| actix-router | 17.897 µs | 214 | 13.93 KB | 214 | 13.96 KB |

#### `path-tree` inspired benches

In a router of 320 routes, benchmark matching 80 paths.

| Library | Time | Alloc Count | Alloc Size | Dealloc Count | Dealloc Size |
|:-----------------|----------:|------------:|-----------:|--------------:|-------------:|
| wayfind | 5.3596 µs | 60 | 3.847 KB | 60 | 3.847 KB |
| path-tree | 8.6949 µs | 60 | 8.727 KB | 60 | 8.75 KB |
| matchit | 10.130 µs | 141 | 19.09 KB | 141 | 19.11 KB |
| xitca-router | 11.984 µs | 210 | 26.79 KB | 210 | 26.81 KB |
| ntex-router | 36.829 µs | 202 | 20.82 KB | 202 | 20.84 KB |
| route-recognizer | 70.734 µs | 2873 | 192.9 KB | 2873 | 206.1 KB |
| routefinder | 87.920 µs | 526 | 49.68 KB | 526 | 49.71 KB |
| actix-router | 189.66 µs | 2202 | 130.1 KB | 2202 | 130.1 KB |
| wayfind | 5.1040 µs | 59 | 2.567 KB | 59 | 2.567 KB |
| matchit | 6.4678 µs | 140 | 17.81 KB | 140 | 17.83 KB |
| path-tree | 7.0941 µs | 59 | 7.447 KB | 59 | 7.47 KB |
| xitca-router | 7.5814 µs | 209 | 25.51 KB | 209 | 25.53 KB |
| ntex-router | 31.100 µs | 201 | 19.54 KB | 201 | 19.56 KB |
| route-recognizer | 54.829 µs | 2872 | 191.7 KB | 2872 | 204.8 KB |
| routefinder | 75.961 µs | 525 | 48.4 KB | 525 | 48.43 KB |
| actix-router | 152.62 µs | 2201 | 128.8 KB | 2201 | 128.8 KB |

## License

Expand Down
Loading

0 comments on commit 7636a2e

Please sign in to comment.