include_dir_as_map 1.1.0

Embed files from a directory as a hashmap in the rust binary
Documentation
  • Coverage
  • 100%
    2 out of 2 items documented0 out of 0 items with examples
  • Size
  • Source code size: 6.62 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.7 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 12s Average build duration of successful builds.
  • all releases: 12s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • fboulnois/include_dir_as_map
    2 1 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • fboulnois

include_dir_as_map

A procedural macro which embeds files from a directory into the rust binary as a hashmap. This can be used to embed assets such as images, html, css, and js.

include_dir_as_map extends include_str!() and include_bytes!() and is similar to include_dir.

Usage

Include the following section in Cargo.toml:

[dependencies]
include_dir_as_map="1"

In your rust code, include the following:

// DirMap is an alias for HashMap<String, Vec<u8>>
use include_dir_as_map::{include_dir_as_map, DirMap};

// Environment variables will be expanded at compile time
let dirmap: DirMap = include_dir_as_map!("$CARGO_MANIFEST_DIR");
let bytes = dirmap.get("Cargo.toml")?;

All paths are relative to the embedded directory, so if root contains files root/foo.txt and root/next/bar.txt, then include_dir_as_map!("root") will result in a hashmap with keys foo.txt and next/bar.txt.

Features

By default, the files are read from the filesystem at runtime in debug mode for compilation speed. To override this behavior, enable the always-embed feature in Cargo.toml:

[dependencies]
include_dir_as_map={ version="1", features=[ "always-embed" ] }

Examples

See the examples/ directory for more examples:

Development

Building

To build the library and examples:

cargo build --workspace

Testing

To test the library and procedural macro:

cargo test --workspace