pub struct Path<T>(pub T);
Expand description

Extractor that will get captures from the URL and parse them using serde.

Any percent encoded parameters will be automatically decoded. The decoded parameters must be valid UTF-8, otherwise Path will fail and return a 400 Bad Request response.

Example

use submillisecond::{router, extract::Path};
use uuid::Uuid;

fn users_teams_show(
    Path((user_id, team_id)): Path<(Uuid, Uuid)>,
) {
    // ...
}

router! {
    GET "/users/:user_id/team/:team_id" => users_teams_show
}

If the path contains only one parameter, then you can omit the tuple.

use submillisecond::{router, extract::Path};
use uuid::Uuid;

fn user_info(
    Path(user_id): Path<Uuid>,
) {
    // ...
}

router! {
    GET "/users/:user_id" => user_info
}

Path segments also can be deserialized into any type that implements serde::Deserialize. This includes tuples and structs:

use serde::Deserialize;
use submillisecond::{router, extract::Path};
use uuid::Uuid;

// Path segment labels will be matched with struct field names
#[derive(Deserialize)]
struct Params {
    user_id: Uuid,
    team_id: Uuid,
}

fn users_teams_show(
    Path(Params { user_id, team_id }): Path<Params>,
) {
    // ...
}

// When using tuples the path segments will be matched by their position in the route
fn users_teams_create(
    Path((user_id, team_id)): Path<(String, String)>,
) {
    // ...
}

router! {
    GET "/users/:user_id/team/:team_id" => users_teams_show
    POST "/users/:user_id/team/:team_id" => users_teams_create
}

If you wish to capture all path parameters you can use HashMap or Vec:

use submillisecond::{router, extract::Path};
use std::collections::HashMap;

fn params_map(
    Path(params): Path<HashMap<String, String>>,
) {
    // ...
}

fn params_vec(
    Path(params): Path<Vec<(String, String)>>,
) {
    // ...
}

router! {
    GET "/users/:user_id/team/:team_id" => params_map
    POST "/users/:user_id/team/:team_id" => params_vec
}

Providing detailed rejection output

If the URI cannot be deserialized into the target type the request will be rejected and an error response will be returned.

Tuple Fields§

§0: T

Trait Implementations§

source§

impl<T: Debug> Debug for Path<T>

source§

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

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

impl<T> Deref for Path<T>

§

type Target = T

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<T> DerefMut for Path<T>

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
source§

impl<T> FromRequest for Path<T>where T: DeserializeOwned,

§

type Rejection = PathRejection

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response.
source§

fn from_request(req: &mut RequestContext) -> Result<Self, Self::Rejection>

Perform the extraction.

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for Path<T>where T: RefUnwindSafe,

§

impl<T> Send for Path<T>where T: Send,

§

impl<T> Sync for Path<T>where T: Sync,

§

impl<T> Unpin for Path<T>where T: Unpin,

§

impl<T> UnwindSafe for Path<T>where T: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> FromOwnedRequest for Twhere T: FromRequest,

§

type Rejection = <T as FromRequest>::Rejection

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response.
source§

fn from_owned_request( req: RequestContext ) -> Result<T, <T as FromOwnedRequest>::Rejection>

Extract from an owned instance of the request. The first extractor in handlers will use this method, and can help avoid cloning in many cases.
source§

impl<T, U> Into<U> for Twhere 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.

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

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

§

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 Twhere U: TryFrom<T>,

§

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.
§

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

§

fn vzip(self) -> V

§

impl<T> Formattable for Twhere T: Deref, <T as Deref>::Target: Formattable,

§

impl<T> Parsable for Twhere T: Deref, <T as Deref>::Target: Parsable,