mirror of
https://github.com/tokio-rs/axum.git
synced 2025-03-13 19:27:53 +01: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 <jplatte+git@posteo.de> * fix one more Co-authored-by: Jonas Platte <jplatte+git@posteo.de>
This commit is contained in:
parent
d19beffd6d
commit
4ff78e552d
4 changed files with 53 additions and 2 deletions
|
@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning].
|
|||
|
||||
# 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)
|
||||
|
||||
|
|
|
@ -190,6 +190,26 @@ macro_rules! impl_first_element_is {
|
|||
where
|
||||
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
|
||||
|
||||
- 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)
|
||||
|
||||
|
|
27
axum-macros/tests/typed_path/pass/option_result.rs
Normal file
27
axum-macros/tests/typed_path/pass/option_result.rs
Normal file
|
@ -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);
|
||||
}
|
Loading…
Add table
Reference in a new issue