[][src]Struct rocket::Route

pub struct Route {
    pub name: Option<&'static str>,
    pub method: Method,
    pub handler: Box<dyn Handler>,
    pub base: Origin<'static>,
    pub uri: Origin<'static>,
    pub rank: isize,
    pub format: Option<MediaType>,
    // some fields omitted
}

A route: a method, its handler, path, rank, and format/media type.

Fields

The name of this route, if one was given.

The method this route matches against.

The function that should be called when the route matches.

The base mount point of this Route.

The uri (in Rocket's route format) that should be matched against. This URI already includes the base mount point.

The rank of this route. Lower ranks have higher priorities.

The media type this route matches against, if any.

Methods

impl Route
[src]

Creates a new route with the given method, path, and handler with a base of /.

Ranking

The route's rank is set so that routes with static paths (no dynamic parameters) are ranked higher than routes with dynamic paths, routes with query strings with static segments are ranked higher than routes with fully dynamic queries, and routes with queries are ranked higher than routes without queries. This default ranking is summarized by the table below:

static path query rank
yes partly static -6
yes fully dynamic -5
yes none -4
no partly static -3
no fully dynamic -2
no none -1

Example

use rocket::Route;
use rocket::http::Method;

// this is rank -6 (static path, ~static query)
let route = Route::new(Method::Get, "/foo?bar=baz&<zoo>", handler);
assert_eq!(route.rank, -6);

// this is rank -5 (static path, fully dynamic query)
let route = Route::new(Method::Get, "/foo?<zoo..>", handler);
assert_eq!(route.rank, -5);

// this is a rank -4 route (static path, no query)
let route = Route::new(Method::Get, "/", handler);
assert_eq!(route.rank, -4);

// this is a rank -3 route (dynamic path, ~static query)
let route = Route::new(Method::Get, "/foo/<bar>?blue", handler);
assert_eq!(route.rank, -3);

// this is a rank -2 route (dynamic path, fully dynamic query)
let route = Route::new(Method::Get, "/<bar>?<blue>", handler);
assert_eq!(route.rank, -2);

// this is a rank -1 route (dynamic path, no query)
let route = Route::new(Method::Get, "/<bar>/foo/<baz..>", handler);
assert_eq!(route.rank, -1);

Panics

Panics if path is not a valid origin URI or Rocket route URI.

Creates a new route with the given rank, method, path, and handler with a base of /.

Example

use rocket::Route;
use rocket::http::Method;

// this is a rank 1 route matching requests to `GET /`
let index = Route::ranked(1, Method::Get, "/", handler);

Panics

Panics if path is not a valid origin URI or Rocket route URI.

Retrieves the path of the base mount point of this route as an &str.

Example

use rocket::Route;
use rocket::http::Method;

let mut index = Route::new(Method::Get, "/", handler);
assert_eq!(index.base(), "/");
assert_eq!(index.base.path(), "/");

Sets the base mount point of the route to base and sets the path to path. The path should not contains the base mount point. If base contains a query, it is ignored. Note that self.uri will include the new base after this method is called.

Errors

Returns an error if any of the following occur:

  • The base mount point contains dynamic parameters.
  • The base mount point or path contain encoded characters.
  • The path is not a valid Rocket route URI.

Example

use rocket::Route;
use rocket::http::{Method, uri::Origin};

let mut index = Route::new(Method::Get, "/", handler);
assert_eq!(index.base(), "/");
assert_eq!(index.base.path(), "/");

let new_base = Origin::parse("/greeting").unwrap();
let new_uri = Origin::parse("/hi").unwrap();
index.set_uri(new_base, new_uri);
assert_eq!(index.base(), "/greeting");
assert_eq!(index.uri.path(), "/greeting/hi");

Trait Implementations

impl<'a, 'r> FromRequest<'a, 'r> for &'r Route
[src]

The associated error to be returned if derivation fails.

impl Clone for Route
[src]

Performs copy-assignment from source. Read more

impl Debug for Route
[src]

impl Display for Route
[src]

Auto Trait Implementations

impl Send for Route

impl Sync for Route

Blanket Implementations

impl<T> From for T
[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

impl<T, U> TryFrom for T where
    T: From<U>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Typeable for T where
    T: Any

Get the TypeId of this object.

impl<T> IntoCollection for T
[src]

impl<T, I> AsResult for T where
    I: Input
[src]