rocket_community/fs/
mod.rs

1//! File serving, file accepting, and file metadata types.
2
3mod file_name;
4mod named_file;
5mod server;
6mod temp_file;
7
8pub mod rewrite;
9
10pub use file_name::*;
11pub use named_file::*;
12pub use server::*;
13pub use temp_file::*;
14
15crate::export! {
16    /// Generates a crate-relative version of a path.
17    ///
18    /// This macro is primarily intended for use with [`FileServer`] to serve
19    /// files from a path relative to the crate root.
20    ///
21    /// The macro accepts one parameter, `$path`, an absolute or (preferably)
22    /// relative path. It returns a path as an `&'static str` prefixed with the
23    /// path to the crate root. Use `Path::new(relative!($path))` to retrieve an
24    /// `&'static Path`.
25    ///
26    /// # Example
27    ///
28    /// Serve files from the crate-relative `static/` directory:
29    ///
30    /// ```rust
31    /// # #[macro_use] extern crate rocket_community as rocket;
32    /// use rocket::fs::{FileServer, relative};
33    ///
34    /// #[launch]
35    /// fn rocket() -> _ {
36    ///     rocket::build().mount("/", FileServer::new(relative!("static")))
37    /// }
38    /// ```
39    ///
40    /// Path equivalences:
41    ///
42    /// ```rust
43    /// # extern crate rocket_community as rocket;
44    /// use std::path::Path;
45    ///
46    /// use rocket::fs::relative;
47    ///
48    /// let manual = Path::new(env!("CARGO_MANIFEST_DIR")).join("static");
49    /// let automatic_1 = Path::new(relative!("static"));
50    /// let automatic_2 = Path::new(relative!("/static"));
51    /// assert_eq!(manual, automatic_1);
52    /// assert_eq!(automatic_1, automatic_2);
53    /// ```
54    ///
55    macro_rules! relative {
56        ($path:expr) => {
57            if cfg!(windows) {
58                concat!(env!("CARGO_MANIFEST_DIR"), "\\", $path)
59            } else {
60                concat!(env!("CARGO_MANIFEST_DIR"), "/", $path)
61            }
62        };
63    }
64}