wayfind 0.6.0

A speedy, flexible router.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
[![crates.io](https://img.shields.io/crates/v/wayfind)](https://crates.io/crates/wayfind)
[![documentation](https://docs.rs/wayfind/badge.svg)](https://docs.rs/wayfind)
[![rust: 1.66+](https://img.shields.io/badge/rust-1.66+-orange.svg)](https://img.shields.io/badge/rust-1.66-orange.svg)
![unsafe: forbidden](https://img.shields.io/badge/unsafe-forbidden-brightgreen.svg)
![license: MIT/Apache-2.0](https://img.shields.io/badge/license-MIT%2FApache--2.0-blue.svg)

[![codspeed](https://img.shields.io/endpoint?url=https://codspeed.io/badge.json)](https://codspeed.io/DuskSystems/wayfind)
[![codecov](https://codecov.io/gh/DuskSystems/wayfind/graph/badge.svg?token=QMSW55438K)](https://codecov.io/gh/DuskSystems/wayfind)

# `wayfind`

A speedy, flexible router for Rust.

> [!NOTE]
> `wayfind` is still a work in progress.

## Why another router?

`wayfind` attempts to bridge the gap between existing Rust router options:

- fast routers, lacking in flexibility
- flexible routers, lacking in speed

Real-world projects often need fancy routing capabilities, such as projects ported from frameworks like [Ruby on Rails](https://guides.rubyonrails.org/routing.html), or those adhering to specifications like the [Open Container Initiative (OCI) Distribution Specification](https://github.com/opencontainers/distribution-spec/blob/main/spec.md).

The goal of `wayfind` is to remain competitive with the fastest libraries, while offering advanced routing features when needed. Unused features shouldn't impact performance - you only pay for what you use.

## Features

### Dynamic Routing

Dynamic parameters can match any byte, **excluding** the path delimiter `/`.

We support both:
- whole segment parameters: `/{name}/`
- inline parameters: `/{year}-{month}-{day}/`

Dynamic parameters are greedy in nature, similar to a regex `.*`, and will attempt to match as many bytes as possible.

#### Example

```rust
use std::error::Error;
use wayfind::{Path, Router};

fn main() -> Result<(), Box<dyn Error>> {
    let mut router = Router::new();
    router.insert("/users/{id}", 1)?;
    router.insert("/users/{id}/files/{filename}.{extension}", 2)?;

    let path = Path::new("/users/123")?;
    let search = router.search(&path)?.unwrap();
    assert_eq!(*search.data, 1);
    assert_eq!(search.route, "/users/{id}".into());
    assert_eq!(search.parameters[0].key, "id");
    assert_eq!(search.parameters[0].value, "123");

    let path = Path::new("/users/123/files/my.document.pdf")?;
    let search = router.search(&path)?.unwrap();
    assert_eq!(*search.data, 2);
    assert_eq!(search.route, "/users/{id}/files/{filename}.{extension}".into());
    assert_eq!(search.parameters[0].key, "id");
    assert_eq!(search.parameters[0].value, "123");
    assert_eq!(search.parameters[1].key, "filename");
    assert_eq!(search.parameters[1].value, "my.document");
    assert_eq!(search.parameters[2].key, "extension");
    assert_eq!(search.parameters[2].value, "pdf");

    Ok(())
}
```

### Wildcard Routing

Wildcard parameters can match any byte, **including** the path delimiter `/`.

We support both:
- inline wildcards: `/{*path}.html`
- mid-route wildcards: `/api/{*path}/help`
- end-route catch-all: `/{*catch_all}`

Like dynamic parameters, wildcard parameters are also greedy in nature.

#### Example

```rust
use std::error::Error;
use wayfind::{Path, Router};

fn main() -> Result<(), Box<dyn Error>> {
    let mut router = Router::new();
    router.insert("/files/{*slug}/delete", 1)?;
    router.insert("/{*catch_all}", 2)?;

    let path = Path::new("/files/documents/reports/annual.pdf/delete")?;
    let search = router.search(&path)?.unwrap();
    assert_eq!(*search.data, 1);
    assert_eq!(search.route, "/files/{*slug}/delete".into());
    assert_eq!(search.parameters[0].key, "slug");
    assert_eq!(search.parameters[0].value, "documents/reports/annual.pdf");

    let path = Path::new("/any/other/path")?;
    let search = router.search(&path)?.unwrap();
    assert_eq!(*search.data, 2);
    assert_eq!(search.route, "/{*catch_all}".into());
    assert_eq!(search.parameters[0].key, "catch_all");
    assert_eq!(search.parameters[0].value, "any/other/path");

    Ok(())
}
```

### Optional Groups

Optional groups allow for parts of a route to be absent.

They are commonly used for:
- optional IDs: `/users(/{id})`
- optional trailing slashes: `/users(/)`
- optional file extensions: `/images/{name}(.{extension})`

They work via 'expanding' the route into equivilant, simplified routes.

`/release/v{major}(.{minor}(.{patch}))`
- `/release/v{major}.{minor}.{patch}`
- `/release/v{major}.{minor}`
- `/release/v{major}`

There is a small overhead to using optional groups, due to `Arc` usage internally for data storage.

#### Example

```rust
use std::error::Error;
use wayfind::{Path, Router};

fn main() -> Result<(), Box<dyn Error>> {
    let mut router = Router::new();
    router.insert("/users(/{id})", 1)?;
    router.insert("/files/{*slug}/{file}(.{extension})", 2)?;

    let path = Path::new("/users")?;
    let search = router.search(&path)?.unwrap();
    assert_eq!(*search.data, 1);
    assert_eq!(search.route, "/users(/{id})".into());
    assert_eq!(search.expanded, Some("/users".into()));

    let path = Path::new("/users/123")?;
    let search = router.search(&path)?.unwrap();
    assert_eq!(*search.data, 1);
    assert_eq!(search.route, "/users(/{id})".into());
    assert_eq!(search.expanded, Some("/users/{id}".into()));
    assert_eq!(search.parameters[0].key, "id");
    assert_eq!(search.parameters[0].value, "123");

    let path = Path::new("/files/documents/folder/report.pdf")?;
    let search = router.search(&path)?.unwrap();
    assert_eq!(*search.data, 2);
    assert_eq!(search.route, "/files/{*slug}/{file}(.{extension})".into());
    assert_eq!(search.expanded, Some("/files/{*slug}/{file}.{extension}".into()));
    assert_eq!(search.parameters[0].key, "slug");
    assert_eq!(search.parameters[0].value, "documents/folder");
    assert_eq!(search.parameters[1].key, "file");
    assert_eq!(search.parameters[1].value, "report");
    assert_eq!(search.parameters[2].key, "extension");
    assert_eq!(search.parameters[2].value, "pdf");

    let path = Path::new("/files/documents/folder/readme")?;
    let search = router.search(&path)?.unwrap();
    assert_eq!(*search.data, 2);
    assert_eq!(search.route, "/files/{*slug}/{file}(.{extension})".into());
    assert_eq!(search.expanded, Some("/files/{*slug}/{file}".into()));
    assert_eq!(search.parameters[0].key, "slug");
    assert_eq!(search.parameters[0].value, "documents/folder");
    assert_eq!(search.parameters[1].key, "file");
    assert_eq!(search.parameters[1].value, "readme");

    Ok(())
}
```

### Constraints

Constraints allow for custom logic to be injected into the routing process.

We support constraints for all types of parameters:
- Dynamic constraint: `/{name:constraint}`
- Wildcard constraint: `/{*name:constraint}`

The typical use-case for constraints would be to run a regex, or a simple `FromStr` implementation, against a path segment.

A common mistake would be to use these for validation of parameters. This should be avoided.

If a constraint fails to match, and no other suitable match exists, it results in a `Not Found` response, rather than any sort of `Bad Request`.

They act as an escape-hatch for when you need to disambiguate routes.

The current constraint implementation has a number of limitations:
- constraints cannot take parameters
- checks cannot make use of any prior state
- checks cannot store data after a successful check

#### Default Constraints

`wayfind` ships with a number of default constraints.

Curently, these can't be disabled.

| Name    | Method               |
|:--------|:---------------------|
| `u8`    | `u8::from_str`       |
| `u16`   | `u16::from_str`      |
| `u32`   | `u32::from_str`      |
| `u64`   | `u64::from_str`      |
| `u128`  | `u128::from_str`     |
| `usize` | `usize::from_str`    |
| `i8`    | `i8::from_str`       |
| `i16`   | `i16::from_str`      |
| `i32`   | `i32::from_str`      |
| `i64`   | `i64::from_str`      |
| `i128`  | `i128::from_str`     |
| `isize` | `isize::from_str`    |
| `f32`   | `f32::from_str`      |
| `f64`   | `f64::from_str`      |
| `bool`  | `bool::from_str`     |
| `ipv4`  | `Ipv4Addr::from_str` |
| `ipv6`  | `Ipv6Addr::from_str` |

#### Example

```rust
use std::error::Error;
use wayfind::{Constraint, Path, Router};

struct NamespaceConstraint;
impl Constraint for NamespaceConstraint {
    const NAME: &'static str = "namespace";

    fn check(segment: &str) -> bool {
        segment
            .split('/')
            .all(|part| {
                !part.is_empty() && part.chars().all(|c| c.is_ascii_alphanumeric() || c == '.' || c == '_' || c == '-')
            })
    }
}

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

    router.insert("/v2", 1)?;
    router.insert("/v2/{*name:namespace}/blobs/{type}:{digest}", 2)?;

    let path = Path::new("/v2")?;
    let search = router.search(&path)?.unwrap();
    assert_eq!(*search.data, 1);
    assert_eq!(search.route, "/v2".into());

    let path = Path::new("/v2/my-org/my-repo/blobs/sha256:1234567890")?;
    let search = router.search(&path)?.unwrap();
    assert_eq!(*search.data, 2);
    assert_eq!(search.route, "/v2/{*name:namespace}/blobs/{type}:{digest}".into());
    assert_eq!(search.parameters[0].key, "name");
    assert_eq!(search.parameters[0].value, "my-org/my-repo");
    assert_eq!(search.parameters[1].key, "type");
    assert_eq!(search.parameters[1].value, "sha256");
    assert_eq!(search.parameters[2].key, "digest");
    assert_eq!(search.parameters[2].value, "1234567890");

    let path = Path::new("/v2/invalid repo/blobs/uploads")?;
    assert!(router.search(&path)?.is_none());

    Ok(())
}
```

### User-Friendly Error Messages

Where possible, we try to provide user-friendly error messages.

#### Example

```rust
use std::error::Error;
use wayfind::{Constraint, Router, errors::ConstraintError};

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 Constraint for ConstraintA {
    const NAME: &'static str = "my_constraint";

    fn check(segment: &str) -> bool {
        segment == "a"
    }
}

struct ConstraintB;
impl Constraint for ConstraintB {
    const NAME: &'static str = "my_constraint";

    fn check(segment: &str) -> bool {
        segment == "b"
    }
}

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

    let error = router.constraint::<ConstraintB>().unwrap_err();
    assert_eq!(error.to_string(), ERROR_DISPLAY.trim());

    Ok(())
}
```

### Router Display

Routers can print their routes as an tree diagram.

- `` represents the root node.
- `` represents nodes within the tree that can be matched against.

Currenty, this doesn't handle split multi-byte characters well.

#### Example

```rust
use std::error::Error;
use wayfind::Router;

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();

    router.insert("/pet", 1)?;
    router.insert("/pet/findByStatus", 2)?;
    router.insert("/pet/findByTags", 3)?;
    router.insert("/pet/{petId}", 4)?;
    router.insert("/pet/{petId}/uploadImage", 5)?;

    router.insert("/store/inventory", 6)?;
    router.insert("/store/order", 7)?;
    router.insert("/store/order/{orderId}", 8)?;

    router.insert("/user", 9)?;
    router.insert("/user/createWithList", 10)?;
    router.insert("/user/login", 11)?;
    router.insert("/user/logout", 12)?;
    router.insert("/user/{username}", 13)?;

    router.insert("{*catch_all}", 14)?;

    assert_eq!(router.to_string(), ROUTER_DISPLAY.trim());
    Ok(())
}
```

## Performance

`wayfind` is fast, and appears to be competitive against other top performers in all benchmarks we currently run.

However, as is often the case, your mileage may vary (YMMV).
Benchmarks, especially micro-benchmarks, should be taken with a grain of salt.

### Benchmarks

All benchmarks ran on a M1 Pro laptop.

Check out our [codspeed results](https://codspeed.io/DuskSystems/wayfind/benchmarks) for a more accurate set of timings.

#### Context

For all benchmarks, we percent-decode the path before matching.
After matching, we convert any extracted parameters to strings.

Some routers perform these operations automatically, while others require them to be done manually.

We do this to try and match behaviour as best as possible. This is as close to an "apples-to-apples" comparison as we can get.

#### `matchit` inspired benches

In a router of 130 routes, benchmark matching 4 paths.

| Library          | Time      | Alloc Count | Alloc Size | Dealloc Count | Dealloc Size |
|:-----------------|----------:|------------:|-----------:|--------------:|-------------:|
| matchit          | 499.02 ns | 4           | 416 B      | 4             | 448 B        |
| wayfind          | 508.99 ns | 7           | 649 B      | 7             | 649 B        |
| path-tree        | 577.41 ns | 4           | 416 B      | 4             | 448 B        |
| xitca-router     | 580.88 ns | 7           | 800 B      | 7             | 832 B        |
| ntex-router      | 1.7491 µs | 18          | 1.248 KB   | 18            | 1.28 KB      |
| route-recognizer | 4.6071 µs | 160         | 8.505 KB   | 160           | 8.537 KB     |
| routefinder      | 6.3982 µs | 67          | 5.024 KB   | 67            | 5.056 KB     |
| actix-router     | 20.950 µ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          | 7.7141 µs | 117         | 9.991 KB   | 117           | 9.991 KB     |
| matchit          | 9.0602 µs | 140         | 17.81 KB   | 140           | 17.83 KB     |
| path-tree        | 9.2373 µs | 59          | 7.447 KB   | 59            | 7.47 KB      |
| xitca-router     | 10.722 µs | 209         | 25.51 KB   | 209           | 25.53 KB     |
| ntex-router      | 30.095 µs | 201         | 19.54 KB   | 201           | 19.56 KB     |
| route-recognizer | 92.357 µs | 2872        | 191.7 KB   | 2872          | 204.8 KB     |
| routefinder      | 103.36 µs | 525         | 48.4 KB    | 525           | 48.43 KB     |
| actix-router     | 178.13 µs | 2201        | 128.8 KB   | 2201          | 128.8 KB     |

## License

`wayfind` is licensed under the terms of both the [MIT License](LICENSE-MIT) and the [Apache License (Version 2.0)](LICENSE-APACHE).

## Inspirations

- [poem]https://github.com/poem-web/poem: Initial experimentations started out as a Poem router fork
- [matchit]https://github.com/ibraheemdev/matchit: Performance leader among pre-existing routers
- [path-tree]https://github.com/viz-rs/path-tree: Extensive testing and router display feature
- [ASP.NET Core]https://github.com/dotnet/AspNetCore: Constraints-based approach to routing