Skip to content

Commit

Permalink
chore(tower): improve
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Dec 23, 2023
1 parent 8cf845c commit bb8bd58
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 25 deletions.
25 changes: 25 additions & 0 deletions viz-tower/src/layer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use viz_core::Transform;

use crate::Middleware;

/// Transforms a Tower layer into Viz Middleware.
#[derive(Debug)]
pub struct Layered<L>(L);

impl<L> Layered<L> {
/// Creates a new tower layer.
pub fn new(l: L) -> Self {
Self(l)
}
}

impl<L, H> Transform<H> for Layered<L>
where
L: Clone,
{
type Output = Middleware<L, H>;

fn transform(&self, h: H) -> Self::Output {
Middleware::new(self.0.clone(), h)
}
}
5 changes: 4 additions & 1 deletion viz-tower/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ mod service;
pub use service::HandlerService;

mod middleware;
pub use middleware::{Layered, Middleware};
pub use middleware::Middleware;

mod layer;
pub use layer::Layered;

/// Converts a tower [`Service`] into a [`Handler`].
#[derive(Debug, Clone)]
Expand Down
23 changes: 0 additions & 23 deletions viz-tower/src/middleware.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,10 @@
use tower::{Layer, Service, ServiceExt};
use viz_core::{
async_trait, Body, BoxError, Bytes, Error, Handler, HttpBody, Request, Response, Result,
Transform,
};

use crate::HandlerService;

/// Transforms a Tower layer into Viz Middleware.
#[derive(Debug)]
pub struct Layered<L>(L);

impl<L> Layered<L> {
/// Creates a new tower layer.
pub fn new(l: L) -> Self {
Self(l)
}
}

impl<L, H> Transform<H> for Layered<L>
where
L: Clone,
{
type Output = Middleware<L, H>;

fn transform(&self, h: H) -> Self::Output {
Middleware::new(self.0.clone(), h)
}
}

/// A [`Service`] created from a [`Handler`] by applying a Tower middleware.
#[derive(Debug, Clone)]
pub struct Middleware<L, H> {
Expand Down
2 changes: 1 addition & 1 deletion viz-tower/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ where

impl<H> Service<Request> for HandlerService<H>
where
H: Handler<Request, Output = Result<Response>> + Clone + Send + 'static,
H: Handler<Request, Output = Result<Response>> + Send + Clone + 'static,
{
type Response = Response;
type Error = Error;
Expand Down

0 comments on commit bb8bd58

Please sign in to comment.