Skip to content

Commit 048ce60

Browse files
committed
Simplify standard rewrites type
- Introduce `FileMap` and `FileFilter` traits
1 parent a43f2af commit 048ce60

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

core/lib/src/fs/server.rs

+18-16
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::http::{
1010
Method,
1111
HeaderMap,
1212
Header,
13-
uri::{Segments, Origin},
13+
uri::Segments,
1414
Status,
1515
ext::IntoOwned,
1616
};
@@ -267,6 +267,19 @@ impl<F> Rewriter for MapFile<F>
267267
}
268268
}
269269

270+
/// Helper trait to simplify standard rewrites
271+
#[doc(hidden)]
272+
pub trait FileMap: for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>) -> FileResponse<'p, 'h> + Send + Sync + 'static {}
273+
impl<F> FileMap for F
274+
where F: for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>) -> FileResponse<'p, 'h> + Send + Sync + 'static
275+
{}
276+
/// Helper trait to simplify standard rewrites
277+
#[doc(hidden)]
278+
pub trait FileFilter: Fn(&File<'_, '_>, &Request<'_>) -> bool + Send + Sync + 'static {}
279+
impl<F> FileFilter for F
280+
where F: Fn(&File<'_, '_>, &Request<'_>) -> bool + Send + Sync + 'static
281+
{}
282+
270283
/// Prepends the provided path, to serve files from a directory.
271284
///
272285
/// You can use [`relative!`] to make a path relative to the crate root, rather
@@ -286,10 +299,7 @@ impl<F> Rewriter for MapFile<F>
286299
///
287300
/// Panics if `path` does not exist. See [`file_root_permissive`] for a
288301
/// non-panicing variant.
289-
pub fn dir_root(path: impl AsRef<Path>)
290-
-> impl for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>)
291-
-> FileResponse<'p, 'h> + Send + Sync + 'static
292-
{
302+
pub fn dir_root(path: impl AsRef<Path>) -> impl FileMap {
293303
let path = path.as_ref();
294304
if !path.is_dir() {
295305
let path = path.display();
@@ -319,10 +329,7 @@ pub fn dir_root(path: impl AsRef<Path>)
319329
///
320330
/// Panics if `path` does not exist. See [`file_root_permissive`] for a
321331
/// non-panicing variant.
322-
pub fn file_root(path: impl AsRef<Path>)
323-
-> impl for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>)
324-
-> FileResponse<'p, 'h> + Send + Sync + 'static
325-
{
332+
pub fn file_root(path: impl AsRef<Path>) -> impl FileMap {
326333
let path = path.as_ref();
327334
if !path.exists() {
328335
let path = path.display();
@@ -348,10 +355,7 @@ pub fn file_root(path: impl AsRef<Path>)
348355
/// .map_file(file_root_permissive("/tmp/rocket"))
349356
/// # }
350357
/// ```
351-
pub fn file_root_permissive(path: impl AsRef<Path>)
352-
-> impl for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>)
353-
-> FileResponse<'p, 'h> + Send + Sync + 'static
354-
{
358+
pub fn file_root_permissive(path: impl AsRef<Path>) -> impl FileMap {
355359
let path = path.as_ref().to_path_buf();
356360
move |f, _r| {
357361
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
420424
/// .map_file(index("index.html"))
421425
/// # }
422426
/// ```
423-
pub fn index(index: &'static str)
424-
-> impl for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>) -> FileResponse<'p, 'h> + Send + Sync
425-
{
427+
pub fn index(index: &'static str) -> impl FileMap {
426428
move |f, _r| if f.path.is_dir() {
427429
FileResponse::File(f.map_path(|p| p.join(index)))
428430
} else {

0 commit comments

Comments
 (0)