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:
David Pedersen 2022-05-06 09:42:10 +02:00 committed by GitHub
parent d19beffd6d
commit 4ff78e552d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 2 deletions

View file

@ -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)

View file

@ -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
{}
};
}

View file

@ -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)

View 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);
}