Module std.net.http.router

Method and path routing over ordinary Hew values.

Register a typed route value, then resolve request.method() and request.path(). The router owns no requests, connections or handlers. Patterns use whole-segment parameters, such as /users/{id}. Among complete path matches, the first differing segment prefers a literal to a parameter, independently of registration order. Method selection follows path selection.

Contents

Functions

Function new

pub fn new<T>() -> Router<T>

Create an empty table; the binding supplies its route value type.

Types

Enum RouteConfigError

A route that cannot be registered. Failed additions leave the table intact.

Variants

InvalidMethod(string)
InvalidPattern(string)
DuplicateRoute(string, string)

Enum RouteError

Request routing failure. A wrong method carries the available methods in registration order, suitable for an HTTP Allow header.

Variants

InvalidPath(string)
InvalidEncoding(url.DecodeError)
NotFound
MethodNotAllowed(Vec<string>)

Enum ExtractError

An absent named parameter or a value that cannot be read as the asked type.

Variants

Missing(string)
Number(string, string.NumberError)
Boolean(string, string)

Struct Router

A route table. T is commonly an application enum identifying a handler. Lookup copies the selected value using ordinary Hew value semantics.

Fields

routes: Vec<Route<T>>

Struct RouteMatch

The selected route value and its once-decoded path parameters.

Fields

value: T
parameters: Vec<(string, string)>

Inherent methods

impl<T: Clone> Router<T>

fn add(var self, method: string, pattern: string, value: T) -> () fails RouteConfigError

Register a method and pattern. Methods are case-sensitive HTTP tokens. Parameter names are unique ASCII identifiers; parameters match one non-empty segment. Renaming parameters does not make a duplicate route distinct. Literal segments use the same percent encoding as requests.

fn resolve(self, method: string, target: string) -> RouteMatch<T> fails RouteError

Resolve an origin-form target such as /users/42?view=full. The query is excluded; fragments and control characters are rejected. Split on raw /, then decode each component exactly once. Encoded / and \ are content within one segment, never new separators. No case, dot-segment, repeated-slash or trailing-slash normalization is performed. HEAD and OPTIONS require explicit registrations like other methods.

impl<T> RouteMatch<T>

fn param(self, name: string) -> string fails ExtractError

Read one already-decoded parameter. Pass it directly to an application parser for other types; decoding it again would change its meaning.

fn param_i64(self, name: string) -> i64 fails ExtractError

Read a signed decimal integer, rejecting invalid text and overflow.

fn param_bool(self, name: string) -> bool fails ExtractError

Read exactly true or false, case-sensitively.