mirror of https://github.com/rwf2/Rocket.git
Set 'StaticFiles' rank with method, not bit-flags.
This commit is contained in:
parent
dbcb0a75b9
commit
1e611ff86e
|
@ -29,14 +29,12 @@ use rocket::outcome::IntoOutcome;
|
||||||
/// * [`Options::None`] - Return only present, visible files.
|
/// * [`Options::None`] - Return only present, visible files.
|
||||||
/// * [`Options::DotFiles`] - In addition to visible files, return dotfiles.
|
/// * [`Options::DotFiles`] - In addition to visible files, return dotfiles.
|
||||||
/// * [`Options::Index`] - Render `index.html` pages for directory requests.
|
/// * [`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.
|
/// `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,
|
/// For instance, to request that both dot files and index pages be returned,
|
||||||
/// use `Options::DotFiles | Options::Index`.
|
/// use `Options::DotFiles | Options::Index`.
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct Options(u32);
|
pub struct Options(u8);
|
||||||
|
|
||||||
#[allow(non_upper_case_globals, non_snake_case)]
|
#[allow(non_upper_case_globals, non_snake_case)]
|
||||||
impl Options {
|
impl Options {
|
||||||
|
@ -56,17 +54,6 @@ impl Options {
|
||||||
/// directories beginning with `.`. This is _not_ enabled by default.
|
/// directories beginning with `.`. This is _not_ enabled by default.
|
||||||
pub const DotFiles: Options = Options(0b0010);
|
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 `self` is a superset of `other`. In other words,
|
||||||
/// returns `true` if all of the options in `other` are also in `self`.
|
/// returns `true` if all of the options in `other` are also in `self`.
|
||||||
///
|
///
|
||||||
|
@ -95,7 +82,7 @@ impl Options {
|
||||||
|
|
||||||
impl Default for Options {
|
impl Default for Options {
|
||||||
fn default() -> Self {
|
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
|
/// 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
|
/// local file system. To use it, construct a `StaticFiles` using either
|
||||||
/// [`StaticFiles::from()`] or [`StaticFiles::new()`] then simply `mount` the
|
/// [`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
|
/// # Options
|
||||||
///
|
///
|
||||||
/// The handler's functionality can be customized by passing an [`Options`] to
|
/// 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
|
/// # Example
|
||||||
///
|
///
|
||||||
|
@ -169,13 +158,18 @@ impl ::std::ops::BitOr for Options {
|
||||||
pub struct StaticFiles {
|
pub struct StaticFiles {
|
||||||
root: PathBuf,
|
root: PathBuf,
|
||||||
options: Options,
|
options: Options,
|
||||||
|
rank: isize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StaticFiles {
|
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
|
/// Constructs a new `StaticFiles` that serves files from the file system
|
||||||
/// `path`. By default, [`Options::Index`] and
|
/// `path`. By default, [`Options::Index`] is set, and the generated routes
|
||||||
/// [`Options::Rank(10)`](Options::Rank) are set. To serve static files with
|
/// have a rank of `10`. To serve static files with other options, use
|
||||||
/// other options, use [`StaticFiles::new()`].
|
/// [`StaticFiles::new()`]. To choose a different rank for generated routes,
|
||||||
|
/// use [`StaticFiles::rank()`].
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # 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 {
|
pub fn from<P: AsRef<Path>>(path: P) -> Self {
|
||||||
StaticFiles::new(path, Options::default())
|
StaticFiles::new(path, Options::default())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Constructs a new `StaticFiles` that serves files from the file system
|
/// 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
|
/// # Example
|
||||||
///
|
///
|
||||||
|
@ -216,25 +227,43 @@ impl StaticFiles {
|
||||||
///
|
///
|
||||||
/// fn main() {
|
/// fn main() {
|
||||||
/// # if false {
|
/// # if false {
|
||||||
/// let options = Options::Index | Options::DotFiles | Options::Rank(-1);
|
/// let options = Options::Index | Options::DotFiles;
|
||||||
/// rocket::ignite()
|
/// rocket::ignite()
|
||||||
/// .mount("/static", StaticFiles::from("/www/public"))
|
/// .mount("/static", StaticFiles::from("/www/public"))
|
||||||
/// .mount("/pub", StaticFiles::new("/www/public", options))
|
/// .mount("/pub", StaticFiles::new("/www/public", options).rank(-1))
|
||||||
/// .launch();
|
/// .launch();
|
||||||
/// # }
|
/// # }
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn new<P: AsRef<Path>>(path: P, options: Options) -> Self {
|
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 {
|
impl Into<Vec<Route>> for StaticFiles {
|
||||||
fn into(self) -> Vec<Route> {
|
fn into(self) -> Vec<Route> {
|
||||||
let rank = self.options.rank();
|
let non_index = Route::ranked(self.rank, Method::Get, "/<path..>", self.clone());
|
||||||
let non_index = Route::ranked(rank, Method::Get, "/<path..>", self.clone());
|
|
||||||
if self.options.contains(Options::Index) {
|
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]
|
vec![index, non_index]
|
||||||
} else {
|
} else {
|
||||||
vec![non_index]
|
vec![non_index]
|
||||||
|
|
|
@ -109,15 +109,11 @@ mod static_tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_ranking() {
|
fn test_ranking() {
|
||||||
let root = static_root();
|
let root = static_root();
|
||||||
for rank in (-128..127).chain([-32768, 32767].iter().cloned()) {
|
for rank in -128..128 {
|
||||||
let opt_rank = Options::Rank(rank as i16);
|
let a = StaticFiles::new(&root, Options::None).rank(rank);
|
||||||
let a = StaticFiles::new(&root, opt_rank);
|
let b = StaticFiles::from(&root).rank(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 handler in vec![a, b] {
|
||||||
let routes: Vec<Route> = handler.into();
|
let routes: Vec<Route> = handler.into();
|
||||||
assert!(routes.iter().all(|route| route.rank == rank), "{}", rank);
|
assert!(routes.iter().all(|route| route.rank == rank), "{}", rank);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue