Giter VIP home page Giter VIP logo

include_dir_as_map's Introduction

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

include_dir_as_map's People

Contributors

fboulnois avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

placrosse

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.