Struct rocket::Route
[−]
[src]
pub struct Route { pub method: Method, pub handler: Handler, pub base: URI<'static>, pub uri: URI<'static>, pub rank: isize, pub format: Option<MediaType>, }
A route: a method, its handler, path, rank, and format/media type.
Fields
method: Method
The method this route matches against.
handler: Handler
The function that should be called when the route matches.
base: URI<'static>
The base mount point of this Route
.
uri: URI<'static>
The uri (in Rocket format) that should be matched against. This uri already includes the base mount point.
rank: isize
The rank of this route. Lower ranks have higher priorities.
format: Option<MediaType>
The media type this route matches against, if any.
Methods
impl Route
[src]
pub fn new<S>(m: Method, path: S, handler: Handler) -> Route where
S: AsRef<str>,
[src]
S: AsRef<str>,
Creates a new route with the given method, path, and handler with a base
of /
.
Ranking
The route rank's is set so that routes with static paths are ranked higher than route's with dynamic paths, and routes with query strings are ranked higher than ranks without query strings. This default ranking is summarized by the table below:
static path | query | rank |
---|---|---|
yes | yes | -4 |
yes | no | -3 |
no | yes | -2 |
no | no | -1 |
Example
use rocket::{Request, Route, Data}; use rocket::handler::Outcome; use rocket::http::Method; fn handler<'r>(request: &'r Request, _data: Data) -> Outcome<'r> { Outcome::from(request, "Hello, world!") } // this is a rank -3 route matching requests to `GET /` let index = Route::new(Method::Get, "/", handler); // this is a rank -4 route matching requests to `GET /?<name>` let index_name = Route::new(Method::Get, "/?<name>", handler); // this is a rank -1 route matching requests to `GET /<name>` let name = Route::new(Method::Get, "/<name>", handler);
pub fn ranked<S>(rank: isize, m: Method, uri: S, handler: Handler) -> Route where
S: AsRef<str>,
[src]
S: AsRef<str>,
Creates a new route with the given rank, method, path, and handler with
a base of /
.
Example
use rocket::{Request, Route, Data}; use rocket::handler::Outcome; use rocket::http::Method; fn handler<'r>(request: &'r Request, _data: Data) -> Outcome<'r> { Outcome::from(request, "Hello, world!") } // this is a rank 1 route matching requests to `GET /` let index = Route::ranked(1, Method::Get, "/", handler);
pub fn base(&self) -> &str
[src]
Retrieves the path of the base mount point of this route as an &str
.
Example
use rocket::{Request, Route, Data}; use rocket::handler::Outcome; use rocket::http::Method; fn handler<'r>(request: &'r Request, _data: Data) -> Outcome<'r> { Outcome::from(request, "Hello, world!") } let mut index = Route::ranked(1, Method::Get, "/", handler); assert_eq!(index.base(), "/"); assert_eq!(index.base.path(), "/");
pub fn set_base<S>(&mut self, path: S) where
S: AsRef<str>,
[src]
S: AsRef<str>,
Sets the base mount point of the route. Does not update the rank or any other parameters.
Example
use rocket::{Request, Route, Data}; use rocket::handler::Outcome; use rocket::http::Method; fn handler<'r>(request: &'r Request, _data: Data) -> Outcome<'r> { Outcome::from(request, "Hello, world!") } let mut index = Route::ranked(1, Method::Get, "/", handler); assert_eq!(index.base(), "/"); assert_eq!(index.base.path(), "/"); index.set_base("/hi"); assert_eq!(index.base(), "/hi"); assert_eq!(index.base.path(), "/hi");
pub fn set_uri<S>(&mut self, uri: S) where
S: AsRef<str>,
[src]
S: AsRef<str>,
Sets the path of the route. Does not update the rank or any other parameters.
Example
use rocket::{Request, Route, Data}; use rocket::handler::Outcome; use rocket::http::Method; fn handler<'r>(request: &'r Request, _data: Data) -> Outcome<'r> { Outcome::from(request, "Hello, world!") } let mut index = Route::ranked(1, Method::Get, "/", handler); assert_eq!(index.uri.path(), "/"); index.set_uri("/hello"); assert_eq!(index.uri.path(), "/hello");
Trait Implementations
impl<'a, 'r> FromRequest<'a, 'r> for &'r Route
[src]
type Error = ()
The associated error to be returned if derivation fails.
fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error>
[src]
Derives an instance of Self
from the incoming request metadata. Read more
impl Clone for Route
[src]
fn clone(&self) -> Route
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl Display for Route
[src]
fn fmt(&self, f: &mut Formatter) -> Result
[src]
Formats the value using the given formatter. Read more