Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Option and Result in typed paths #1001

Merged
merged 4 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}