Struct rustful::TreeRouter [] [src]

pub struct TreeRouter<T> {
    pub find_hyperlinks: bool,
    // some fields omitted
}

Stores handlers, using an HTTP method and a route as key.

Variables

Routes may contain variables, that are useful for capturing parts of the requested path as input to the handler. The syntax for a variable is simply an indicator character (: or *) followed by a label. Variables without labels are also valid, but their values will be discarded.

Variable Segments (:label)

A variable segment will match a single arbitrary segment. They are probably the most commonly used variables and may, for example, be used to select a blog post: "posts/:year/:month/:day/:title_slug".

pattern = "a/:v/b"
"a/c/b" -> v = "c"
"a/c/d/b" -> no match
"a/b" -> no match
"a/c/b/d" -> no match

Variable Sequences (*label)

A variable sequence is similar to a variable segment, but with the difference that it may consume multiple segments until the rest of the path gives a match. An example use case is a route for downloadable files that may be arranged in arbitrary directories: "downloads/*file_path".

pattern = "a/*v/b"
"a/c/b" -> v = "c"
"a/c/d/b" -> v = "c/d"
"a/b" -> no match
"a/c/b/d" -> no match
pattern = "a/b/*v"
"a/b/c" -> v = "c"
"a/b/c/d" -> v = "c/d"
"a/b" -> no match

Hyperlinks

The 'TreeRouter` has support for shallow hyperlinks to children, siblings, cousins, ans so forth. The use of variable sequences complicates this process and may cause confusing results in certain situations. The hyperlinks may or may not point to a handler.

Hyperlinks has to be activated by setting find_hyperlinks to true.

Fields

find_hyperlinks: bool

Should the router search for hyperlinks? Setting this to true may slow down endpoint search, but enables hyperlinks.

Methods

impl<T> TreeRouter<T>
[src]

fn new() -> TreeRouter<T>

Creates an empty TreeRouter.

fn insert_router<'r, R: Route<'r> + ?Sized>(&mut self, route: &'r R, router: TreeRouter<T>)

Insert an other TreeRouter at a path. The content of the other TreeRouter will be merged with this one and content with the same path and method will be overwritten.

Trait Implementations

impl<T: Clone> Clone for TreeRouter<T>
[src]

fn clone(&self) -> TreeRouter<T>

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)
1.0.0

Performs copy-assignment from source. Read more

impl<T: Handler> Router for TreeRouter<T>
[src]

type Handler = T

The request handler type that is stored within this router.

fn find<'a>(&'a self, method: &Method, route: &[u8]) -> Endpoint<'a, T>

Find and return the matching handler and variable values.

fn insert<'a, D: ?Sized + Deref<Target=R> + 'a, R: ?Sized + Route<'a> + 'a>(&mut self, method: Method, route: &'a D, item: T)

Insert a new handler into the router.

impl<T: Handler, D: Deref<Target=R>, R: ?Sized + for<'a> Route<'a>> FromIterator<(Method, D, T)> for TreeRouter<T>
[src]

fn from_iter<I: IntoIterator<Item=(Method, D, T)>>(iterator: I) -> TreeRouter<T>

Create a TreeRouter from a collection of routes.

extern crate rustful;
use rustful::Method::Get;
use rustful::TreeRouter;

let routes = vec![
    (Get, "/about", about_us),
    (Get, "/users", list_users),
    (Get, "/users/:id", show_user),
    (Get, "/products", list_products),
    (Get, "/products/:id", show_product),
    (Get, "/*", show_error),
    (Get, "/", show_welcome)
];

let router: TreeRouter<_> = routes.into_iter().collect();

impl<T> Default for TreeRouter<T>
[src]

fn default() -> TreeRouter<T>

Returns the "default value" for a type. Read more