#embed #http #include #dir #folder

app embed_dir

Small program to generate code to embed a folder in a rust program

3 unstable releases

Uses old Rust 2015

0.2.0 Aug 7, 2017
0.1.1 Aug 3, 2017
0.1.0 Aug 3, 2017

#1695 in Filesystem

46 downloads per month

GPL-3.0 license

16KB
156 lines

Embed-dir for Rust

crate.io package... https://crates.io/crates/embed_dir/0.1.0

You have the great macro include_bytes! in Rust

But sometimes is not enough. You could need to embed several files, generally in a folder.

In order to automate it, this simple and small tool generates code for you.

[NOTE] Perhaps in the future this tool could be integrated on compilation process running build

Running the tool

If you run the program without params or incorrect params number

    Simple program to generate code to embed files recursivily from a chosen folder.

    It will generate a source file with code to embed the files on folder.

    More info:  https://github.com/jleahred/embed_dir

    Usage:
        embed_dir  <origin-folder>  <destiny-file>

    where:
        origin-folder   string is the path to the folder containing the files to embed
        destiny-file    string is output filename (without .rs extension)


    example:

        embed_dir src/public src/embed.rs

Example on own project directory

cargo run test_folder src/test/pr.rs

Generated code

This will generate a file per directory as:

[source, rust]

//  autogenerated  embed_dir  on ...


macro_rules! gen_get_includes_bytes{
    ($rel_path:expr, $($file:expr),+) => {
        pub fn get(file_name: &str) -> Option<&'static [u8]> {
            match file_name {
                $(
                    $file => Some(include_bytes!(concat!($rel_path, $file))),
                )+
                _ => None,
            }
        }
    }
}


gen_get_includes_bytes!("../../test_folder",
          "README.adoc",
          "Cargo.toml",
          "pr/README.adoc"
);

The first string indicates the relative position to files from generated source file.

[source, rust]

"../../http_static/",

Next strings are the files (they can be nested on folders) included.

Using the code

Quite simple and direct, just call get on the created module with the file name.

[source, rust]

    match super::http_static2::get("index.html") {
        Some(content) => content,
        None => &[] as &'static [u8],
    }

No runtime deps