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

refactor: RPITIT and AFIT #110

Merged
merged 8 commits into from
Jan 1, 2024
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ homepage = "https://viz.rs"
documentation = "https://docs.rs/viz"
repository = "https://github.com/viz-rs/viz"
license = "MIT"
rust-version = "1.63" # follows `tokio` and `hyper`
rust-version = "1.75"

[workspace.dependencies]
viz = { version = "0.7.1", path = "viz" }
Expand Down
8 changes: 3 additions & 5 deletions viz-core/src/from_request.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
//! Extracts data from the [`Request`] by types.

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

/// An interface for extracting data from the HTTP [`Request`].
#[crate::async_trait]
pub trait FromRequest: Sized {
/// The type returned in the event of a conversion error.
type Error: IntoResponse;

/// Extracts this type from the HTTP [`Request`].
async fn extract(req: &mut Request) -> Result<Self, Self::Error>;
#[must_use]
fn extract(req: &mut Request) -> impl Future<Output = Result<Self, Self::Error>> + Send;
}

#[crate::async_trait]
impl<T> FromRequest for Option<T>
where
T: FromRequest,
Expand All @@ -24,7 +23,6 @@ where
}
}

#[crate::async_trait]
impl<T> FromRequest for Result<T, T::Error>
where
T: FromRequest,
Expand Down
2 changes: 1 addition & 1 deletion viz-core/src/handler/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ impl<I, O> BoxHandler<I, O> {
/// Creates a new `BoxHandler`.
pub fn new<H>(h: H) -> Self
where
H: Handler<I, Output = O> + Send + Sync + Clone + 'static,
H: Handler<I, Output = O> + Clone,
{
Self(Box::new(h))
}
Expand Down
1 change: 0 additions & 1 deletion viz-core/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ macro_rules! tuple_impls {
};
// "Private" internal implementation
(@impl $( $T:ident )*) => {
#[crate::async_trait]
impl<$($T,)*> FromRequest for ($($T,)*)
where
$($T: FromRequest + Send,)*
Expand Down
1 change: 0 additions & 1 deletion viz-core/src/middleware/csrf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ pub enum Store {
#[derive(Debug, Clone)]
pub struct CsrfToken(pub String);

#[crate::async_trait]
impl FromRequest for CsrfToken {
type Error = Error;

Expand Down
22 changes: 12 additions & 10 deletions viz-core/src/request.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
header,
types::{PayloadError, RealIp},
Body, BodyState, Bytes, FromRequest, Request, Result,
Body, BodyState, Bytes, FromRequest, Future, Request, Result,
};
use headers::HeaderMapExt;
use http_body_util::{BodyExt, Collected};
Expand Down Expand Up @@ -36,7 +36,6 @@ use crate::types::Session;
use crate::types::{ParamsError, PathDeserializer, RouteInfo};

/// The [`Request`] Extension.
#[crate::async_trait]
pub trait RequestExt: private::Sealed + Sized {
/// Get URL's schema of this request.
fn schema(&self) -> Option<&http::uri::Scheme>;
Expand Down Expand Up @@ -75,7 +74,7 @@ pub trait RequestExt: private::Sealed + 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 @@ -90,33 +89,37 @@ pub trait RequestExt: private::Sealed + 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 @@ -125,7 +128,7 @@ pub trait RequestExt: private::Sealed + 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 @@ -193,7 +196,6 @@ pub trait RequestExt: private::Sealed + Sized {
fn realip(&self) -> Option<RealIp>;
}

#[crate::async_trait]
impl RequestExt for Request {
fn schema(&self) -> Option<&http::uri::Scheme> {
self.uri().scheme()
Expand Down
6 changes: 2 additions & 4 deletions viz-core/src/response.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use futures_util::Stream;
use http_body_util::Full;

use crate::{header, Body, BoxError, Bytes, Error, Response, Result, StatusCode};
use crate::{header, Body, BoxError, Bytes, Error, Future, Response, Result, StatusCode};

/// The [`Response`] Extension.
#[crate::async_trait]
pub trait ResponseExt: private::Sealed + Sized {
/// Get the size of this response's body.
fn content_length(&self) -> Option<u64>;
Expand Down Expand Up @@ -96,7 +95,7 @@ pub trait ResponseExt: private::Sealed + Sized {

/// Downloads transfers the file from path as an attachment.
#[cfg(feature = "fs")]
async fn download<T>(path: T, name: Option<&str>) -> Result<Self>
fn download<T>(path: T, name: Option<&str>) -> impl Future<Output = Result<Self>> + Send
where
T: AsRef<std::path::Path> + Send;

Expand Down Expand Up @@ -186,7 +185,6 @@ pub trait ResponseExt: private::Sealed + Sized {
}
}

#[crate::async_trait]
impl ResponseExt for Response {
fn content_length(&self) -> Option<u64> {
self.headers()
Expand Down
1 change: 0 additions & 1 deletion viz-core/src/types/cookie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ impl Cookies {
}
}

#[crate::async_trait]
impl FromRequest for Cookies {
type Error = CookiesError;

Expand Down
1 change: 0 additions & 1 deletion viz-core/src/types/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ impl<T> Payload for Form<T> {
}
}

#[crate::async_trait]
impl<T> FromRequest for Form<T>
where
T: DeserializeOwned,
Expand Down
1 change: 0 additions & 1 deletion viz-core/src/types/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ where
}
}

#[crate::async_trait]
impl<T> FromRequest for Header<T>
where
T: headers::Header,
Expand Down
1 change: 0 additions & 1 deletion viz-core/src/types/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ impl<T> Payload for Json<T> {
}
}

#[crate::async_trait]
impl<T> FromRequest for Json<T>
where
T: serde::de::DeserializeOwned,
Expand Down
1 change: 0 additions & 1 deletion viz-core/src/types/limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ impl Limits {
}
}

#[crate::async_trait]
impl FromRequest for Limits {
type Error = Infallible;

Expand Down
1 change: 0 additions & 1 deletion viz-core/src/types/multipart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ impl<T> Payload for Multipart<T> {
}
}

#[crate::async_trait]
impl FromRequest for Multipart {
type Error = PayloadError;

Expand Down
1 change: 0 additions & 1 deletion viz-core/src/types/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ impl Params {
}
}

#[crate::async_trait]
impl<T> FromRequest for Params<T>
where
T: DeserializeOwned,
Expand Down
1 change: 0 additions & 1 deletion viz-core/src/types/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ where
}
}

#[crate::async_trait]
impl<T> FromRequest for Query<T>
where
T: DeserializeOwned,
Expand Down
1 change: 0 additions & 1 deletion viz-core/src/types/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ impl fmt::Debug for Session {
}
}

#[crate::async_trait]
impl FromRequest for Session {
type Error = Infallible;

Expand Down
1 change: 0 additions & 1 deletion viz-core/src/types/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ impl<T> DerefMut for State<T> {
}
}

#[crate::async_trait]
impl<T> FromRequest for State<T>
where
T: Clone + Send + Sync + 'static,
Expand Down
1 change: 0 additions & 1 deletion viz-core/src/types/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ impl WebSocket {
}
}

#[crate::async_trait]
impl FromRequest for WebSocket {
type Error = WebSocketError;

Expand Down
4 changes: 2 additions & 2 deletions viz-core/tests/from_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use headers::HeaderValue;
use viz_core::{
header::{CONTENT_LENGTH, CONTENT_TYPE},
types::{Form, Json, Limits, PayloadError, State, StateError},
Body, FromRequest, Request, RequestExt, Result,
Body, Request, RequestExt, Result,
};

#[tokio::test]
Expand All @@ -16,7 +16,7 @@ async fn from_request() -> Result<()> {
.body(Body::Empty)?;
req.extensions_mut().insert(Limits::default());

let result: Result<Json<Option<String>>, PayloadError> = FromRequest::extract(&mut req).await;
let result: Result<Json<Option<String>>, PayloadError> = req.extract().await;
assert!(result.is_err());

let mut req = Request::builder()
Expand Down
4 changes: 1 addition & 3 deletions viz-core/tests/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,16 @@ async fn handler() -> Result<()> {

struct MyU8(u8);

#[async_trait]
impl FromRequest for MyU8 {
type Error = std::convert::Infallible;

async fn extract(_req: &mut Request) -> Result<Self, Self::Error> {
async fn extract(_: &mut Request) -> Result<Self, Self::Error> {
Ok(MyU8(u8::MAX))
}
}

struct MyString(String);

#[async_trait]
impl FromRequest for MyString {
type Error = std::convert::Infallible;

Expand Down
6 changes: 1 addition & 5 deletions viz-macros/tests/handler.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
#![allow(clippy::unused_async)]
#![allow(clippy::unnecessary_wraps)]

use viz_core::{
async_trait, Error, FromRequest, Handler, IntoResponse, Request, Result, StatusCode,
};
use viz_core::{Error, FromRequest, Handler, IntoResponse, Request, Result, StatusCode};
use viz_macros::handler;

#[derive(Debug)]
struct Foo;

#[async_trait]
impl FromRequest for Foo {
type Error = Error;

Expand All @@ -21,7 +18,6 @@ impl FromRequest for Foo {
#[derive(Debug)]
struct Bar;

#[async_trait]
impl FromRequest for Bar {
type Error = Error;

Expand Down
3 changes: 1 addition & 2 deletions viz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,10 +342,9 @@
//!
//! ```
//! # use std::{cmp, convert::Infallible};
//! # use viz::{async_trait, FromRequest, Request, RequestExt, Result};
//! # use viz::{FromRequest, Request, RequestExt, Result};
//! struct Counter(u16);
//!
//! #[async_trait]
//! impl FromRequest for Counter {
//! type Error = Infallible;
//! async fn extract(req: &mut Request) -> Result<Self, Self::Error> {
Expand Down