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

Implement TypedPath for WithRejection<TypedPath, _> #2491

Merged
merged 2 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion axum-extra/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning].

# Unreleased

- None.
- **added:** Implement `TypedPath` for `WithRejection<TypedPath, _>`

# 0.9.1 (29. December, 2023)

Expand Down
23 changes: 22 additions & 1 deletion axum-extra/src/extract/with_rejection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use axum::async_trait;
use axum::extract::{FromRequest, FromRequestParts, Request};
use axum::response::IntoResponse;
use http::request::Parts;
use std::fmt::Debug;
use std::fmt::{Debug, Display};
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};

Expand Down Expand Up @@ -137,6 +137,27 @@ where
}
}

#[cfg(feature = "typed-routing")]
const _: () = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do it this way? I find the const _ trick a bit weird.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just so you don't need to put #[cfg(feature = "typed-routing")] on the imports as well (unless you want warnings) or use fully qualified paths.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd prefer either of those solutions 😅

cc rust-lang/rust#65516 ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope that doesn't land 😅 I think its pretty useful to be able to define stuff everywhere though I do see its a nightmare from and IDE point of view.

But I've fixed it in c1b2756

use crate::routing::TypedPath;

impl<E, R> TypedPath for WithRejection<E, R>
where
E: TypedPath,
{
const PATH: &'static str = E::PATH;
}
};

impl<E, R> Display for WithRejection<E, R>
where
E: Display,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}

#[cfg(test)]
mod tests {
use axum::body::Body;
Expand Down
31 changes: 30 additions & 1 deletion axum-extra/src/routing/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,15 @@ impl_second_element_is!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13,
#[cfg(test)]
mod tests {
use super::*;
use crate::routing::TypedPath;
use crate::{
extract::WithRejection,
routing::{RouterExt, TypedPath},
};
use axum::{
extract::rejection::PathRejection,
response::{IntoResponse, Response},
Router,
};
use serde::Deserialize;

#[derive(TypedPath, Deserialize)]
Expand Down Expand Up @@ -434,4 +442,25 @@ mod tests {

assert_eq!(uri, "/users/1?&foo=foo&bar=123&baz=true&qux=1337");
}

#[allow(dead_code)] // just needs to compile
fn supports_with_rejection() {
async fn handler(_: WithRejection<UsersShow, MyRejection>) {}

struct MyRejection {}

impl IntoResponse for MyRejection {
fn into_response(self) -> Response {
unimplemented!()
}
}

impl From<PathRejection> for MyRejection {
fn from(_: PathRejection) -> Self {
unimplemented!()
}
}

let _: Router = Router::new().typed_get(handler);
}
}