https://github.com/fboulnois/include_dir_as_map
Embed files from a directory in a rust binary as a hashmap
https://github.com/fboulnois/include_dir_as_map
actix actix-web embed rust rust-lang static-server web
Last synced: 4 months ago
JSON representation
Embed files from a directory in a rust binary as a hashmap
- Host: GitHub
- URL: https://github.com/fboulnois/include_dir_as_map
- Owner: fboulnois
- License: mit
- Created: 2023-09-12T01:28:28.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-11-25T19:51:24.000Z (about 2 years ago)
- Last Synced: 2025-01-12T21:33:28.425Z (about 1 year ago)
- Topics: actix, actix-web, embed, rust, rust-lang, static-server, web
- Language: Rust
- Homepage:
- Size: 19.5 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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`](https://github.com/Michael-F-Bryan/include_dir).
## Usage
Include the following section in `Cargo.toml`:
```toml
[dependencies]
include_dir_as_map="1"
```
In your rust code, include the following:
```rust ignore
// DirMap is an alias for HashMap>
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`:
```toml
[dependencies]
include_dir_as_map={ version="1", features=[ "always-embed" ] }
```
## Examples
See the `examples/` directory for more examples:
* [Hello World](examples/example-hello-world)
* [Actix Web](examples/example-actix-web)
## Development
### Building
To build the library and examples:
```sh
cargo build --workspace
```
### Testing
To test the library and procedural macro:
```sh
cargo test --workspace
```