Skip to content

Commit ae40009

Browse files
committed
Updates based on PR review
- `Rewrite` and `Rewriter` are no longer re-exported from `fs`. - Renamed `FileServer::directory` to `FileServer::new_without_index`. - Added missing documentation in a number of places.
1 parent f736ac8 commit ae40009

File tree

3 files changed

+92
-24
lines changed

3 files changed

+92
-24
lines changed

core/lib/src/fs/mod.rs

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

1615
crate::export! {
1716
/// Generates a crate-relative version of a path.

core/lib/src/fs/rewrite.rs

+20-8
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,21 @@ use crate::response::Redirect;
1515
/// with the file contents, if [`Rewrite::File`] is specified, or a redirect, if
1616
/// [`Rewrite::Redirect`] is specified.
1717
///
18-
/// # Creating a `FileServer`
18+
/// # Creating a [`FileServer`]
1919
///
20-
/// The primary way to create a `FileServer` is via [`FileServer::new()`] which
21-
/// creates a new `FileServer` with a default set of `Rewriter`s: a filter for
20+
/// The primary way to create a [`FileServer`] is via [`FileServer::new()`] which
21+
/// creates a new [`FileServer`] with a default set of `Rewriter`s: a filter for
2222
/// dotfiles, a root path to apply as a prefix, an index file rewriter, and a
2323
/// rewriter to normalize directories to always include a trailing slash.
24+
///
25+
/// [`FileServer`]: super::FileServer
26+
/// [`FileServer::new()`]: super::FileServer::new
2427
pub trait Rewriter: Send + Sync + 'static {
2528
/// Alter the [`Rewrite`] as needed.
2629
fn rewrite<'r>(&self, opt: Option<Rewrite<'r>>, req: &'r Request<'_>) -> Option<Rewrite<'r>>;
2730
}
2831

29-
/// A Response from a [`FileServer`]
32+
/// A Response from a [`FileServer`](super::FileServer)
3033
#[derive(Debug, Clone)]
3134
#[non_exhaustive]
3235
pub enum Rewrite<'r> {
@@ -36,20 +39,27 @@ pub enum Rewrite<'r> {
3639
Redirect(Redirect),
3740
}
3841

39-
/// A File response from a [`FileServer`] and a rewriter.
42+
/// A File response from a [`FileServer`](super::FileServer) and a rewriter.
4043
#[derive(Debug, Clone)]
44+
#[non_exhaustive]
4145
pub struct File<'r> {
42-
/// The path to the file that [`FileServer`] will respond with.
46+
/// The path to the file that [`FileServer`](super::FileServer) will respond with.
4347
pub path: Cow<'r, Path>,
4448
/// A list of headers to be added to the generated response.
4549
pub headers: HeaderMap<'r>,
4650
}
4751

4852
impl<'r> File<'r> {
53+
/// A new `File`, with not additional headers.
4954
pub fn new(path: impl Into<Cow<'r, Path>>) -> Self {
5055
Self { path: path.into(), headers: HeaderMap::new() }
5156
}
5257

58+
/// A new `File`, with not additional headers.
59+
///
60+
/// # Panics
61+
///
62+
/// Panics if the `path` does not exist.
5363
pub fn checked<P: AsRef<Path>>(path: P) -> Self {
5464
let path = path.as_ref();
5565
if !path.exists() {
@@ -79,7 +89,7 @@ impl<'r> File<'r> {
7989
/// # Windows Note
8090
///
8191
/// This does *not* check the file metadata on any platform, so hidden files
82-
/// on Windows will not be detected.
92+
/// on Windows will not be detected by this method.
8393
pub fn is_hidden(&self) -> bool {
8494
self.path.iter().any(|s| s.as_encoded_bytes().starts_with(b"."))
8595
}
@@ -180,7 +190,7 @@ impl Rewriter for TrailingDirs {
180190
/// use rocket::fs::FileServer;
181191
/// use rocket::fs::rewrite::DirIndex;
182192
///
183-
/// FileServer::directory("static")
193+
/// FileServer::new_without_index("static")
184194
/// .rewrite(DirIndex::if_exists("index.htm"))
185195
/// .rewrite(DirIndex::unconditional("index.html"));
186196
/// ```
@@ -190,10 +200,12 @@ pub struct DirIndex {
190200
}
191201

192202
impl DirIndex {
203+
/// Appends `path` to every request for a directory.
193204
pub fn unconditional(path: impl AsRef<Path>) -> Self {
194205
Self { path: path.as_ref().to_path_buf(), check: false }
195206
}
196207

208+
/// Only appends `path` to a request for a directory if the file exists.
197209
pub fn if_exists(path: impl AsRef<Path>) -> Self {
198210
Self { path: path.as_ref().to_path_buf(), check: true }
199211
}

core/lib/src/fs/server.rs

+72-15
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,22 @@ impl FileServer {
8282
/// `path` with a default rank.
8383
///
8484
/// Adds a set of default rewrites:
85-
/// - [`filter_dotfiles`]: Hides all dotfiles.
86-
/// - [`prefix(path)`](prefix): Applies the root path.
87-
/// - [`normalize_dirs`]: Normalizes directories to have a trailing slash.
88-
/// - [`index("index.html")`](index): Appends `index.html` to directory requests.
85+
/// - `|f, _| f.is_visible()`: Hides all dotfiles.
86+
/// - [`Prefix::checked(path)`]: Applies the root path.
87+
/// - [`TrailingDirs`]: Normalizes directories to have a trailing slash.
88+
/// - [`DirIndex::unconditional("index.html")`](DirIndex::unconditional):
89+
/// Appends `index.html` to directory requests.
90+
///
91+
/// If you don't want to serve requests for directories, or want to
92+
/// customize what files are served when a directory is requested, see
93+
/// [`Self::new_without_index`].
94+
///
95+
/// If you need to allow requests for dotfiles, or make any other changes
96+
/// to the default rewrites, see [`Self::empty`].
97+
///
98+
/// [`Prefix::checked(path)`]: crate::fs::rewrite::Prefix::checked
99+
/// [`TrailingDirs`]: crate::fs::rewrite::TrailingDirs
100+
/// [`DirIndex::unconditional`]: crate::fs::DirIndex::unconditional
89101
pub fn new<P: AsRef<Path>>(path: P) -> Self {
90102
Self::empty()
91103
.filter(|f, _| f.is_visible())
@@ -95,22 +107,48 @@ impl FileServer {
95107
}
96108

97109
/// Constructs a new `FileServer` that serves files from the file system
98-
/// `path` with a default rank.
110+
/// `path` with a default rank. This variant does not add a default
111+
/// directory index option.
99112
///
100113
/// 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 {
114+
/// - `|f, _| f.is_visible()`: Hides all dotfiles.
115+
/// - [`Prefix::checked(path)`]: Applies the root path.
116+
/// - [`TrailingDirs`]: Normalizes directories to have a trailing slash.
117+
///
118+
/// In most cases, [`Self::new`] is good enough. However, if you do not want
119+
/// to automatically respond to requests for directories with `index.html`,
120+
/// this method is provided.
121+
///
122+
/// # Example
123+
///
124+
/// Constructs a default file server to server files from `./static`, but
125+
/// uses `index.txt` if `index.html` doesn't exist.
126+
///
127+
/// ```rust,no_run
128+
/// # #[macro_use] extern crate rocket;
129+
/// use rocket::fs::{FileServer, rewrite::DirIndex};
130+
///
131+
/// #[launch]
132+
/// fn rocket() -> _ {
133+
/// let server = FileServer::new("static")
134+
/// .rewrite(DirIndex::if_exists("index.html"))
135+
/// .rewrite(DirIndex::unconditional("index.txt"));
136+
///
137+
/// rocket::build()
138+
/// .mount("/", server)
139+
/// }
140+
/// ```
141+
///
142+
/// [`Prefix::checked(path)`]: crate::fs::rewrite::Prefix::checked
143+
/// [`TrailingDirs`]: crate::fs::rewrite::TrailingDirs
144+
pub fn new_without_index<P: AsRef<Path>>(path: P) -> Self {
105145
Self::empty()
106146
.filter(|f, _| f.is_visible())
107147
.rewrite(Prefix::checked(path))
108148
.rewrite(TrailingDirs)
109149
}
110150

111151
/// Constructs a new `FileServer`, with default rank, and no rewrites.
112-
///
113-
/// See [`FileServer::empty_ranked()`].
114152
pub fn empty() -> Self {
115153
Self {
116154
rewrites: vec![],
@@ -127,7 +165,7 @@ impl FileServer {
127165
/// # fn make_server() -> FileServer {
128166
/// FileServer::empty()
129167
/// .rank(5)
130-
/// }
168+
/// # }
131169
pub fn rank(mut self, rank: isize) -> Self {
132170
self.rank = rank;
133171
self
@@ -140,7 +178,7 @@ impl FileServer {
140178
/// Redirects all requests that have been filtered to the root of the `FileServer`.
141179
///
142180
/// ```rust,no_run
143-
/// # use rocket::fs::{FileServer, Rewrite};
181+
/// # use rocket::fs::{FileServer, rewrite::Rewrite};
144182
/// # use rocket::{response::Redirect, uri, Build, Rocket, Request};
145183
/// fn redir_missing<'r>(p: Option<Rewrite<'r>>, _req: &Request<'_>) -> Option<Rewrite<'r>> {
146184
/// match p {
@@ -169,7 +207,7 @@ impl FileServer {
169207
/// Filter out all paths with a filename of `hidden`.
170208
///
171209
/// ```rust,no_run
172-
/// #[macro_use] extern crate rocket;
210+
/// # #[macro_use] extern crate rocket;
173211
/// use rocket::fs::FileServer;
174212
///
175213
/// #[launch]
@@ -179,7 +217,7 @@ impl FileServer {
179217
///
180218
/// rocket::build()
181219
/// .mount("/", server)
182-
/// # }
220+
/// }
183221
/// ```
184222
pub fn filter<F: Send + Sync + 'static>(self, f: F) -> Self
185223
where F: Fn(&File<'_>, &Request<'_>) -> bool
@@ -200,6 +238,25 @@ impl FileServer {
200238
self.rewrite(Filter(f))
201239
}
202240

241+
/// Change what files this `FileServer` will respond with
242+
///
243+
/// # Example
244+
///
245+
/// Append `index.txt` to every path.
246+
///
247+
/// ```rust,no_run
248+
/// # #[macro_use] extern crate rocket;
249+
/// use rocket::fs::FileServer;
250+
///
251+
/// #[launch]
252+
/// fn rocket() -> _ {
253+
/// let server = FileServer::new("static")
254+
/// .map(|f, _| f.map_path(|p| p.join("index.txt")).into());
255+
///
256+
/// rocket::build()
257+
/// .mount("/", server)
258+
/// }
259+
/// ```
203260
pub fn map<F: Send + Sync + 'static>(self, f: F) -> Self
204261
where F: for<'r> Fn(File<'r>, &Request<'_>) -> Rewrite<'r>
205262
{

0 commit comments

Comments
 (0)