From a43f2af14219ee5a42b646f7454eec9672bfb3d1 Mon Sep 17 00:00:00 2001 From: Matthew Pomes Date: Mon, 10 Jun 2024 21:32:23 -0500 Subject: [PATCH] Fix line length --- core/lib/src/fs/server.rs | 47 ++++++++++++++++++++++++----------- core/lib/tests/file_server.rs | 11 +++++--- 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/core/lib/src/fs/server.rs b/core/lib/src/fs/server.rs index c7a717eca6..da076d3fc0 100644 --- a/core/lib/src/fs/server.rs +++ b/core/lib/src/fs/server.rs @@ -117,7 +117,8 @@ impl fmt::Debug for DebugListRewrite<'_> { /// - [`FileServer::map_file()`] pub trait Rewriter: Send + Sync + 'static { /// Alter the [`FileResponse`] as needed. - fn rewrite<'p, 'h>(&self, path: Option>, req: &Request<'_>) -> Option>; + fn rewrite<'p, 'h>(&self, path: Option>, req: &Request<'_>) + -> Option>; } /// A Response from a [`FileServer`] @@ -193,7 +194,9 @@ impl<'p, 'h> File<'p, 'h> { // FileResponse::Redirect(Redirect::permanent(f(self.full_uri.clone().into_owned()))) // } - async fn respond_to<'r>(self, req: &'r Request<'_>, data: Data<'r>) -> Outcome<'r> where 'h: 'r { + async fn respond_to<'r>(self, req: &'r Request<'_>, data: Data<'r>) -> Outcome<'r> + where 'h: 'r + { /// Normalize paths to enable `file_root` to work properly fn strip_trailing_slash(p: &Path) -> &Path { let bytes = p.as_os_str().as_encoded_bytes(); @@ -223,17 +226,24 @@ impl<'p, 'h> File<'p, 'h> { } impl Rewriter for F - where F: for<'r, 'h> Fn(Option>, &Request<'_>) -> Option> + where F: for<'r, 'h> Fn(Option>, &Request<'_>) + -> Option> { - fn rewrite<'p, 'h>(&self, path: Option>, req: &Request<'_>) -> Option> { + fn rewrite<'p, 'h>(&self, path: Option>, req: &Request<'_>) + -> Option> + { self(path, req) } } /// Helper to implement [`FileServer::filter_file()`] struct FilterFile(F); -impl, &Request<'_>) -> bool + Send + Sync + 'static> Rewriter for FilterFile { - fn rewrite<'p, 'h>(&self, path: Option>, req: &Request<'_>) -> Option> { +impl Rewriter for FilterFile + where F: Fn(&File<'_, '_>, &Request<'_>) -> bool + Send + Sync + 'static +{ + fn rewrite<'p, 'h>(&self, path: Option>, req: &Request<'_>) + -> Option> + { match path { Some(FileResponse::File(file)) if !self.0(&file, req) => None, path => path, @@ -244,9 +254,12 @@ impl, &Request<'_>) -> bool + Send + Sync + 'static> Rewrite /// Helper to implement [`FileServer::map_file()`] struct MapFile(F); impl Rewriter for MapFile - where F: for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>) -> FileResponse<'p, 'h> + Send + Sync + 'static, + where F: for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>) + -> FileResponse<'p, 'h> + Send + Sync + 'static, { - fn rewrite<'p, 'h>(&self, path: Option>, req: &Request<'_>) -> Option> { + fn rewrite<'p, 'h>(&self, path: Option>, req: &Request<'_>) + -> Option> + { match path { Some(FileResponse::File(file)) => Some(self.0(file, req)), path => path, @@ -274,7 +287,8 @@ 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 + -> impl for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>) + -> FileResponse<'p, 'h> + Send + Sync + 'static { let path = path.as_ref(); if !path.is_dir() { @@ -306,7 +320,8 @@ 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 + -> impl for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>) + -> FileResponse<'p, 'h> + Send + Sync + 'static { let path = path.as_ref(); if !path.exists() { @@ -334,7 +349,8 @@ pub fn file_root(path: impl AsRef) /// # } /// ``` pub fn file_root_permissive(path: impl AsRef) - -> impl for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>) -> FileResponse<'p, 'h> + Send + Sync + 'static + -> impl for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>) + -> FileResponse<'p, 'h> + Send + Sync + 'static { let path = path.as_ref().to_path_buf(); move |f, _r| { @@ -477,7 +493,8 @@ impl FileServer { /// /// Redirects all requests that have been filtered to the root of the `FileServer`. /// ```rust,no_run - /// # use rocket::{fs::{FileServer, FileResponse}, response::Redirect, uri, Build, Rocket, Request}; + /// # use rocket::{fs::{FileServer, FileResponse}, response::Redirect, + /// # uri, Build, Rocket, Request}; /// fn redir_missing<'p, 'h>(p: Option>, _req: &Request<'_>) /// -> Option> /// { @@ -533,12 +550,14 @@ impl FileServer { /// rocket::build() /// .mount( /// "/", - /// FileServer::from("static").map_file(|f, _r| f.map_path(|p| p.join("hidden")).into()) + /// FileServer::from("static") + /// .map_file(|f, _r| f.map_path(|p| p.join("hidden")).into()) /// ) /// # } /// ``` pub fn map_file(self, f: F) -> Self - where F: for<'r, 'h> Fn(File<'r, 'h>, &Request<'_>) -> FileResponse<'r, 'h> + Send + Sync + 'static + where F: for<'r, 'h> Fn(File<'r, 'h>, &Request<'_>) + -> FileResponse<'r, 'h> + Send + Sync + 'static { self.and_rewrite(MapFile(f)) } diff --git a/core/lib/tests/file_server.rs b/core/lib/tests/file_server.rs index 56f4ee6c40..1802d48fa4 100644 --- a/core/lib/tests/file_server.rs +++ b/core/lib/tests/file_server.rs @@ -5,7 +5,14 @@ use rocket::{Rocket, Route, Build}; use rocket::http::Status; use rocket::local::blocking::Client; use rocket::fs::{ - dir_root, file_root, filter_dotfiles, index, file_root_permissive, normalize_dirs, relative, FileServer + dir_root, + file_root, + filter_dotfiles, + index, + file_root_permissive, + normalize_dirs, + relative, + FileServer }; fn static_root() -> &'static Path { @@ -60,14 +67,12 @@ fn rocket() -> Rocket { FileServer::empty() .filter_file(filter_dotfiles) .map_file(file_root(root.join("other/hello.txt"))) - ) .mount( "/missing_root", FileServer::empty() .filter_file(filter_dotfiles) .map_file(file_root_permissive(root.join("no_file"))) - ) }