mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-29 00:00:18 +02:00
Support Option and Result in typed paths (#1001)
* Support `Option` and `Result` in typed paths * changelog * Update axum-extra/CHANGELOG.md Co-authored-by: Jonas Platte <[email protected]> * fix one more Co-authored-by: Jonas Platte <[email protected]>
This commit is contained in:
co-authored by
Jonas Platte
parent
d19beffd6d
commit
4ff78e552d
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning].
|
|||||||
|
|
||||||
# Unreleased
|
# Unreleased
|
||||||
|
|
||||||
- None.
|
- **fixed:** `Option` and `Result` are now supported in typed path route handler parameters ([#1001])
|
||||||
|
|
||||||
|
[#1001]: https://github.com/tokio-rs/axum/pull/1001
|
||||||
|
|
||||||
# 0.3.0 (27. April, 2022)
|
# 0.3.0 (27. April, 2022)
|
||||||
|
|
||||||
|
|||||||
@@ -190,6 +190,26 @@ macro_rules! impl_first_element_is {
|
|||||||
where
|
where
|
||||||
P: TypedPath
|
P: TypedPath
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
impl<P, $($ty,)*> FirstElementIs<P> for (Option<P>, $($ty,)*)
|
||||||
|
where
|
||||||
|
P: TypedPath
|
||||||
|
{}
|
||||||
|
|
||||||
|
impl<P, $($ty,)*> Sealed for (Option<P>, $($ty,)*)
|
||||||
|
where
|
||||||
|
P: TypedPath
|
||||||
|
{}
|
||||||
|
|
||||||
|
impl<P, E, $($ty,)*> FirstElementIs<P> for (Result<P, E>, $($ty,)*)
|
||||||
|
where
|
||||||
|
P: TypedPath
|
||||||
|
{}
|
||||||
|
|
||||||
|
impl<P, E, $($ty,)*> Sealed for (Result<P, E>, $($ty,)*)
|
||||||
|
where
|
||||||
|
P: TypedPath
|
||||||
|
{}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
# Unreleased
|
# Unreleased
|
||||||
|
|
||||||
- None.
|
- **fixed:** `Option` and `Result` are now supported in typed path route handler parameters ([#1001])
|
||||||
|
|
||||||
|
[#1001]: https://github.com/tokio-rs/axum/pull/1001
|
||||||
|
|
||||||
# 0.2.0 (31. March, 2022)
|
# 0.2.0 (31. March, 2022)
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
use axum_extra::routing::{TypedPath, RouterExt};
|
||||||
|
use axum::{extract::rejection::PathRejection, http::StatusCode};
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
#[derive(TypedPath, Deserialize)]
|
||||||
|
#[typed_path("/users/:id")]
|
||||||
|
struct UsersShow {
|
||||||
|
id: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn option_handler(_: Option<UsersShow>) {}
|
||||||
|
|
||||||
|
async fn result_handler(_: Result<UsersShow, PathRejection>) {}
|
||||||
|
|
||||||
|
#[derive(TypedPath, Deserialize)]
|
||||||
|
#[typed_path("/users")]
|
||||||
|
struct UsersIndex;
|
||||||
|
|
||||||
|
#[axum_macros::debug_handler]
|
||||||
|
async fn result_handler_unit_struct(_: Result<UsersIndex, StatusCode>) {}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
axum::Router::<axum::body::Body>::new()
|
||||||
|
.typed_get(option_handler)
|
||||||
|
.typed_post(result_handler)
|
||||||
|
.typed_post(result_handler_unit_struct);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user