Wado

core:router

HTTP path router (core:router).

A Router<H> maps a request's method and path to a handler of any type H, and captures the parameters its pattern names.

Patterns

A pattern is a path of /-separated segments. Each segment is one of:

/ alone is the root. Registering a pattern panics when it does not start with /, has an empty segment (/a//b, or a trailing /), has nothing after a : or *, or uses one name twice. It also panics when a parameter or wildcard takes a different name from one registered before at the same place: /users/:id and then /users/:name/posts.

Matching

A path is matched one segment at a time. Where a literal and a parameter could both take a segment, the literal takes it. A path that then finds no route does not go back to try the parameter: with /users/list and /users/:id registered, /users/list/extra matches neither.

A path that finds no route, or finds one without a handler for the request's method, falls back to a wildcard. The deepest wildcard passed on the way that takes the method matches, so /*rest catches whatever no other route takes.

A trailing slash matters: /users and /users/ are different paths. A wildcard matches the empty rest after one (/static/), but not the bare prefix (/static). A path that does not start with / matches nothing. Parameters hold the path's raw text: %20 is not decoded.

A route registered for a method takes that method alone, so HEAD does not fall back to GET. A route registered with any takes every method, but a handler registered for the method itself at the same pattern wins.

When match_path returns None, allowed_methods tells a 405 from a 404: it lists the methods the path would match under, and is empty for a 404.

See docs/wep-2026-05-06-core-router.md for the design.

Synopsis

let mut routes = Router::<i32>::new();
routes.get("/health", 1);
routes.get("/users/:id", 2);
assert routes.match_path(Method::Get, &"/health") matches { Some(hit) && hit.handler == 1 };
assert routes.match_path(Method::Get, &"/users/42") matches { Some(hit) && hit.handler == 2
    && hit.params.get("id") matches { Some(v) && v == "42" } };

Structs

pub struct PathParams

The parameters a matched route captured, by name. Each value is the raw text the parameter or wildcard took from the path.

Fields are private.

pub fn len(&self) -> i32

The number of captured parameters: 0 for a route with none.

pub fn is_empty(&self) -> bool

True when the route captured no parameters.

pub fn get<S: AsStrSlice>(&self, name: S) -> Option<String>

The value captured as name, or None when the route has no such parameter.

pub fn deserialize<T: Deserialize>(&self) -> Result<T, DeserializeError>

Deserializes the parameters into the struct T, each into the field of its name (#[wire] renames apply). A field parses from the parameter's text, so it may be a number, bool, char, String, any type that deserializes from a string, or an Option of one. A parameter T does not name is skipped.

Fails with InvalidValue on text the field's type cannot parse, and with UnexpectedType when T is not a struct or has a field of a list, map, struct, or other variant type.

impl IndexValue<String> for PathParams

fn index_value(&self, name: String) -> String

pub struct RouteMatch<H>

A route that matched: its handler, and the parameters it captured.

handler: H

params: PathParams

pub struct Router<H>

An HTTP path router over handlers of type H.

Fields are private.

pub fn new() -> Router<H>

An empty router.

pub fn route<S: AsStrSlice>(&mut self, method: Method, pattern: S, handler: H)

Registers handler for method at pattern, replacing the handler registered for both before. Panics on a malformed pattern.

pub fn get<S: AsStrSlice>(&mut self, pattern: S, handler: H)

route(Method::Get, pattern, handler).

pub fn post<S: AsStrSlice>(&mut self, pattern: S, handler: H)

route(Method::Post, pattern, handler).

pub fn put<S: AsStrSlice>(&mut self, pattern: S, handler: H)

route(Method::Put, pattern, handler).

pub fn patch<S: AsStrSlice>(&mut self, pattern: S, handler: H)

route(Method::Patch, pattern, handler).

pub fn delete<S: AsStrSlice>(&mut self, pattern: S, handler: H)

route(Method::Delete, pattern, handler).

pub fn options<S: AsStrSlice>(&mut self, pattern: S, handler: H)

route(Method::Options, pattern, handler).

pub fn any<S: AsStrSlice>(&mut self, pattern: S, handler: H)

Registers handler for every method at pattern, Method::Other(_) included, replacing the any handler registered there before. A handler registered for the request's method at the same pattern wins over it. Panics on a malformed pattern.

pub fn match_path(&self, method: Method, path: &String) -> Option<&RouteMatch<H>>

The route path matches under method, or None. path is the path alone, with no query or fragment; match_request strips them.

pub fn allowed_methods<S: AsStrSlice>(&self, path: S) -> List<Method>

The methods match_path would match path under, each once. A method only an any handler takes is not listed, so the list is empty on a 404 and where only any handlers match.

pub fn match_request(&self, request: &Request) -> Option<&RouteMatch<H>>

Matches a wasi:http request: its method, and its path with the query and fragment stripped. A request without a path matches as /.