Skip to content

Commit 17d886d

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

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

core/lib/src/fs/server.rs

+21-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,22 @@ impl<F> Rewriter for MapFile<F>
267267
}
268268
}
269269

270+
/// Helper trait to simplify standard rewrites
271+
#[doc(hidden)]
272+
pub trait FileMap:
273+
for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>) -> FileResponse<'p, 'h> + Send + Sync + 'static
274+
{}
275+
impl<F> FileMap for F
276+
where F: for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>)
277+
-> FileResponse<'p, 'h> + Send + Sync + 'static
278+
{}
279+
/// Helper trait to simplify standard rewrites
280+
#[doc(hidden)]
281+
pub trait FileFilter: Fn(&File<'_, '_>, &Request<'_>) -> bool + Send + Sync + 'static {}
282+
impl<F> FileFilter for F
283+
where F: Fn(&File<'_, '_>, &Request<'_>) -> bool + Send + Sync + 'static
284+
{}
285+
270286
/// Prepends the provided path, to serve files from a directory.
271287
///
272288
/// You can use [`relative!`] to make a path relative to the crate root, rather
@@ -286,10 +302,7 @@ impl<F> Rewriter for MapFile<F>
286302
///
287303
/// Panics if `path` does not exist. See [`file_root_permissive`] for a
288304
/// 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-
{
305+
pub fn dir_root(path: impl AsRef<Path>) -> impl FileMap {
293306
let path = path.as_ref();
294307
if !path.is_dir() {
295308
let path = path.display();
@@ -319,10 +332,7 @@ pub fn dir_root(path: impl AsRef<Path>)
319332
///
320333
/// Panics if `path` does not exist. See [`file_root_permissive`] for a
321334
/// 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-
{
335+
pub fn file_root(path: impl AsRef<Path>) -> impl FileMap {
326336
let path = path.as_ref();
327337
if !path.exists() {
328338
let path = path.display();
@@ -348,10 +358,7 @@ pub fn file_root(path: impl AsRef<Path>)
348358
/// .map_file(file_root_permissive("/tmp/rocket"))
349359
/// # }
350360
/// ```
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-
{
361+
pub fn file_root_permissive(path: impl AsRef<Path>) -> impl FileMap {
355362
let path = path.as_ref().to_path_buf();
356363
move |f, _r| {
357364
FileResponse::File(f.map_path(|p| path.join(p)))
@@ -420,9 +427,7 @@ pub fn normalize_dirs<'p, 'h>(file: File<'p, 'h>, req: &Request<'_>) -> FileResp
420427
/// .map_file(index("index.html"))
421428
/// # }
422429
/// ```
423-
pub fn index(index: &'static str)
424-
-> impl for<'p, 'h> Fn(File<'p, 'h>, &Request<'_>) -> FileResponse<'p, 'h> + Send + Sync
425-
{
430+
pub fn index(index: &'static str) -> impl FileMap {
426431
move |f, _r| if f.path.is_dir() {
427432
FileResponse::File(f.map_path(|p| p.join(index)))
428433
} else {

0 commit comments

Comments
 (0)