memory_serve/build/
mod.rs

1use std::path::PathBuf;
2
3mod code;
4mod file_asset;
5mod list;
6
7const ASSET_FILE: &str = "memory_serve_assets.rs";
8const QUIET_ENV_NAME: &str = "MEMORY_SERVE_QUIET";
9
10/// Load a directory of assets, keeping an administration of all files
11/// and optionally embedding them into the binary
12pub fn load_directory<P: Into<PathBuf>>(path: P) {
13    // determine whether to dynamically load assets or embed them in the binary
14    let embed = !cfg!(debug_assertions) || cfg!(feature = "force-embed");
15
16    load_directory_with_embed(path, embed);
17}
18
19/// Load a directory of assets, optionally embedding them into the binary
20pub fn load_directory_with_embed<P: Into<PathBuf>>(path: P, embed: bool) {
21    load_names_directories(vec![("default", path)], embed);
22}
23
24/// Load multiple named directories of assets, optionally embedding them into the binary
25pub fn load_names_directories<N, P>(named_paths: impl IntoIterator<Item = (N, P)>, embed: bool)
26where
27    N: Into<String>,
28    P: Into<PathBuf>,
29{
30    let out_dir: PathBuf = std::env::var("OUT_DIR")
31        .expect("OUT_DIR environment variable not set, make sure you call this from a build.rs")
32        .into();
33
34    println!("cargo::rerun-if-env-changed={QUIET_ENV_NAME}");
35    fn log(msg: &str) {
36        if std::env::var(QUIET_ENV_NAME) != Ok("1".to_string()) {
37            println!("cargo:warning={}", msg);
38        }
39    }
40
41    // using a string is faster than using quote ;)
42    let mut code = "&[".to_string();
43
44    for (name, asset_dir) in named_paths {
45        let asset_dir = asset_dir
46            .into()
47            .canonicalize()
48            .expect("Could not canonicalize the provided path");
49        let asset_dir_label = asset_dir.to_string_lossy();
50        let assets = code::assets_to_code(&asset_dir_label, &asset_dir, &out_dir, embed, log);
51
52        println!("cargo::rerun-if-changed={asset_dir_label}");
53
54        code = format!("{code}(\"{}\", {assets}),", name.into());
55    }
56
57    code.push(']');
58
59    let target = out_dir.join(ASSET_FILE);
60
61    std::fs::write(target, code).expect("Unable to write memory-serve asset file.");
62}
63
64#[cfg(test)]
65/// Load assets directly from disk for use in integration tests.
66pub(super) fn load_test_assets<P: Into<PathBuf>>(path: P) -> &'static [crate::Asset] {
67    fn log(msg: &str) {
68        println!("{}", msg);
69    }
70
71    let embed = !cfg!(debug_assertions) || cfg!(feature = "force-embed");
72    let assets = list::list_assets(&path.into(), embed, log);
73
74    let assets = assets
75        .into_iter()
76        .map(|fa| crate::Asset {
77            route: fa.route.leak(),
78            is_compressed: fa.compressed_bytes.is_some(),
79            path: fa.path.to_string_lossy().to_string().leak(),
80            etag: fa.etag.leak(),
81            content_type: fa.content_type.leak(),
82            bytes: fa.compressed_bytes.map(|v| {
83                let s: &'static [u8] = v.leak();
84
85                s
86            }),
87            should_compress: fa.should_compress,
88        })
89        .collect::<Vec<_>>();
90
91    Box::leak(assets.into_boxed_slice())
92}