mirror of
https://github.com/tokio-rs/axum.git
synced 2026-09-08 00:00:24 +02:00
Add Either{2..8} to axum-extra (#1263)
* Add `Either{2..8}`
* Apply suggestions from code review
Co-authored-by: Jonas Platte <[email protected]>
* Either2 => Either
* Update axum-extra/src/either.rs
Co-authored-by: Jonas Platte <[email protected]>
Co-authored-by: Jonas Platte <[email protected]>
This commit is contained in:
co-authored by
Jonas Platte
parent
fb21561616
commit
e22f6f9d2b
+1
-46
@@ -63,13 +63,8 @@
|
||||
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
|
||||
#![cfg_attr(test, allow(clippy::float_cmp))]
|
||||
|
||||
use axum::{
|
||||
async_trait,
|
||||
extract::{FromRequest, RequestParts},
|
||||
response::IntoResponse,
|
||||
};
|
||||
|
||||
pub mod body;
|
||||
pub mod either;
|
||||
pub mod extract;
|
||||
pub mod handler;
|
||||
pub mod response;
|
||||
@@ -81,46 +76,6 @@ pub mod json_lines;
|
||||
#[cfg(feature = "protobuf")]
|
||||
pub mod protobuf;
|
||||
|
||||
/// Combines two extractors or responses into a single type.
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub enum Either<L, R> {
|
||||
/// A value of type L.
|
||||
Left(L),
|
||||
/// A value of type R.
|
||||
Right(R),
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<L, R, B> FromRequest<B> for Either<L, R>
|
||||
where
|
||||
L: FromRequest<B>,
|
||||
R: FromRequest<B>,
|
||||
B: Send,
|
||||
{
|
||||
type Rejection = R::Rejection;
|
||||
|
||||
async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
|
||||
if let Ok(l) = req.extract().await {
|
||||
return Ok(Either::Left(l));
|
||||
}
|
||||
|
||||
Ok(Either::Right(req.extract().await?))
|
||||
}
|
||||
}
|
||||
|
||||
impl<L, R> IntoResponse for Either<L, R>
|
||||
where
|
||||
L: IntoResponse,
|
||||
R: IntoResponse,
|
||||
{
|
||||
fn into_response(self) -> axum::response::Response {
|
||||
match self {
|
||||
Self::Left(inner) => inner.into_response(),
|
||||
Self::Right(inner) => inner.into_response(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "typed-routing")]
|
||||
#[doc(hidden)]
|
||||
pub mod __private {
|
||||
|
||||
Reference in New Issue
Block a user