Skip to content

Commit

Permalink
chore(core): improve
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Jan 1, 2024
1 parent 768a226 commit cb48f4c
Show file tree
Hide file tree
Showing 46 changed files with 120 additions and 155 deletions.
8 changes: 4 additions & 4 deletions viz-core/src/from_request.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! Extracts data from the [`Request`] by types.
use crate::{async_trait, IntoResponse, Request};
use crate::{IntoResponse, Request};

/// An interface for extracting data from the HTTP [`Request`].
#[async_trait]
#[crate::async_trait]
pub trait FromRequest: Sized {
/// The type returned in the event of a conversion error.
type Error: IntoResponse;
Expand All @@ -12,7 +12,7 @@ pub trait FromRequest: Sized {
async fn extract(req: &mut Request) -> Result<Self, Self::Error>;
}

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

#[async_trait]
#[crate::async_trait]
impl<T> FromRequest for Result<T, T::Error>
where
T: FromRequest,
Expand Down
8 changes: 3 additions & 5 deletions viz-core/src/handler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Traits and types for handling an HTTP.
use crate::{async_trait, Future};

mod cloneable;

mod after;
Expand Down Expand Up @@ -58,7 +56,7 @@ pub use service::ServiceHandler;
/// A simplified asynchronous interface for handling input and output.
///
/// Composable request handlers.
#[async_trait]
#[crate::async_trait]
pub trait Handler<Input>: Send + Sync + 'static {
/// The returned type after the call operator is used.
type Output;
Expand All @@ -67,12 +65,12 @@ pub trait Handler<Input>: Send + Sync + 'static {
async fn call(&self, input: Input) -> Self::Output;
}

#[async_trait]
#[crate::async_trait]
impl<F, I, Fut, O> Handler<I> for F
where
I: Send + 'static,
F: Fn(I) -> Fut + ?Sized + Clone + Send + Sync + 'static,
Fut: Future<Output = O> + Send + 'static,
Fut: ::core::future::Future<Output = O> + Send + 'static,
{
type Output = Fut::Output;

Expand Down
7 changes: 3 additions & 4 deletions viz-core/src/handler/after.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{async_trait, Handler, Result};
use crate::{Handler, Result};

/// Maps the output `Result<T>` after the handler called.
#[derive(Debug, Clone)]
Expand All @@ -15,13 +15,12 @@ impl<H, F> After<H, F> {
}
}

#[async_trait]
#[crate::async_trait]
impl<H, F, I, O> Handler<I> for After<H, F>
where
I: Send + 'static,
H: Handler<I, Output = Result<O>>,
F: Handler<H::Output, Output = H::Output> + Clone + 'static,
O: 'static,
F: Handler<H::Output, Output = H::Output>,
{
type Output = F::Output;

Expand Down
4 changes: 2 additions & 2 deletions viz-core/src/handler/and_then.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{async_trait, Handler, Result};
use crate::{Handler, Result};

/// Calls `op` if the output is `Ok`, otherwise returns the `Err` value of the output.
#[derive(Debug, Clone)]
Expand All @@ -15,7 +15,7 @@ impl<H, F> AndThen<H, F> {
}
}

#[async_trait]
#[crate::async_trait]
impl<H, F, I, O> Handler<I> for AndThen<H, F>
where
I: Send + 'static,
Expand Down
5 changes: 2 additions & 3 deletions viz-core/src/handler/around.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{async_trait, Handler, Result};
use crate::{Handler, Result};

/// Represents a middleware parameter, which is a tuple that includes Requset and `BoxHandler`.
pub type Next<I, H> = (I, H);
Expand All @@ -18,13 +18,12 @@ impl<H, F> Around<H, F> {
}
}

#[async_trait]
#[crate::async_trait]
impl<H, F, I, O> Handler<I> for Around<H, F>
where
I: Send + 'static,
H: Handler<I, Output = Result<O>> + Clone,
F: Handler<Next<I, H>, Output = H::Output>,
O: 'static,
{
type Output = F::Output;

Expand Down
5 changes: 2 additions & 3 deletions viz-core/src/handler/before.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{async_trait, Handler, Result};
use crate::{Handler, Result};

/// Maps the input before the handler calls.
#[derive(Debug, Clone)]
Expand All @@ -15,13 +15,12 @@ impl<H, F> Before<H, F> {
}
}

#[async_trait]
#[crate::async_trait]
impl<H, F, I, O> Handler<I> for Before<H, F>
where
I: Send + 'static,
F: Handler<I, Output = Result<I>>,
H: Handler<I, Output = Result<O>>,
O: 'static,
{
type Output = H::Output;

Expand Down
6 changes: 3 additions & 3 deletions viz-core/src/handler/boxed.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt;

use super::cloneable::BoxCloneable;
use crate::{async_trait, Handler, Request, Response, Result};
use crate::{Handler, Request, Response, Result};

/// A [`Clone`] + [`Send`] boxed [`Handler`].
pub struct BoxHandler<I = Request, O = Result<Response>>(BoxCloneable<I, O>);
Expand All @@ -18,15 +18,15 @@ impl<I, O> BoxHandler<I, O> {

impl<I, O> Clone for BoxHandler<I, O>
where
I: 'static,
I: Send + 'static,
O: 'static,
{
fn clone(&self) -> Self {
Self(self.0.clone_box())
}
}

#[async_trait]
#[crate::async_trait]
impl<I, O> Handler<I> for BoxHandler<I, O>
where
I: Send + 'static,
Expand Down
4 changes: 2 additions & 2 deletions viz-core/src/handler/catch_error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::marker::PhantomData;

use crate::{async_trait, Handler, IntoResponse, Response, Result};
use crate::{Handler, IntoResponse, Response, Result};

/// Catches rejected error while calling the handler.
#[derive(Debug)]
Expand Down Expand Up @@ -36,7 +36,7 @@ impl<H, F, E, R> CatchError<H, F, E, R> {
}
}

#[async_trait]
#[crate::async_trait]
impl<H, I, O, F, E, R> Handler<I> for CatchError<H, F, E, R>
where
I: Send + 'static,
Expand Down
4 changes: 2 additions & 2 deletions viz-core/src/handler/catch_unwind.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{async_trait, future::FutureExt, Handler, IntoResponse, Response, Result};
use crate::{future::FutureExt, Handler, IntoResponse, Response, Result};

/// Catches unwinding panics while calling the handler.
#[derive(Debug, Clone)]
Expand All @@ -15,7 +15,7 @@ impl<H, F> CatchUnwind<H, F> {
}
}

#[async_trait]
#[crate::async_trait]
impl<H, F, I, O, R> Handler<I> for CatchUnwind<H, F>
where
I: Send + 'static,
Expand Down
8 changes: 3 additions & 5 deletions viz-core/src/handler/fn_ext.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use crate::{async_trait, Request};

/// A handler with extractors.
#[async_trait]
pub trait FnExt<E>: Send + Sync + 'static {
#[crate::async_trait]
pub trait FnExt<I, E>: Send + Sync + 'static {
/// The returned type after the call operator is used.
type Output;

/// Performs the call operation.
async fn call(&self, req: Request) -> Self::Output;
async fn call(&self, i: I) -> Self::Output;
}
13 changes: 7 additions & 6 deletions viz-core/src/handler/fn_ext_hanlder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::marker::PhantomData;

use crate::{async_trait, FnExt, FromRequest, Handler, IntoResponse, Request, Result};
use crate::{FnExt, FromRequest, Handler, IntoResponse, Result};

/// A wrapper of the extractors handler.
#[derive(Debug)]
Expand All @@ -22,17 +22,18 @@ impl<H, E, O> FnExtHandler<H, E, O> {
}
}

#[async_trait]
impl<H, E, O> Handler<Request> for FnExtHandler<H, E, O>
#[crate::async_trait]
impl<I, H, E, O> Handler<I> for FnExtHandler<H, E, O>
where
I: Send + 'static,
E: FromRequest + 'static,
E::Error: IntoResponse,
H: FnExt<E, Output = Result<O>>,
H: FnExt<I, E, Output = Result<O>>,
O: 'static,
{
type Output = H::Output;

async fn call(&self, req: Request) -> Self::Output {
self.0.call(req).await.map_err(IntoResponse::into_error)
async fn call(&self, i: I) -> Self::Output {
self.0.call(i).await.map_err(IntoResponse::into_error)
}
}
9 changes: 5 additions & 4 deletions viz-core/src/handler/into_handler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{handler::FnExtHandler, FnExt, FromRequest, Handler, IntoResponse, Request, Result};
use crate::{handler::FnExtHandler, FnExt, FromRequest, Handler, IntoResponse, Result};

/// The trait implemented by types that can be converted to a [`Handler`].
pub trait IntoHandler<E, I> {
pub trait IntoHandler<I, E> {
/// The target handler.
type Handler: Handler<I>;

Expand All @@ -10,11 +10,12 @@ pub trait IntoHandler<E, I> {
fn into_handler(self) -> Self::Handler;
}

impl<H, E, O> IntoHandler<E, Request> for H
impl<I, H, E, O> IntoHandler<I, E> for H
where
I: Send + 'static,
E: FromRequest + 'static,
E::Error: IntoResponse,
H: FnExt<E, Output = Result<O>>,
H: FnExt<I, E, Output = Result<O>>,
O: 'static,
{
type Handler = FnExtHandler<H, E, O>;
Expand Down
4 changes: 2 additions & 2 deletions viz-core/src/handler/map.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{async_trait, Handler, Result};
use crate::{Handler, Result};

/// Maps the `Ok` value of the output if after the handler called.
#[derive(Debug, Clone)]
Expand All @@ -15,7 +15,7 @@ impl<H, F> Map<H, F> {
}
}

#[async_trait]
#[crate::async_trait]
impl<H, F, I, O, T> Handler<I> for Map<H, F>
where
I: Send + 'static,
Expand Down
4 changes: 2 additions & 2 deletions viz-core/src/handler/map_err.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{async_trait, Error, Handler, Result};
use crate::{Error, Handler, Result};

/// Maps the `Err` value of the output if after the handler called.
#[derive(Debug, Clone)]
Expand All @@ -15,7 +15,7 @@ impl<H, F> MapErr<H, F> {
}
}

#[async_trait]
#[crate::async_trait]
impl<H, F, I, O, E> Handler<I> for MapErr<H, F>
where
I: Send + 'static,
Expand Down
6 changes: 3 additions & 3 deletions viz-core/src/handler/map_into_response.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{async_trait, Handler, IntoResponse, Response, Result};
use crate::{Handler, IntoResponse, Response, Result};

/// Maps the handler's output type to the [`Response`].
#[derive(Debug, Clone)]
Expand All @@ -12,12 +12,12 @@ impl<H> MapInToResponse<H> {
}
}

#[async_trait]
#[crate::async_trait]
impl<H, I, O> Handler<I> for MapInToResponse<H>
where
I: Send + 'static,
H: Handler<I, Output = Result<O>>,
O: IntoResponse + 'static,
O: IntoResponse,
{
type Output = Result<Response>;

Expand Down
6 changes: 3 additions & 3 deletions viz-core/src/handler/or_else.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{async_trait, Error, Handler, Result};
use crate::{Error, Handler, Result};

/// Calls `op` if the output is `Err`, otherwise returns the `Ok` value of the output.
#[derive(Debug, Clone)]
Expand All @@ -15,13 +15,13 @@ impl<H, F> OrElse<H, F> {
}
}

#[async_trait]
#[crate::async_trait]
impl<H, F, I, O> Handler<I> for OrElse<H, F>
where
I: Send + 'static,
H: Handler<I, Output = Result<O>>,
F: Handler<Error, Output = H::Output>,
O: Send + 'static,
O: Send,
{
type Output = F::Output;

Expand Down
6 changes: 2 additions & 4 deletions viz-core/src/handler/service.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use hyper::service::Service;

use crate::{
async_trait, Body, BoxError, Bytes, Error, Handler, HttpBody, Request, Response, Result,
};
use crate::{Body, BoxError, Bytes, Error, Handler, HttpBody, Request, Response, Result};

/// Converts a hyper [`Service`] to a viz [`Handler`].
#[derive(Debug, Clone)]
Expand All @@ -15,7 +13,7 @@ impl<S> ServiceHandler<S> {
}
}

#[async_trait]
#[crate::async_trait]
impl<I, O, S> Handler<Request<I>> for ServiceHandler<S>
where
I: HttpBody + Send + 'static,
Expand Down
2 changes: 1 addition & 1 deletion viz-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub use thiserror::Error as ThisError;

#[doc(hidden)]
mod tuples {
use super::{async_trait, Error, FnExt, FromRequest, Future, IntoResponse, Request, Result};
use super::{Error, FnExt, FromRequest, Future, IntoResponse, Request, Result};

tuple_impls!(A B C D E F G H I J K L);
}
6 changes: 3 additions & 3 deletions viz-core/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ macro_rules! tuple_impls {
};
// "Private" internal implementation
(@impl $( $T:ident )*) => {
#[async_trait]
#[crate::async_trait]
impl<$($T,)*> FromRequest for ($($T,)*)
where
$($T: FromRequest + Send,)*
Expand All @@ -22,8 +22,8 @@ macro_rules! tuple_impls {
}
}

#[async_trait]
impl<$($T,)* Fun, Fut, Out> FnExt<($($T,)*)> for Fun
#[crate::async_trait]
impl<$($T,)* Fun, Fut, Out> FnExt<Request, ($($T,)*)> for Fun
where
$($T: FromRequest + Send,)*
$($T::Error: IntoResponse + Send,)*
Expand Down
Loading

0 comments on commit cb48f4c

Please sign in to comment.