Crate pagetop_build
source ·Expand description
This function uses the static_files library to embed at compile time a bundle of static files in your binary.
Just create folder with static resources in your project (for example static
):
cd project_dir
mkdir static
echo "Hello, world!" > static/hello
Add to Cargo.toml
the required dependencies:
[build-dependencies]
pagetop-build = { ... }
Add build.rs
with call to bundle resources (guides will be the magic word in this example):
use pagetop_build::StaticFilesBundle;
fn main() -> std::io::Result<()> {
StaticFilesBundle::from_dir("./static")
.with_name("guides")
.build()
}
Optionally, you can pass a function to filter those files into the ./static
folder which
should be excluded in the resources bundle:
use pagetop_build::StaticFilesBundle;
fn main() -> std::io::Result<()> {
StaticFilesBundle::from_dir("./static")
.with_name("guides")
.with_filter(except_css_dir)
.build()
}
fn except_css_dir(p: &Path) -> bool {
if let Some(parent) = p.parent() {
!matches!(parent.to_str(), Some("/css"))
}
true
}
This will create a file called guides.rs
in the standard directory
OUT_DIR where all
intermediate and output artifacts are placed during compilation.
You don’t need to access this file, just include it in your project using the builder name as an identifier:
use pagetop::prelude::*;
static_files!(guides);
Also you can get the bundle as a static reference to the generated HashMap
resources
collection:
use pagetop::prelude::*;
static_files!(guides => BUNDLE_GUIDES);
You can build more than one resources file to compile with your project.