Rocket

A web framework for Rust. https://rocket.rs (mirror)
git clone git://git.thc420.xyz/Rocket
Log | Files | Refs | README

commit 1e611ff86e4f85ee024ac698943f97b6b422ba14
parent dbcb0a75b9556763ac3ab708f40c8f8ed75f1a1e
Author: Sergio Benitez <sb@sergio.bz>
Date:   Fri, 17 May 2019 10:25:55 -0700

Set 'StaticFiles' rank with method, not bit-flags.

Diffstat:
Mcontrib/lib/src/serve.rs | 83+++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------
Mcontrib/lib/tests/static_files.rs | 14+++++---------
2 files changed, 61 insertions(+), 36 deletions(-)

diff --git a/contrib/lib/src/serve.rs b/contrib/lib/src/serve.rs @@ -29,14 +29,12 @@ use rocket::outcome::IntoOutcome; /// * [`Options::None`] - Return only present, visible files. /// * [`Options::DotFiles`] - In addition to visible files, return dotfiles. /// * [`Options::Index`] - Render `index.html` pages for directory requests. -/// * [`Options::Rank(n)`](Options::Rank) - Mount the static files route(s) -/// with rank `n`. /// /// `Options` structures can be `or`d together to select two or more options. /// For instance, to request that both dot files and index pages be returned, /// use `Options::DotFiles | Options::Index`. #[derive(Debug, Clone, Copy)] -pub struct Options(u32); +pub struct Options(u8); #[allow(non_upper_case_globals, non_snake_case)] impl Options { @@ -56,17 +54,6 @@ impl Options { /// directories beginning with `.`. This is _not_ enabled by default. pub const DotFiles: Options = Options(0b0010); - /// `Options` setting a rank of `rank` on the [`StaticFiles`] route(s). By - /// default, the rank is set to `10`. - pub const fn Rank(rank: i16) -> Options { - Options((rank as u32) << 16) - } - - /// Return the rank set in `self`. - fn rank(self) -> isize { - ((self.0 >> 16) as i16) as isize - } - /// Returns `true` if `self` is a superset of `other`. In other words, /// returns `true` if all of the options in `other` are also in `self`. /// @@ -95,7 +82,7 @@ impl Options { impl Default for Options { fn default() -> Self { - Options::Index | Options::Rank(10) + Options::Index } } @@ -113,12 +100,14 @@ impl ::std::ops::BitOr for Options { /// This handler makes it simple to serve static files from a directory on the /// local file system. To use it, construct a `StaticFiles` using either /// [`StaticFiles::from()`] or [`StaticFiles::new()`] then simply `mount` the -/// handler at a desired path. +/// handler at a desired path. When mounted, the handler will generate route(s) +/// that serve the desired static files. /// /// # Options /// /// The handler's functionality can be customized by passing an [`Options`] to -/// [`StaticFiles::new()`]. +/// [`StaticFiles::new()`]. Additionally, the rank of generate routes, which +/// defaults to `10`, can be set via the [`StaticFiles::rank()`] builder method. /// /// # Example /// @@ -169,13 +158,18 @@ impl ::std::ops::BitOr for Options { pub struct StaticFiles { root: PathBuf, options: Options, + rank: isize, } impl StaticFiles { + /// The default rank use by `StaticFiles` routes. + const DEFAULT_RANK: isize = 10; + /// Constructs a new `StaticFiles` that serves files from the file system - /// `path`. By default, [`Options::Index`] and - /// [`Options::Rank(10)`](Options::Rank) are set. To serve static files with - /// other options, use [`StaticFiles::new()`]. + /// `path`. By default, [`Options::Index`] is set, and the generated routes + /// have a rank of `10`. To serve static files with other options, use + /// [`StaticFiles::new()`]. To choose a different rank for generated routes, + /// use [`StaticFiles::rank()`]. /// /// # Example /// @@ -195,12 +189,29 @@ impl StaticFiles { /// # } /// } /// ``` + /// + /// Exactly as before, but set the rank for generated routes to `30`. + /// + /// ```rust + /// # extern crate rocket; + /// # extern crate rocket_contrib; + /// use rocket_contrib::serve::StaticFiles; + /// + /// fn main() { + /// # if false { + /// rocket::ignite() + /// .mount("/static", StaticFiles::from("/www/public").rank(30)) + /// .launch(); + /// # } + /// } + /// ``` pub fn from<P: AsRef<Path>>(path: P) -> Self { StaticFiles::new(path, Options::default()) } /// Constructs a new `StaticFiles` that serves files from the file system - /// `path` with `options` enabled. + /// `path` with `options` enabled. By default, the handler's routes have a + /// rank of `10`. To choose a different rank, use [`StaticFiles::rank()`]. /// /// # Example /// @@ -216,25 +227,43 @@ impl StaticFiles { /// /// fn main() { /// # if false { - /// let options = Options::Index | Options::DotFiles | Options::Rank(-1); + /// let options = Options::Index | Options::DotFiles; /// rocket::ignite() /// .mount("/static", StaticFiles::from("/www/public")) - /// .mount("/pub", StaticFiles::new("/www/public", options)) + /// .mount("/pub", StaticFiles::new("/www/public", options).rank(-1)) /// .launch(); /// # } /// } /// ``` pub fn new<P: AsRef<Path>>(path: P, options: Options) -> Self { - StaticFiles { root: path.as_ref().into(), options } + StaticFiles { root: path.as_ref().into(), options, rank: Self::DEFAULT_RANK } + } + + /// Sets the rank for generated routes to `rank`. + /// + /// # Example + /// + /// ```rust + /// # extern crate rocket_contrib; + /// use rocket_contrib::serve::{StaticFiles, Options}; + /// + /// // A `StaticFiles` created with `from()` with routes of rank `3`. + /// StaticFiles::from("/public").rank(3); + /// + /// // A `StaticFiles` created with `new()` with routes of rank `-15`. + /// StaticFiles::new("/public", Options::Index).rank(-15); + /// ``` + pub fn rank(mut self, rank: isize) -> Self { + self.rank = rank; + self } } impl Into<Vec<Route>> for StaticFiles { fn into(self) -> Vec<Route> { - let rank = self.options.rank(); - let non_index = Route::ranked(rank, Method::Get, "/<path..>", self.clone()); + let non_index = Route::ranked(self.rank, Method::Get, "/<path..>", self.clone()); if self.options.contains(Options::Index) { - let index = Route::ranked(rank, Method::Get, "/", self); + let index = Route::ranked(self.rank, Method::Get, "/", self); vec![index, non_index] } else { vec![non_index] diff --git a/contrib/lib/tests/static_files.rs b/contrib/lib/tests/static_files.rs @@ -109,15 +109,11 @@ mod static_tests { #[test] fn test_ranking() { let root = static_root(); - for rank in (-128..127).chain([-32768, 32767].iter().cloned()) { - let opt_rank = Options::Rank(rank as i16); - let a = StaticFiles::new(&root, opt_rank); - let b = StaticFiles::new(&root, Options::None | opt_rank); - let c = StaticFiles::new(&root, Options::Index | opt_rank); - let d = StaticFiles::new(&root, Options::DotFiles | opt_rank); - let e = StaticFiles::new(&root, Options::Index | Options::DotFiles | opt_rank); - - for handler in vec![a, b, c, d, e] { + for rank in -128..128 { + let a = StaticFiles::new(&root, Options::None).rank(rank); + let b = StaticFiles::from(&root).rank(rank); + + for handler in vec![a, b] { let routes: Vec<Route> = handler.into(); assert!(routes.iter().all(|route| route.rank == rank), "{}", rank); }