Skip to content

Commit 4beb00d

Browse files
committed
Update doc-tests and add small pieces of functionality
1 parent 2cc7612 commit 4beb00d

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

core/lib/src/fs/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub use server::*;
1111
pub use named_file::*;
1212
pub use temp_file::*;
1313
pub use file_name::*;
14+
pub use rewrite::{Rewrite, Rewriter};
1415

1516
crate::export! {
1617
/// Generates a crate-relative version of a path.

core/lib/src/fs/rewrite.rs

+22-4
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,14 @@ impl<'r> File<'r> {
7272
}
7373
}
7474

75-
/// Returns `true` if the file is a dotfile. A dotfile is a file whose name
76-
/// starts with a period (`.`) and is considered hidden.
75+
/// Returns `true` if the file is a dotfile. A dotfile is a file whose
76+
/// name or any directory in it's path start with a period (`.`) and is
77+
/// considered hidden.
78+
///
79+
/// # Windows Note
80+
///
81+
/// This does *not* check the file metadata on any platform, so hidden files
82+
/// on Windows will not be detected.
7783
pub fn is_hidden(&self) -> bool {
7884
self.path.iter().any(|s| s.as_encoded_bytes().starts_with(b"."))
7985
}
@@ -94,7 +100,7 @@ impl<'r> File<'r> {
94100
/// use rocket::fs::rewrite::Prefix;
95101
///
96102
/// FileServer::empty()
97-
/// .filter_file(|f| f.is_visible())
103+
/// .filter(|f, _| f.is_visible())
98104
/// .rewrite(Prefix::checked("static"));
99105
/// ```
100106
pub struct Prefix(PathBuf);
@@ -145,7 +151,7 @@ impl Rewriter for PathBuf {
145151
/// use rocket::fs::rewrite::{Prefix, TrailingDirs};
146152
///
147153
/// FileServer::empty()
148-
/// .filter_file(|f| f.is_visible())
154+
/// .filter(|f, _| f.is_visible())
149155
/// .rewrite(TrailingDirs);
150156
/// ```
151157
pub struct TrailingDirs;
@@ -215,12 +221,24 @@ impl<'r> From<File<'r>> for Rewrite<'r> {
215221
}
216222
}
217223

224+
impl<'r> From<File<'r>> for Option<Rewrite<'r>> {
225+
fn from(value: File<'r>) -> Self {
226+
Some(Rewrite::File(value))
227+
}
228+
}
229+
218230
impl<'r> From<Redirect> for Rewrite<'r> {
219231
fn from(value: Redirect) -> Self {
220232
Self::Redirect(value)
221233
}
222234
}
223235

236+
impl<'r> From<Redirect> for Option<Rewrite<'r>> {
237+
fn from(value: Redirect) -> Self {
238+
Some(Rewrite::Redirect(value))
239+
}
240+
}
241+
224242
impl<F: Send + Sync + 'static> Rewriter for F
225243
where F: for<'r> Fn(Option<Rewrite<'r>>, &Request<'_>) -> Option<Rewrite<'r>>
226244
{

core/lib/src/fs/server.rs

+21-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::fs::rewrite::*;
1515
///
1616
/// This handler makes is simple to serve static files from a directory on the
1717
/// local file system. To use it, construct a `FileServer` using
18-
/// [`FileServer::from()`], then simply `mount` the handler. When mounted, the
18+
/// [`FileServer::new()`], then simply `mount` the handler. When mounted, the
1919
/// handler serves files from the specified directory. If the file is not found,
2020
/// the handler _forwards_ the request. By default, `FileServer` has a rank of
2121
/// `10`. Use [`FileServer::new()`] to create a handler with a custom rank.
@@ -26,7 +26,7 @@ use crate::fs::rewrite::*;
2626
/// the use of [`Rewriter`]s. See [`Rewriter`] for more detailed documentation
2727
/// on how to take full advantage of `FileServer`'s extensibility.
2828
///
29-
/// [`FileServer::from()`] and [`FileServer::new()`] construct a `FileServer`
29+
/// [`FileServer::new()`] construct a `FileServer`
3030
/// with common rewrites: they filter out dotfiles, redirect requests to
3131
/// directories to include a trailing slash, and use `index.html` to respond to
3232
/// requests for a directory. If you want to customize or replace these default
@@ -43,7 +43,7 @@ use crate::fs::rewrite::*;
4343
///
4444
/// #[launch]
4545
/// fn rocket() -> _ {
46-
/// rocket::build().mount("/public", FileServer::from("/static"))
46+
/// rocket::build().mount("/public", FileServer::new("/static"))
4747
/// }
4848
/// ```
4949
///
@@ -65,7 +65,7 @@ use crate::fs::rewrite::*;
6565
///
6666
/// #[launch]
6767
/// fn rocket() -> _ {
68-
/// rocket::build().mount("/", FileServer::from(relative!("static")))
68+
/// rocket::build().mount("/", FileServer::new(relative!("static")))
6969
/// }
7070
/// ```
7171
#[derive(Clone)]
@@ -94,6 +94,20 @@ impl FileServer {
9494
.rewrite(DirIndex::unconditional("index.html"))
9595
}
9696

97+
/// Constructs a new `FileServer` that serves files from the file system
98+
/// `path` with a default rank.
99+
///
100+
/// Adds a set of default rewrites:
101+
/// - [`filter_dotfiles`]: Hides all dotfiles.
102+
/// - [`prefix(path)`](prefix): Applies the root path.
103+
/// - [`normalize_dirs`]: Normalizes directories to have a trailing slash.
104+
pub fn directory<P: AsRef<Path>>(path: P) -> Self {
105+
Self::empty()
106+
.filter(|f, _| f.is_visible())
107+
.rewrite(Prefix::checked(path))
108+
.rewrite(TrailingDirs)
109+
}
110+
97111
/// Constructs a new `FileServer`, with default rank, and no rewrites.
98112
///
99113
/// See [`FileServer::empty_ranked()`].
@@ -126,9 +140,8 @@ impl FileServer {
126140
/// Redirects all requests that have been filtered to the root of the `FileServer`.
127141
///
128142
/// ```rust,no_run
129-
/// # use rocket::{Rocket, Build, Request};
130143
/// # use rocket::fs::{FileServer, Rewrite};
131-
/// # use rocket::{response::Redirect, # uri, Build, Rocket, Request};
144+
/// # use rocket::{response::Redirect, uri, Build, Rocket, Request};
132145
/// fn redir_missing<'r>(p: Option<Rewrite<'r>>, _req: &Request<'_>) -> Option<Rewrite<'r>> {
133146
/// match p {
134147
/// None => Redirect::temporary(uri!("/")).into(),
@@ -138,7 +151,7 @@ impl FileServer {
138151
///
139152
/// # fn launch() -> Rocket<Build> {
140153
/// rocket::build()
141-
/// .mount("/", FileServer::from("static").rewrite(redir_missing))
154+
/// .mount("/", FileServer::new("static").rewrite(redir_missing))
142155
/// # }
143156
/// ```
144157
///
@@ -161,7 +174,7 @@ impl FileServer {
161174
///
162175
/// #[launch]
163176
/// fn rocket() -> _ {
164-
/// let server = FileServer::from("static")
177+
/// let server = FileServer::new("static")
165178
/// .filter(|f, _| f.path.file_name() != Some("hidden".as_ref()));
166179
///
167180
/// rocket::build()

0 commit comments

Comments
 (0)