dir-embed

Crates.iodir-embed
lib.rsdir-embed
version0.3.0
created_at2025-07-28 17:49:46.364206+00
updated_at2025-07-30 13:30:34.404755+00
descriptionLike include_bytes! for directories
homepagehttps://git.kapelle.org/niklas/embed-dir
repositoryhttps://git.kapelle.org/niklas/embed-dir
max_upload_size
id1771432
size14,291
(Djeeberjr)

documentation

README

Simple way to use include_bytes! for directories.

Why

I wanted to include a directory for my microcontroller project, but none of the solutions I used before seems to work in a no_std environment.

Example

You can embed files three ways.

Bytes mode

use dir_embed::Embed;

#[derive(Embed)]
#[dir = "../web/static"] // Path is relativ to the current file
#[mode = "bytes"] // Is the default. Can be omitted.
pub struct Assets;

fn main(){
    let file: &[u8] = Assets::get("css/style.css").expect("Can't find file");

    let string = str::from_utf8(file).expect("Failed to parse file");

    println!("{string}");
}

Str mode

use dir_embed::Embed;

#[derive(Embed)]
#[dir = "../web/static"] // Path is relativ to the current file
#[mode = "str"]
pub struct Assets;

fn main(){
    let file: &str = Assets::get("css/style.css").expect("Can't find file");

    println!("{file}");
}

Mime mode

Same as "Bytes mode" but also add the guessed mime type from mime_guess. Defaults to application/octet-stream for unknown types.

use dir_embed::Embed;

#[derive(Embed)]
#[dir = "../web/static"] // Path is relativ to the current file
#[mode = "mime"] 
pub struct Assets;

fn main(){
    let file: (&[u8],&str) = Assets::get("css/style.css").expect("Can't find file");

    let string = str::from_utf8(file.0).expect("Failed to parse file");

    println!("{string}");
    println!("MIME: {file.1}"); // text/css
}
Commit count: 0

cargo fmt