Skip to content

Commit

Permalink
Move hoop functions to Handler trait
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislearn committed Aug 23, 2024
1 parent 1fc0cf7 commit 78b4aa1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 49 deletions.
25 changes: 25 additions & 0 deletions crates/core/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,31 @@ pub trait Handler: Send + Sync + 'static {
/// Handle http request.
#[must_use = "handle future must be used"]
async fn handle(&self, req: &mut Request, depot: &mut Depot, res: &mut Response, ctrl: &mut FlowCtrl);

/// Wrap to `HoopedHandler`.
#[inline]
fn hooped<H: Handler>(self) -> HoopedHandler where Self: Sized {
HoopedHandler::new(self)
}

/// Hoop this handler with middleware.
#[inline]
fn hoop<H: Handler>(self, hoop: H) -> HoopedHandler where Self: Sized {
HoopedHandler::new(self).hoop(hoop)
}

/// Hoop this handler with middleware.
///
/// This middleware only effective when the filter return true.
#[inline]
fn hoop_when<H, F>(self, hoop: H, filter: F) -> HoopedHandler
where
Self: Sized,
H: Handler,
F: Fn(&Request, &Depot) -> bool + Send + Sync + 'static,
{
HoopedHandler::new(self).hoop_when(hoop, filter)
}
}

#[doc(hidden)]
Expand Down
25 changes: 1 addition & 24 deletions crates/serve-static/src/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::str::FromStr;
use std::time::SystemTime;

use salvo_core::fs::NamedFile;
use salvo_core::handler::{Handler, HoopedHandler};
use salvo_core::handler::{Handler, };
use salvo_core::http::header::ACCEPT_ENCODING;
use salvo_core::http::{self, HeaderValue, Request, Response, StatusCode, StatusError};
use salvo_core::writing::Text;
Expand Down Expand Up @@ -242,29 +242,6 @@ impl StaticDir {
false
}

/// Wrap to `HoopedHandler`.
#[inline]
pub fn hooped<H: Handler>(self) -> HoopedHandler {
HoopedHandler::new(self)
}

/// Add a handler as middleware, it will run the handler when error catched.
#[inline]
pub fn hoop<H: Handler>(self, hoop: H) -> HoopedHandler {
HoopedHandler::new(self).hoop(hoop)
}

/// Add a handler as middleware, it will run the handler when error catched.
///
/// This middleware only effective when the filter return true.
#[inline]
pub fn hoop_when<H, F>(self, hoop: H, filter: F) -> HoopedHandler
where
H: Handler,
F: Fn(&Request, &Depot) -> bool + Send + Sync + 'static,
{
HoopedHandler::new(self).hoop_when(hoop, filter)
}
}
#[derive(Serialize, Deserialize, Debug)]
struct CurrentInfo {
Expand Down
26 changes: 1 addition & 25 deletions crates/serve-static/src/embed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rust_embed::{EmbeddedFile, Metadata, RustEmbed};
use salvo_core::http::header::{CONTENT_TYPE, ETAG, IF_NONE_MATCH};
use salvo_core::http::{HeaderValue, Mime, Request, Response, StatusCode};
use salvo_core::{async_trait, Depot, FlowCtrl, IntoVecString};
use salvo_core::handler::{HoopedHandler, Handler};
use salvo_core::handler::{ Handler};

use super::{decode_url_path_safely, format_url_path_safely, join_path, redirect_to_dir_url};

Expand Down Expand Up @@ -108,30 +108,6 @@ where
self.fallback = Some(fallback.into());
self
}

/// Wrap to `HoopedHandler`.
#[inline]
pub fn hooped<H: Handler>(self) -> HoopedHandler {
HoopedHandler::new(self)
}

/// Add a handler as middleware, it will run the handler when error catched.
#[inline]
pub fn hoop<H: Handler>(self, hoop: H) -> HoopedHandler {
HoopedHandler::new(self).hoop(hoop)
}

/// Add a handler as middleware, it will run the handler when error catched.
///
/// This middleware only effective when the filter return true.
#[inline]
pub fn hoop_when<H, F>(self, hoop: H, filter: F) -> HoopedHandler
where
H: Handler,
F: Fn(&Request, &Depot) -> bool + Send + Sync + 'static,
{
HoopedHandler::new(self).hoop_when(hoop, filter)
}
}
#[async_trait]
impl<T> Handler for StaticEmbed<T>
Expand Down

0 comments on commit 78b4aa1

Please sign in to comment.