Skip to content

Commit

Permalink
update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ibraheemdev committed Mar 10, 2024
1 parent ab40e66 commit 17d9a34
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

## Parameters

Along with static routes, the router also supports dynamic route segments. These can either be named or catch-all parameters:
Along with static routes, the router also supports dynamic route segments. These can either be named or catch-all parameters.

### Named Parameters

Named parameters like `/{id}` match anything until the next `/` or the end of the path:
Named parameters like `/{id}` match anything until the next `/` or the end of the path.

```rust,ignore
let mut m = Router::new();
Expand All @@ -39,9 +39,11 @@ assert_eq!(m.at("/users/23")?.params.get("id"), Some("23"));
assert!(m.at("/users").is_err());
```

Note that named parameters must be followed by a `/` or the end of the route. Dynamic suffixes are not currently supported.

### Catch-all Parameters

Catch-all parameters start with `*` and match anything until the end of the path. 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 All @@ -54,6 +56,22 @@ assert_eq!(m.at("/c/bar.css")?.params.get("p"), Some("c/bar.css"));
assert_eq!(m.at("/").is_err());
```

### Escaping Parameters

The literal characters `{` and `}` may be included in a static route by escaping them with the same character. For example, the `{` character is escaped with `{{` and the `}` character is escaped with `}}`.

```rust,ignore
let mut m = Router::new();
m.insert("/{{hello}}", true)?;
m.insert("/{hello}", true)?;
// match the static route
assert!(m.at("/{hello}")?.value);
// match the dynamic route
assert_eq!(m.at("/hello")?.params.get("hello"), Some("hello"));
```

## Routing Priority

Static and dynamic route segments are allowed to overlap. If they do, static segments will be given higher priority:
Expand All @@ -67,7 +85,7 @@ m.insert("/{*filepath}", "...").unwrap(); // priority: 2

## How does it work?

The router takes advantage of the fact that URL routes generally follow a hierarchical structure. Routes are stored them in a radix trie that makes heavy use of common prefixes:
The router takes advantage of the fact that URL routes generally follow a hierarchical structure. Routes are stored them in a radix trie that makes heavy use of common prefixes.

```text
Priority Path Value
Expand Down
10 changes: 6 additions & 4 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ pub enum InsertError {
},
/// Only one parameter per route segment is allowed.
///
/// Prefixes are allowed before a parmeter, but not after it. For example,
/// Static segments are also allowed before a parameter, but not after it. For example,
/// `/foo-{bar}` is a valid route, but `/{bar}-foo` is not.
InvalidParamSegment,
/// Parameters must be registered with a valid name, and matching braces.
/// Parameters must be registered with a valid name and matching braces.
///
/// Note you can use `{{` or `}}` to escape literal brackets.
InvalidParam,
/// Catch-all parameters are only allowed at the end of a path.
InvalidCatchAll,
Expand Down Expand Up @@ -95,10 +97,10 @@ impl InsertError {
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut router = Router::new();
/// router.insert("/home", "Welcome!")?;
/// router.insert("/blog/", "Our blog.")?;
/// router.insert("/blog", "Our blog.")?;
///
/// // no routes match
/// if let Err(err) = router.at("/foobar") {
/// if let Err(err) = router.at("/blo") {
/// assert_eq!(err, MatchError::NotFound);
/// }
/// # Ok(())
Expand Down
29 changes: 26 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@
//!
//! ## Parameters
//!
//! Along with static routes, the router also supports dynamic route segments. These can either be named or catch-all parameters:
//! Along with static routes, the router also supports dynamic route segments. These can either be named or catch-all parameters.
//!
//! ### Named Parameters
//!
//! Named parameters like `/{id}` match anything until the next `/` or the end of the path:
//! Named parameters like `/{id}` match anything until the next `/` or the end of the path.
//!
//! ```rust
//! # use matchit::Router;
Expand All @@ -43,10 +43,12 @@
//! # }
//! ```
//!
//! Note that named parameters must be followed by a `/` or the end of the route. Dynamic suffixes are not currently supported.
//!
//! ### Catch-all Parameters
//!
//! Catch-all parameters start with `*` and match anything until the end of the path.
//! They must always be at the **end** of the route:
//! They must always be at the **end** of the route.
//!
//! ```rust
//! # use matchit::Router;
Expand All @@ -64,6 +66,27 @@
//! # }
//! ```
//!
//! ### Escaping Parameters
//!
//! The literal characters `{` and `}` may be included in a static route by escaping them with the same character.
//! For example, the `{` character is escaped with `{{` and the `}` character is escaped with `}}`.
//!
//! ```rust
//! # use matchit::Router;
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let mut m = Router::new();
//! m.insert("/{{hello}}", true)?;
//! m.insert("/{hello}", true)?;
//!
//! // match the static route
//! assert!(m.at("/{hello}")?.value);
//!
//! // match the dynamic route
//! assert_eq!(m.at("/hello")?.params.get("hello"), Some("hello"));
//! # Ok(())
//! # }
//! ```
//!
//! ## Routing Priority
//!
//! Static and dynamic route segments are allowed to overlap. If they do, static segments will be given higher priority:
Expand Down

0 comments on commit 17d9a34

Please sign in to comment.