Struct rocket::Route

source ·
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>,
    /* private fields */
}
Expand description

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

Fields§

§name: Option<&'static str>

The name of this route, if one was given.

§method: Method

The method this route matches against.

§handler: Box<dyn Handler>

The function that should be called when the route matches.

§base: Origin<'static>

The base mount point of this Route.

§uri: Origin<'static>

The uri (in Rocket’s route 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.

Implementations§

source§

impl Route

source

pub fn new<S, H>(method: Method, path: S, handler: H) -> Route
where S: AsRef<str>, H: Handler + 'static,

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 pathqueryrank
yespartly static-6
yesfully dynamic-5
yesnone-4
nopartly static-3
nofully dynamic-2
nonone-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.

source

pub fn ranked<S, H>(rank: isize, method: Method, path: S, handler: H) -> Route
where S: AsRef<str>, H: Handler + 'static,

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.

source

pub fn base(&self) -> &str

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(), "/");
source

pub fn set_uri<'a>( &mut self, base: Origin<'a>, path: Origin<'a>, ) -> Result<(), RouteUriError>

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§

source§

impl Clone for Route

source§

fn clone(&self) -> Route

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for Route

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Display for Route

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a, 'r> FromRequest<'a, 'r> for &'r Route

source§

type Error = !

The associated error to be returned if derivation fails.
source§

fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error>

Derives an instance of Self from the incoming request metadata. Read more

Auto Trait Implementations§

§

impl !Freeze for Route

§

impl !RefUnwindSafe for Route

§

impl Send for Route

§

impl Sync for Route

§

impl Unpin for Route

§

impl !UnwindSafe for Route

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T, I> AsResult<T, I> for T
where I: Input,

source§

fn as_result(self) -> Result<T, ParseErr<I>>

source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> IntoCollection<T> for T

§

fn into_collection<A>(self) -> SmallVec<A>
where A: Array<Item = T>,

Converts self into a collection.
§

fn mapped<U, F, A>(self, f: F) -> SmallVec<A>
where F: FnMut(T) -> U, A: Array<Item = U>,

source§

impl<T> Same for T

source§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> Typeable for T
where T: Any,

source§

fn get_type(&self) -> TypeId

Get the TypeId of this object.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V