Skip to content

Commit

Permalink
Update doc-tests and add small pieces of functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
the10thWiz committed Jun 30, 2024
1 parent 2cc7612 commit 4beb00d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
1 change: 1 addition & 0 deletions core/lib/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub use server::*;
pub use named_file::*;
pub use temp_file::*;
pub use file_name::*;
pub use rewrite::{Rewrite, Rewriter};

crate::export! {
/// Generates a crate-relative version of a path.
Expand Down
26 changes: 22 additions & 4 deletions core/lib/src/fs/rewrite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ impl<'r> File<'r> {
}
}

/// Returns `true` if the file is a dotfile. A dotfile is a file whose name
/// starts with a period (`.`) and is considered hidden.
/// Returns `true` if the file is a dotfile. A dotfile is a file whose
/// name or any directory in it's path start with a period (`.`) and is
/// considered hidden.
///
/// # Windows Note
///
/// This does *not* check the file metadata on any platform, so hidden files
/// on Windows will not be detected.
pub fn is_hidden(&self) -> bool {
self.path.iter().any(|s| s.as_encoded_bytes().starts_with(b"."))
}
Expand All @@ -94,7 +100,7 @@ impl<'r> File<'r> {
/// use rocket::fs::rewrite::Prefix;
///
/// FileServer::empty()
/// .filter_file(|f| f.is_visible())
/// .filter(|f, _| f.is_visible())
/// .rewrite(Prefix::checked("static"));
/// ```
pub struct Prefix(PathBuf);
Expand Down Expand Up @@ -145,7 +151,7 @@ impl Rewriter for PathBuf {
/// use rocket::fs::rewrite::{Prefix, TrailingDirs};
///
/// FileServer::empty()
/// .filter_file(|f| f.is_visible())
/// .filter(|f, _| f.is_visible())
/// .rewrite(TrailingDirs);
/// ```
pub struct TrailingDirs;
Expand Down Expand Up @@ -215,12 +221,24 @@ impl<'r> From<File<'r>> for Rewrite<'r> {
}
}

impl<'r> From<File<'r>> for Option<Rewrite<'r>> {
fn from(value: File<'r>) -> Self {
Some(Rewrite::File(value))
}
}

impl<'r> From<Redirect> for Rewrite<'r> {
fn from(value: Redirect) -> Self {
Self::Redirect(value)
}
}

impl<'r> From<Redirect> for Option<Rewrite<'r>> {
fn from(value: Redirect) -> Self {
Some(Rewrite::Redirect(value))
}
}

impl<F: Send + Sync + 'static> Rewriter for F
where F: for<'r> Fn(Option<Rewrite<'r>>, &Request<'_>) -> Option<Rewrite<'r>>
{
Expand Down
29 changes: 21 additions & 8 deletions core/lib/src/fs/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::fs::rewrite::*;
///
/// This handler makes is simple to serve static files from a directory on the
/// local file system. To use it, construct a `FileServer` using
/// [`FileServer::from()`], then simply `mount` the handler. When mounted, the
/// [`FileServer::new()`], then simply `mount` the handler. When mounted, the
/// handler serves files from the specified directory. If the file is not found,
/// the handler _forwards_ the request. By default, `FileServer` has a rank of
/// `10`. Use [`FileServer::new()`] to create a handler with a custom rank.
Expand All @@ -26,7 +26,7 @@ use crate::fs::rewrite::*;
/// the use of [`Rewriter`]s. See [`Rewriter`] for more detailed documentation
/// on how to take full advantage of `FileServer`'s extensibility.
///
/// [`FileServer::from()`] and [`FileServer::new()`] construct a `FileServer`
/// [`FileServer::new()`] construct a `FileServer`
/// with common rewrites: they filter out dotfiles, redirect requests to
/// directories to include a trailing slash, and use `index.html` to respond to
/// requests for a directory. If you want to customize or replace these default
Expand All @@ -43,7 +43,7 @@ use crate::fs::rewrite::*;
///
/// #[launch]
/// fn rocket() -> _ {
/// rocket::build().mount("/public", FileServer::from("/static"))
/// rocket::build().mount("/public", FileServer::new("/static"))
/// }
/// ```
///
Expand All @@ -65,7 +65,7 @@ use crate::fs::rewrite::*;
///
/// #[launch]
/// fn rocket() -> _ {
/// rocket::build().mount("/", FileServer::from(relative!("static")))
/// rocket::build().mount("/", FileServer::new(relative!("static")))
/// }
/// ```
#[derive(Clone)]
Expand Down Expand Up @@ -94,6 +94,20 @@ impl FileServer {
.rewrite(DirIndex::unconditional("index.html"))
}

/// Constructs a new `FileServer` that serves files from the file system
/// `path` with a default rank.
///
/// Adds a set of default rewrites:
/// - [`filter_dotfiles`]: Hides all dotfiles.
/// - [`prefix(path)`](prefix): Applies the root path.
/// - [`normalize_dirs`]: Normalizes directories to have a trailing slash.
pub fn directory<P: AsRef<Path>>(path: P) -> Self {
Self::empty()
.filter(|f, _| f.is_visible())
.rewrite(Prefix::checked(path))
.rewrite(TrailingDirs)
}

/// Constructs a new `FileServer`, with default rank, and no rewrites.
///
/// See [`FileServer::empty_ranked()`].
Expand Down Expand Up @@ -126,9 +140,8 @@ impl FileServer {
/// Redirects all requests that have been filtered to the root of the `FileServer`.
///
/// ```rust,no_run
/// # use rocket::{Rocket, Build, Request};
/// # use rocket::fs::{FileServer, Rewrite};
/// # use rocket::{response::Redirect, # uri, Build, Rocket, Request};
/// # use rocket::{response::Redirect, uri, Build, Rocket, Request};
/// fn redir_missing<'r>(p: Option<Rewrite<'r>>, _req: &Request<'_>) -> Option<Rewrite<'r>> {
/// match p {
/// None => Redirect::temporary(uri!("/")).into(),
Expand All @@ -138,7 +151,7 @@ impl FileServer {
///
/// # fn launch() -> Rocket<Build> {
/// rocket::build()
/// .mount("/", FileServer::from("static").rewrite(redir_missing))
/// .mount("/", FileServer::new("static").rewrite(redir_missing))
/// # }
/// ```
///
Expand All @@ -161,7 +174,7 @@ impl FileServer {
///
/// #[launch]
/// fn rocket() -> _ {
/// let server = FileServer::from("static")
/// let server = FileServer::new("static")
/// .filter(|f, _| f.path.file_name() != Some("hidden".as_ref()));
///
/// rocket::build()
Expand Down

0 comments on commit 4beb00d

Please sign in to comment.