ProductPromotion
Logo

Rust

made by https://0x3d.site

GitHub - pyrossh/rust-embed: Rust Macro which loads files into the rust binary at compile time during release and loads the file from the fs during dev.
Rust Macro which loads files into the rust binary at compile time during release and loads the file from the fs during dev. - pyrossh/rust-embed
Visit Site

GitHub - pyrossh/rust-embed: Rust Macro which loads files into the rust binary at compile time during release and loads the file from the fs during dev.

GitHub - pyrossh/rust-embed: Rust Macro which loads files into the rust binary at compile time during release and loads the file from the fs during dev.

Rust Embed Build Status crates.io

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

[dependencies]
rust-embed="8.5.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 debug and when debug-embed feature is not enabled, the folder path is resolved relative to where the binary is run from.
  • In release or when debug-embed feature is enabled, the folder path is resolved relative to where Cargo.toml is.
#[derive(Embed)]
#[folder = "examples/public/"]
struct Asset;

The macro will generate the following code:

impl Asset {
  pub fn get(file_path: &str) -> Option<rust_embed::EmbeddedFile> {
    ...
  }

  pub fn iter() -> impl Iterator<Item = Cow<'static, str>> {
    ...
  }
}
impl RustEmbed for Asset {
  fn get(file_path: &str) -> Option<rust_embed::EmbeddedFile> {
    ...
  }
  fn iter() -> impl Iterator<Item = Cow<'static, str>> {
    ...
  }
}

// Where EmbeddedFile contains these fields,
pub struct EmbeddedFile {
  pub data: Cow<'static, [u8]>,
  pub metadata: Metadata,
}
pub struct Metadata {
  hash: [u8; 32],
  last_modified: Option<u64>,
  created: Option<u64>,
}

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.

Features

debug-embed

Always embed the files in the binary, even in debug mode.

interpolate-folder-path

Allow environment variables to be used in the folder path. Example:

#[derive(Embed)]
#[folder = "$CARGO_MANIFEST_DIR/foo"]
struct Asset;

This will pull the foo directory relative to your Cargo.toml file.

compression

Compress each file when embedding into the binary. Compression is done via include-flate.

include-exclude

Filter files to be embedded with multiple #[include = "*.txt"] and #[exclude = "*.jpg"] attributes. Matching is done on relative file paths, via globset. exclude attributes have higher priority than include attributes. Example:

use rust_embed::Embed;

#[derive(Embed)]
#[folder = "examples/public/"]
#[include = "*.html"]
#[include = "images/*"]
#[exclude = "*.txt"]
struct Asset;

Usage

use rust_embed::Embed;

#[derive(Embed)]
#[folder = "examples/public/"]
#[prefix = "prefix/"]
struct Asset;

fn main() {
  let index_html = Asset::get("prefix/index.html").unwrap();
  println!("{:?}", std::str::from_utf8(index_html.data.as_ref()));

  for file in Asset::iter() {
      println!("{}", file.as_ref());
  }
}

Integrations

  1. Poem for poem framework under feature flag "embed"
  2. warp_embed for warp framework

Examples

To run the example in dev mode where it reads from the fs,

cargo run --example basic

To run the example in release mode where it reads from binary,

cargo run --example basic --release

Note: To run the actix-web example:

cargo run --example actix --features actix

Note: To run the rocket example:

cargo run --example rocket --features rocket

Note: To run the warp example:

cargo run --example warp --features warp-ex

Note: To run the axum example:

cargo run --example axum --features axum-ex

Note: To run the poem example:

cargo run --example poem --features poem-ex

Note: To run the salvo example:

cargo run --example salvo --features salvo-ex

Testing

debug: cargo test --test lib

release: cargo test --test lib --release

Go Rusketeers! The power is yours!

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory