Skip to content

Commit

Permalink
refactor: RequestExt
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Dec 11, 2023
1 parent b637411 commit e38efed
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
3 changes: 1 addition & 2 deletions viz-core/src/from_request.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//! Extracts data from the [Request] by types.
use std::convert::Infallible;
use std::future::Future;

use crate::{IntoResponse, Request};
use crate::{Future, IntoResponse, Request};

/// An interface for extracting data from the HTTP [`Request`].
pub trait FromRequest: Sized {
Expand Down
24 changes: 13 additions & 11 deletions viz-core/src/request.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::mem::replace;

use crate::{
async_trait, header,
header,
types::{PayloadError, RealIp},
Bytes, FromRequest, Incoming, IncomingBody, Request, Result,
Bytes, FromRequest, Future, Incoming, IncomingBody, Request, Result,
};
use headers::HeaderMapExt;
use http_body_util::{BodyExt, Collected};
Expand Down Expand Up @@ -38,7 +38,6 @@ use crate::types::Session;
use crate::types::{ParamsError, PathDeserializer, RouteInfo};

/// The [Request] Extension.
#[async_trait]
pub trait RequestExt: Sized {
/// Get URL's schema of this request.
fn schema(&self) -> Option<&http::uri::Scheme>;
Expand Down Expand Up @@ -77,7 +76,7 @@ pub trait RequestExt: Sized {
fn content_type(&self) -> Option<mime::Mime>;

/// Extract the data from this request by the specified type.
async fn extract<T>(&mut self) -> Result<T, T::Error>
fn extract<T>(&mut self) -> impl Future<Output = Result<T, T::Error>> + Send
where
T: FromRequest;

Expand All @@ -95,33 +94,37 @@ pub trait RequestExt: Sized {
/// Return with a [Bytes][mdn] representation of the request body.
///
/// [mdn]: <https://developer.mozilla.org/en-US/docs/Web/API/Response/arrayBuffer>
async fn bytes(&mut self) -> Result<Bytes, PayloadError>;
fn bytes(&mut self) -> impl Future<Output = Result<Bytes, PayloadError>> + Send;

/// Return with a [Bytes][mdn] by a limit representation of the request body.
///
/// [mdn]: <https://developer.mozilla.org/en-US/docs/Web/API/Response/arrayBuffer>
#[cfg(feature = "limits")]
async fn bytes_with(&mut self, limit: Option<u64>, max: u64) -> Result<Bytes, PayloadError>;
fn bytes_with(
&mut self,
limit: Option<u64>,
max: u64,
) -> impl Future<Output = Result<Bytes, PayloadError>> + Send;

/// Return with a [Text][mdn] representation of the request body.
///
/// [mdn]: <https://developer.mozilla.org/en-US/docs/Web/API/Response/text>
async fn text(&mut self) -> Result<String, PayloadError>;
fn text(&mut self) -> impl Future<Output = Result<String, PayloadError>> + Send;

/// Return with a `application/x-www-form-urlencoded` [FormData][mdn] by the specified type
/// representation of the request body.
///
/// [mdn]: <https://developer.mozilla.org/en-US/docs/Web/API/FormData>
#[cfg(feature = "form")]
async fn form<T>(&mut self) -> Result<T, PayloadError>
fn form<T>(&mut self) -> impl Future<Output = Result<T, PayloadError>> + Send
where
T: serde::de::DeserializeOwned;

/// Return with a [JSON][mdn] by the specified type representation of the request body.
///
/// [mdn]: <https://developer.mozilla.org/en-US/docs/Web/API/Response/json>
#[cfg(feature = "json")]
async fn json<T>(&mut self) -> Result<T, PayloadError>
fn json<T>(&mut self) -> impl Future<Output = Result<T, PayloadError>> + Send
where
T: serde::de::DeserializeOwned;

Expand All @@ -130,7 +133,7 @@ pub trait RequestExt: Sized {
///
/// [mdn]: <https://developer.mozilla.org/en-US/docs/Web/API/FormData>
#[cfg(feature = "multipart")]
async fn multipart(&mut self) -> Result<Multipart, PayloadError>;
fn multipart(&mut self) -> impl Future<Output = Result<Multipart, PayloadError>> + Send;

/// Return a shared state by the specified type.
#[cfg(feature = "state")]
Expand Down Expand Up @@ -198,7 +201,6 @@ pub trait RequestExt: Sized {
fn realip(&self) -> Option<RealIp>;
}

#[async_trait]
impl RequestExt for Request {
fn schema(&self) -> Option<&http::uri::Scheme> {
self.uri().scheme()
Expand Down

0 comments on commit e38efed

Please sign in to comment.