Skip to content

Commit

Permalink
Support Option and Result in typed paths (#1001)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
davidpdrsn and jplatte authored May 6, 2022
1 parent d19beff commit 4ff78e5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
4 changes: 3 additions & 1 deletion axum-extra/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
20 changes: 20 additions & 0 deletions axum-extra/src/routing/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{}
};
}

Expand Down
4 changes: 3 additions & 1 deletion axum-macros/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
27 changes: 27 additions & 0 deletions axum-macros/tests/typed_path/pass/option_result.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 4ff78e5

Please sign in to comment.