Giter Site home page Giter Site logo

maghoff / include-dir-macro Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jcdyer/include-dir-macro

0.0 2.0 0.0 18 KB

A rust trait to include the source of all files in a given subdirectory in the generated binary, and make them available by path name through a hashmap.

Rust 100.00%

include-dir-macro's Introduction

include-dir-macro

Quick start:

Using a nightly toolchain, add this to your Cargo.toml:

[dependencies]
include-dir-macro = "0.1"

Then at the root of your crate (in main.rs or lib.rs), add the following:

#[feature(proc_macro)]
extern crate include-dir-macro;

Finally, you can call the macro as:

include_dir("path/to/directory");

If the path is a relative path, it will be interpreted relative to the directory from which cargo or rustc was invoked

Description

Provides an include_dir!() macro, which returns a HashMap<PathBuf, &'static [u8]> mapping files within a given directory to the contents of the file, stored within the built executable as static byte arrays. Given a crate with a directory structure like this:

/root
+-Cargo.toml
+-src/
| \-main.rs
|   
|       #[feature(proc_macro)]
|       extern crate include_dir_macro;
|       fn main() { 
|           let stat = include_dir!("static");
|       }
|   
\-static/
  +-this
  |
  |     ABC
  |
  +-that.html
  |
  |     <p>123</p>
  |
  \-path/
    \-to/
      \-theother.txt

            Do 
            re
            mi.

The value of stat will be the same as if main.rs included:

use std::collections::HashMap;
use std::path::PathBuf

fn main() {
    let stat = HashMap::new()
    stat.insert(PathBuf::from("this"), b"ABC\n");
    stat.insert(PathBuf::from("that.html"), b"<p>123</p>\n");
    stat.insert(PathBuf::from("path/to/theother.txt"), b"Do\nre\nmi.\n");
}

Rationale

Out of the box, Rust provides a macro called include_bytes!(), which loads the contents of a file into a static byte array at compile time. The contents then live in the binary, requiring no further access to the filesystem. I thought this could be useful for bundling up entire directories, to provide a way to make, for example, whole websites bundled as a static binary, containing all associated images, stylesheets, and scripts.

One shortcoming of of include_bytes!() for this use case is that it only operates on paths specified as str literals, so to use it for an entire directory, one would have to add each file by hand. I saw two ways around this:

One is to include code-generation in a crate's build.rs script that crawls the included directory, and adds an include_str for each file. This method is supported by another crate called "include_dir", but I wanted an approach with the ergonomic simplicity of the include_bytes!() macro. This required using procedural macros, which in turn means that it currently (as of Rust 1.21) rustonly works on nightly releases.

include-dir-macro's People

Contributors

jcdyer avatar maghoff avatar

Watchers

James Cloos avatar  avatar

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.