Rust Embed 
Rust Custom Derive Macro which loads files into the rust binary at compile time during release and loads the file from the fs during dev.
You can use this to embed your css, js and images into a single executable which can be deployed to your servers. Also it makes it easy to build a very small docker image for you to deploy.
Installation
[]
="8.8.0"
Documentation
You need to add the custom derive macro RustEmbed to your struct with an attribute folder which is the path to your static folder.
The path resolution works as follows:
- In
debugand whendebug-embedfeature is not enabled, the folder path is resolved relative to where the binary is run from. - In
releaseor whendebug-embedfeature is enabled, the folder path is resolved relative to whereCargo.tomlis.
;
The macro will generate the following code:
// Where EmbeddedFile contains these fields,
Methods
get(file_path: &str) -> Option<rust_embed::EmbeddedFile>
Given a relative path from the assets folder returns the EmbeddedFile if found.
If the feature debug-embed is enabled or the binary compiled in release mode the bytes have been embeded in the binary and a Option<rust_embed::EmbeddedFile> is returned.
Otherwise the bytes are read from the file system on each call and a Option<rust_embed::EmbeddedFile> is returned.
iter()
Iterates the files in this assets folder.
If the feature debug-embed is enabled or the binary compiled in release mode a static array to the list of relative paths to the files is returned.
Otherwise the files are listed from the file system on each call.
Attributes
prefix
You can add #[prefix = "my_prefix/"] to the RustEmbed struct to add a prefix
to all of the file paths. This prefix will be required on get calls, and will
be included in the file paths returned by iter.
metadata_only
You can add #[metadata_only = true] to the RustEmbed struct to exclude file contents from the
binary. Only file paths and metadata will be embedded.
allow_missing
You can add #[allow_missing = true] to the RustEmbed struct to allow the embedded folder to be missing.
In that case, RustEmbed will be empty.
Features
debug-embed: Always embed the files in the binary, even in debug mode.compression: Compress each file when embedding into the binary. Compression is done via include-flate.deterministic-timestamps: Overwrite embedded files' timestamps with0to preserve deterministic builds withdebug-embedor release mode.interpolate-folder-path: Allow environment variables to be used in thefolderpath. This will pull thefoodirectory relative to yourCargo.tomlfile.
;
include-exclude: Filter files to be embedded with multiple#[include = "*.txt"]and#[exclude = "*.jpg"]attributes. Matching is done on relative file paths, via globset.excludeattributes have higher priority thanincludeattributes.
use Embed;
;
Usage
use Embed;
;
Integrations
- Poem for poem framework under feature flag "embed"
- warp_embed for warp framework
Examples
Testing