Skip to main content

InnerPath

Struct InnerPath 

Source
pub struct InnerPath<T>(pub T);
Expand description

Extractor that will get captures from the path of the innermost router and parse them using serde.

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

§InnerPath vs Path

Path gives you every capture in the request path, including those matched by enclosing nest calls. InnerPath gives you only the ones from the route the innermost router matched. If the router isn’t nested, or is nested only at static paths, InnerPath and Path see exactly the same captures.

§Option<InnerPath<T>> behavior

You can use Option<InnerPath<T>> as an extractor to allow the same handler to be used in a route with parameters that deserialize to T, and another route with no parameters at all.

§Example

These examples assume the serde feature of the uuid crate is enabled.

One InnerPath can extract multiple captures. It is not necessary (and does not work) to give a handler more than one InnerPath argument.

use axum::{
    extract::InnerPath,
    routing::get,
    Router,
};
use uuid::Uuid;

async fn user_info(InnerPath(user_id): InnerPath<Uuid>) {
    // `user_id` is from the `{user_id}` capture,
    // while `{tenant_id}` from the outer router isn't included,
    // so this handler works no matter what it's nested under.
}

let api = Router::new().route("/users/{user_id}", get(user_info));
let app = Router::new().nest("/tenants/{tenant_id}", api);

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

use axum::{
    extract::InnerPath,
    routing::get,
    Router,
};
use serde::Deserialize;
use uuid::Uuid;

#[derive(Deserialize)]
struct Params {
    user_id: Uuid,
    team_id: Uuid,
}

async fn users_teams_show(
    InnerPath(Params { user_id, team_id }): InnerPath<Params>,
) {
    // Capture parameters will be matched by struct field names
}

async fn users_teams_create(
    InnerPath((user_id, team_id)): InnerPath<(String, String)>,
) {
    // When using tuples captures will be matched by their position in the route
}

let api = Router::new().route(
    "/users/{user_id}/team/{team_id}",
    get(users_teams_show).post(users_teams_create)
);
let app = Router::new().nest("/tenants/{tenant_id}", api);

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

use axum::{
    extract::InnerPath,
    routing::get,
    Router,
};
use std::collections::HashMap;

async fn params_map(
    InnerPath(params): InnerPath<HashMap<String, String>>,
) {
    // `params` contains keys "user_id" and "team_id"
}

async fn params_vec(
    InnerPath(params): InnerPath<Vec<(String, String)>>,
) {
    // `params` contains one tuple for `user_id` and one for `team_id`
}

let api = Router::new()
    .route("/users/{user_id}/team/{team_id}", get(params_map).post(params_vec));
let app = Router::new().nest("/tenants/{tenant_id}", api);

§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. See customize-path-rejection for an example of how to customize that error.

Tuple Fields§

§0: T

Trait Implementations§

Source§

impl<T: Debug> Debug for InnerPath<T>

Source§

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

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

impl<T> Deref for InnerPath<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl<T> DerefMut for InnerPath<T>

Source§

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

Mutably dereferences the value.
Source§

impl<T, S> FromRequestParts<S> for InnerPath<T>
where T: DeserializeOwned + Send, S: Send + Sync,

Source§

type Rejection = InnerPathRejection

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§

async fn from_request_parts( parts: &mut Parts, _state: &S, ) -> Result<Self, Self::Rejection>

Perform the extraction.
Source§

impl<T, S> OptionalFromRequestParts<S> for InnerPath<T>
where T: DeserializeOwned + Send + 'static, S: Send + Sync,

Source§

type Rejection = InnerPathRejection

If the extractor fails, it will use this “rejection” type. Read more
Source§

async fn from_request_parts( parts: &mut Parts, _state: &S, ) -> Result<Option<Self>, Self::Rejection>

Perform the extraction.

Auto Trait Implementations§

§

impl<T> Freeze for InnerPath<T>
where T: Freeze,

§

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

§

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

§

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

§

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

§

impl<T> UnsafeUnpin for InnerPath<T>
where T: UnsafeUnpin,

§

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

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> 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S, T> FromRequest<S, ViaParts> for T
where S: Send + Sync, T: FromRequestParts<S>,

Source§

type Rejection = <T as FromRequestParts<S>>::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_request( req: Request<Body>, state: &S, ) -> impl Future<Output = Result<T, <T as FromRequest<S, ViaParts>>::Rejection>>

Perform the extraction.
§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more