Skip to content

Commit

Permalink
Simplify standard rewrites type
Browse files Browse the repository at this point in the history
- Introduce `FileMap` and `FileFilter` traits
  • Loading branch information
the10thWiz committed Jun 12, 2024
1 parent a43f2af commit 17d886d
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions core/lib/src/fs/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::http::{
Method,
HeaderMap,
Header,
uri::{Segments, Origin},
uri::Segments,
Status,
ext::IntoOwned,
};
Expand Down Expand Up @@ -267,6 +267,22 @@ impl<F> Rewriter for MapFile<F>
}
}

/// 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<F> 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<F> 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
Expand All @@ -286,10 +302,7 @@ impl<F> Rewriter for MapFile<F>
///
/// Panics if `path` does not exist. See [`file_root_permissive`] for a
/// non-panicing variant.
pub fn dir_root(path: impl AsRef<Path>)
-> impl for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>)
-> FileResponse<'p, 'h> + Send + Sync + 'static
{
pub fn dir_root(path: impl AsRef<Path>) -> impl FileMap {
let path = path.as_ref();
if !path.is_dir() {
let path = path.display();
Expand Down Expand Up @@ -319,10 +332,7 @@ pub fn dir_root(path: impl AsRef<Path>)
///
/// Panics if `path` does not exist. See [`file_root_permissive`] for a
/// non-panicing variant.
pub fn file_root(path: impl AsRef<Path>)
-> impl for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>)
-> FileResponse<'p, 'h> + Send + Sync + 'static
{
pub fn file_root(path: impl AsRef<Path>) -> impl FileMap {
let path = path.as_ref();
if !path.exists() {
let path = path.display();
Expand All @@ -348,10 +358,7 @@ pub fn file_root(path: impl AsRef<Path>)
/// .map_file(file_root_permissive("/tmp/rocket"))
/// # }
/// ```
pub fn file_root_permissive(path: impl AsRef<Path>)
-> impl for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>)
-> FileResponse<'p, 'h> + Send + Sync + 'static
{
pub fn file_root_permissive(path: impl AsRef<Path>) -> impl FileMap {
let path = path.as_ref().to_path_buf();
move |f, _r| {
FileResponse::File(f.map_path(|p| path.join(p)))
Expand Down Expand Up @@ -420,9 +427,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 {
Expand Down

0 comments on commit 17d886d

Please sign in to comment.