From 048ce602196fd04418aa21802928c3a4f7027749 Mon Sep 17 00:00:00 2001 From: Matthew Pomes Date: Tue, 11 Jun 2024 18:48:22 -0500 Subject: [PATCH] Simplify standard rewrites type - Introduce `FileMap` and `FileFilter` traits --- core/lib/src/fs/server.rs | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/core/lib/src/fs/server.rs b/core/lib/src/fs/server.rs index da076d3fc0..e2b8ef7a27 100644 --- a/core/lib/src/fs/server.rs +++ b/core/lib/src/fs/server.rs @@ -10,7 +10,7 @@ use crate::http::{ Method, HeaderMap, Header, - uri::{Segments, Origin}, + uri::Segments, Status, ext::IntoOwned, }; @@ -267,6 +267,19 @@ impl Rewriter for MapFile } } +/// Helper trait to simplify standard rewrites +#[doc(hidden)] +pub trait FileMap: for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>) -> FileResponse<'p, 'h> + Send + Sync + 'static {} +impl FileMap for F + where F: for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>) -> FileResponse<'p, 'h> + Send + Sync + 'static +{} +/// Helper trait to simplify standard rewrites +#[doc(hidden)] +pub trait FileFilter: Fn(&File<'_, '_>, &Request<'_>) -> bool + Send + Sync + 'static {} +impl FileFilter for F + where F: Fn(&File<'_, '_>, &Request<'_>) -> bool + Send + Sync + 'static +{} + /// Prepends the provided path, to serve files from a directory. /// /// You can use [`relative!`] to make a path relative to the crate root, rather @@ -286,10 +299,7 @@ impl Rewriter for MapFile /// /// Panics if `path` does not exist. See [`file_root_permissive`] for a /// non-panicing variant. -pub fn dir_root(path: impl AsRef) - -> impl for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>) - -> FileResponse<'p, 'h> + Send + Sync + 'static -{ +pub fn dir_root(path: impl AsRef) -> impl FileMap { let path = path.as_ref(); if !path.is_dir() { let path = path.display(); @@ -319,10 +329,7 @@ pub fn dir_root(path: impl AsRef) /// /// Panics if `path` does not exist. See [`file_root_permissive`] for a /// non-panicing variant. -pub fn file_root(path: impl AsRef) - -> impl for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>) - -> FileResponse<'p, 'h> + Send + Sync + 'static -{ +pub fn file_root(path: impl AsRef) -> impl FileMap { let path = path.as_ref(); if !path.exists() { let path = path.display(); @@ -348,10 +355,7 @@ pub fn file_root(path: impl AsRef) /// .map_file(file_root_permissive("/tmp/rocket")) /// # } /// ``` -pub fn file_root_permissive(path: impl AsRef) - -> impl for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>) - -> FileResponse<'p, 'h> + Send + Sync + 'static -{ +pub fn file_root_permissive(path: impl AsRef) -> impl FileMap { let path = path.as_ref().to_path_buf(); move |f, _r| { FileResponse::File(f.map_path(|p| path.join(p))) @@ -420,9 +424,7 @@ pub fn normalize_dirs<'p, 'h>(file: File<'p, 'h>, req: &Request<'_>) -> FileResp /// .map_file(index("index.html")) /// # } /// ``` -pub fn index(index: &'static str) - -> impl for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>) -> FileResponse<'p, 'h> + Send + Sync -{ +pub fn index(index: &'static str) -> impl FileMap { move |f, _r| if f.path.is_dir() { FileResponse::File(f.map_path(|p| p.join(index))) } else {