packer is a library that helps you pack static files into binaries using macro magic. Here's how it's done:
Include the crate in your
[dependencies] packer = "0.5"
Packer from your structs. You need to provide a
source attribute to indicate
the directory from which it should be pulling. Paths are relative to the crate root.
use packer::Packer; #[derive(Packer)] #[packer(source = "tests/basic")] struct Assets;
You can now access any file using the
use packer::Packer; let data: Option<&'static [u8]> = Assets::get("kermit.jpg");
You may also choose to list all the files that have been stored.
use packer::Packer; let files /*: impl Iterator<Item = &'static str>*/ = Assets::list(); // Result (with no guarantee of order): // files = ["tests/basic/first/kermit.jpg", "tests/basic/second/ignored.x", "tests/basic/second/LICENSE"]
(See the documentation for the Packer trait for the full listing of methods.)
When you build in dev mode, it will fetch off your filesystem as usual, but when you build with
--release, it will pack the assets into your binary!
You can choose to ignore certain paths using the
#[derive(Packer)] #[packer(source = "tests/basic/second", ignore = "*.x")] struct Assets; // using BTreeSet since there's no guarantee of order assert_eq!(Assets::list().into_iter().collect::<BTreeSet<_>>(), vec!["tests/basic/second/LICENSE"].into_iter().collect::<BTreeSet<_>>());
By default, the path will keep the prefix. For example, if your file is located at
relative to your project root, then the path that's in the packed version will also be
If you would like it to instead be just
index.html, configure the
prefixed option as follows:
#[derive(Packer)] #[packer(source = "tests/basic", prefixed = false)] struct Assets;
By default, this option is set to true.