Struct submillisecond::extract::path::Path
source · 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> FromRequest for Path<T>where
T: DeserializeOwned,
impl<T> FromRequest for Path<T>where T: DeserializeOwned,
§type Rejection = PathRejection
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>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> FromOwnedRequest for Twhere
T: FromRequest,
impl<T> FromOwnedRequest for Twhere T: FromRequest,
§type Rejection = <T as FromRequest>::Rejection
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>
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.