12 releases

Uses new Rust 2021

0.5.7 Jan 22, 2022
0.5.5 Jan 21, 2022
0.5.3 Dec 5, 2019
0.5.1 Nov 15, 2019
0.1.0 Jan 30, 2019

#853 in Development tools

Download history 27/week @ 2022-08-12 36/week @ 2022-08-19 24/week @ 2022-08-26 24/week @ 2022-09-02 42/week @ 2022-09-09 17/week @ 2022-09-16 16/week @ 2022-09-23 39/week @ 2022-09-30 35/week @ 2022-10-07 8/week @ 2022-10-14 16/week @ 2022-10-21 33/week @ 2022-10-28 44/week @ 2022-11-04 30/week @ 2022-11-11 29/week @ 2022-11-18 12/week @ 2022-11-25

120 downloads per month
Used in 2 crates

MIT license

7KB

Packer

packer is a library that helps you pack static files into binaries using macro magic. 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!

Documentation

Contact

Author: Michael Zhang, Nathan Ringo

License: MIT


lib.rs:

packer is a library that helps you pack static files into binaries using macro magic. Here's how it's done:

Step 1: Include

Include the crate in your Cargo.toml:

[dependencies]
packer = "0.5"

Step 2: Derive

Start deriving 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;

Step 3: Use it!

You can now access any file using the get function:

use packer::Packer;
# #[derive(Packer)]
# #[packer(source = "tests/basic")]
# struct Assets;
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;
# #[derive(Packer)]
# #[packer(source = "tests/basic")]
# struct Assets;
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!

Ignoring Paths

You can choose to ignore certain paths using the ignore option:

# use std::collections::BTreeSet;
# use packer::Packer;
#[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<_>>());

Stripping the Prefix

By default, the path will keep the prefix. For example, if your file is located at static/index.html relative to your project root, then the path that's in the packed version will also be static/index.html. If you would like it to instead be just index.html, configure the prefixed option as follows:

# use std::collections::BTreeSet;
# use packer::Packer;
#[derive(Packer)]
#[packer(source = "tests/basic", prefixed = false)]
struct Assets;

By default, this option is set to true.

Dependencies

~0.7–1.2MB
~25K SLoC