Skip to content

Commit 2cc7612

Browse files
committed
streamline rewriters - use structs for consistency
1 parent 54b80b2 commit 2cc7612

File tree

4 files changed

+301
-356
lines changed

4 files changed

+301
-356
lines changed

core/lib/src/fs/mod.rs

+50-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,53 @@ pub use server::*;
1111
pub use named_file::*;
1212
pub use temp_file::*;
1313
pub use file_name::*;
14-
pub use server::relative;
14+
15+
crate::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;
32+
/// use rocket::fs::{FileServer, relative};
33+
///
34+
/// #[launch]
35+
/// fn rocket() -> _ {
36+
/// rocket::build().mount("/", FileServer::from(relative!("static")))
37+
/// }
38+
/// ```
39+
///
40+
/// Path equivalences:
41+
///
42+
/// ```rust
43+
/// use std::path::Path;
44+
///
45+
/// use rocket::fs::relative;
46+
///
47+
/// let manual = Path::new(env!("CARGO_MANIFEST_DIR")).join("static");
48+
/// let automatic_1 = Path::new(relative!("static"));
49+
/// let automatic_2 = Path::new(relative!("/static"));
50+
/// assert_eq!(manual, automatic_1);
51+
/// assert_eq!(automatic_1, automatic_2);
52+
/// ```
53+
///
54+
macro_rules! relative {
55+
($path:expr) => {
56+
if cfg!(windows) {
57+
concat!(env!("CARGO_MANIFEST_DIR"), "\\", $path)
58+
} else {
59+
concat!(env!("CARGO_MANIFEST_DIR"), "/", $path)
60+
}
61+
};
62+
}
63+
}

0 commit comments

Comments
 (0)