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

axum-extra: WithRejection #1262

Merged
merged 18 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions axum-extra/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protobuf = ["dep:prost"]
query = ["dep:serde", "dep:serde_html_form"]
spa = ["tower-http/fs"]
typed-routing = ["dep:axum-macros", "dep:serde", "dep:percent-encoding"]
with_rejection = []
davidpdrsn marked this conversation as resolved.
Show resolved Hide resolved

[dependencies]
axum = { path = "../axum", version = "0.5", default-features = false }
Expand Down
6 changes: 6 additions & 0 deletions axum-extra/src/extract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ pub mod cookie;
#[cfg(feature = "query")]
mod query;

#[cfg(feature = "with_rejection")]
mod with_rejection;

pub use self::cached::Cached;

#[cfg(feature = "cookie")]
Expand All @@ -31,3 +34,6 @@ pub use self::query::Query;
#[cfg(feature = "json-lines")]
#[doc(no_inline)]
pub use crate::json_lines::JsonLines;

#[cfg(feature = "with_rejection")]
pub use self::with_rejection::WithRejection;
45 changes: 45 additions & 0 deletions axum-extra/src/extract/with_rejection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use axum::async_trait;
use axum::extract::{FromRequest, RequestParts};
use axum::response::IntoResponse;
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};

#[derive(Debug, Clone, Copy, Default)]
davidpdrsn marked this conversation as resolved.
Show resolved Hide resolved
pub struct WithRejection<E, R>(pub E, pub PhantomData<R>);
davidpdrsn marked this conversation as resolved.
Show resolved Hide resolved

impl<E, R> WithRejection<E, R> {
fn into_inner(self) -> E {
self.0
}
}

impl<E, R> Deref for WithRejection<E, R> {
type Target = E;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<E, R> DerefMut for WithRejection<E, R> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

#[async_trait]
impl<B, E, R> FromRequest<B> for WithRejection<E, R>
where
B: Send,
E: FromRequest<B>,
R: From<E::Rejection> + IntoResponse,
{
type Rejection = R;

async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
match req.extract::<E>().await {
Ok(extractor) => Ok(WithRejection(extractor, PhantomData)),
Err(err) => Err(err.into()),
}
davidpdrsn marked this conversation as resolved.
Show resolved Hide resolved
}
}