@@ -10,7 +10,7 @@ use crate::http::{
10
10
Method ,
11
11
HeaderMap ,
12
12
Header ,
13
- uri:: { Segments , Origin } ,
13
+ uri:: Segments ,
14
14
Status ,
15
15
ext:: IntoOwned ,
16
16
} ;
@@ -267,6 +267,22 @@ impl<F> Rewriter for MapFile<F>
267
267
}
268
268
}
269
269
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
+
270
286
/// Prepends the provided path, to serve files from a directory.
271
287
///
272
288
/// You can use [`relative!`] to make a path relative to the crate root, rather
@@ -286,10 +302,7 @@ impl<F> Rewriter for MapFile<F>
286
302
///
287
303
/// Panics if `path` does not exist. See [`file_root_permissive`] for a
288
304
/// 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 {
293
306
let path = path. as_ref ( ) ;
294
307
if !path. is_dir ( ) {
295
308
let path = path. display ( ) ;
@@ -319,10 +332,7 @@ pub fn dir_root(path: impl AsRef<Path>)
319
332
///
320
333
/// Panics if `path` does not exist. See [`file_root_permissive`] for a
321
334
/// 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 {
326
336
let path = path. as_ref ( ) ;
327
337
if !path. exists ( ) {
328
338
let path = path. display ( ) ;
@@ -348,10 +358,7 @@ pub fn file_root(path: impl AsRef<Path>)
348
358
/// .map_file(file_root_permissive("/tmp/rocket"))
349
359
/// # }
350
360
/// ```
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 {
355
362
let path = path. as_ref ( ) . to_path_buf ( ) ;
356
363
move |f, _r| {
357
364
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
420
427
/// .map_file(index("index.html"))
421
428
/// # }
422
429
/// ```
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 {
426
431
move |f, _r| if f. path . is_dir ( ) {
427
432
FileResponse :: File ( f. map_path ( |p| p. join ( index) ) )
428
433
} else {
0 commit comments