rust-embed v8.11.0

#rust#proc-macro#http

git clone https://git.pyrossh.dev/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.


README.md
604dc58 1
# rust-embed
01d1dda 2
604dc58 3
Rust macro which loads files into the rust binary at compile time during release and loads the file from the fs during dev.
26bf2b6 4
b943f43 5
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.
26bf2b6 6
26bf2b6 7
## Installation
26bf2b6 8
b11ef88 9
```toml
26bf2b6 10
[dependencies]
93be519 11
rust-embed="8.11.0"
26bf2b6 12
```
26bf2b6 13
26bf2b6 14
## Documentation
1626073 15
3c3c6be 16
You need to add the custom derive macro RustEmbed to your struct with an attribute `folder` which is the path to your static folder.
1626073 17
1626073 18
The path resolution works as follows:
1626073 19
1626073 20
- In `debug` and when `debug-embed` feature is not enabled, the folder path is resolved relative to where the binary is run from.
1626073 21
- In `release` or when `debug-embed` feature is enabled, the folder path is resolved relative to where `Cargo.toml` is.
1626073 22
26bf2b6 23
```rust
4527045 24
#[derive(Embed)]
547bcc0 25
#[folder = "examples/public/"]
89b83a8 26
struct Asset;
af0ee5f 27
```
6e11b75 28
6e11b75 29
The macro will generate the following code:
6e11b75 30
3c3c6be 31
```rust
01d1dda 32
impl Asset {
c530a6c 33
  pub fn get(file_path: &str) -> Option<rust_embed::EmbeddedFile> {
01d1dda 34
    ...
6e11b75 35
  }
01d1dda 36
89d3274 37
  pub fn iter() -> impl Iterator<Item = Cow<'static, str>> {
6e11b75 38
    ...
6e11b75 39
  }
6e11b75 40
}
663001d 41
impl RustEmbed for Asset {
c530a6c 42
  fn get(file_path: &str) -> Option<rust_embed::EmbeddedFile> {
663001d 43
    ...
663001d 44
  }
74744a7 45
  fn iter() -> impl Iterator<Item = Cow<'static, str>> {
663001d 46
    ...
663001d 47
  }
663001d 48
}
c530a6c 49
c530a6c 50
// Where EmbeddedFile contains these fields,
c530a6c 51
pub struct EmbeddedFile {
c530a6c 52
  pub data: Cow<'static, [u8]>,
c530a6c 53
  pub metadata: Metadata,
c530a6c 54
}
c530a6c 55
pub struct Metadata {
c530a6c 56
  hash: [u8; 32],
c530a6c 57
  last_modified: Option<u64>,
d308513 58
  created: Option<u64>,
c530a6c 59
}
3c3c6be 60
```
af0ee5f 61
7d5313d 62
## Methods
7d5313d 63
* `get(file_path: &str) -> Option<rust_embed::EmbeddedFile>`
6e11b75 64
c530a6c 65
Given a relative path from the assets folder returns the `EmbeddedFile` if found.
c530a6c 66
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.
c530a6c 67
Otherwise the bytes are read from the file system on each call and a `Option<rust_embed::EmbeddedFile>` is returned.
6e11b75 68
7d5313d 69
* `iter()`
01d1dda 70
6e11b75 71
Iterates the files in this assets folder.
1626073 72
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.
6e11b75 73
Otherwise the files are listed from the file system on each call.
6e11b75 74
4ac6f5b 75
## Attributes
7d5313d 76
* `prefix`
c530a6c 77
3db19b9 78
You can add `#[prefix = "my_prefix/"]` to the `RustEmbed` struct to add a prefix
3db19b9 79
to all of the file paths. This prefix will be required on `get` calls, and will
3db19b9 80
be included in the file paths returned by `iter`.
3db19b9 81
7d5313d 82
* `metadata_only`
4ac6f5b 83
4ac6f5b 84
You can add `#[metadata_only = true]` to the `RustEmbed` struct to exclude file contents from the
4ac6f5b 85
binary. Only file paths and metadata will be embedded.
4ac6f5b 86
7d5313d 87
* `allow_missing`
097d187 88
097d187 89
You can add `#[allow_missing = true]` to the `RustEmbed` struct to allow the embedded folder to be missing.
097d187 90
In that case, RustEmbed will be empty.
097d187 91
1626073 92
## Features
1626073 93
cba24b8 94
* `debug-embed`: Always embed the files in the binary, even in debug mode.
7d5313d 95
* `compression`: Compress each file when embedding into the binary. Compression is done via [include-flate](https://crates.io/crates/include-flate).
7d5313d 96
* `deterministic-timestamps`: Overwrite embedded files' timestamps with `0` to preserve deterministic builds with `debug-embed` or release mode.
7d5313d 97
* `interpolate-folder-path`: Allow environment variables to be used in the `folder` path. This will pull the `foo` directory relative to your `Cargo.toml` file.
ff99a58 98
```rust
4527045 99
#[derive(Embed)]
ff99a58 100
#[folder = "$CARGO_MANIFEST_DIR/foo"]
ff99a58 101
struct Asset;
ff99a58 102
```
cba24b8 103
* `include-exclude`: Filter files to be embedded with multiple `#[include = "*.txt"]` and `#[exclude = "*.jpg"]` attributes. 
cba24b8 104
Matching is done on relative file paths, via [globset](https://crates.io/crates/globset). `exclude` attributes have higher priority than `include` attributes.
aa8483b 105
```rust
4527045 106
use rust_embed::Embed;
4527045 107
4527045 108
#[derive(Embed)]
aa8483b 109
#[folder = "examples/public/"]
aa8483b 110
#[include = "*.html"]
aa8483b 111
#[include = "images/*"]
aa8483b 112
#[exclude = "*.txt"]
aa8483b 113
struct Asset;
aa8483b 114
```
aa8483b 115
af0ee5f 116
## Usage
01d1dda 117
af0ee5f 118
```rust
4527045 119
use rust_embed::Embed;
5a7a548 120
4527045 121
#[derive(Embed)]
547bcc0 122
#[folder = "examples/public/"]
3db19b9 123
#[prefix = "prefix/"]
89b83a8 124
struct Asset;
051375a 125
af0ee5f 126
fn main() {
3db19b9 127
  let index_html = Asset::get("prefix/index.html").unwrap();
c530a6c 128
  println!("{:?}", std::str::from_utf8(index_html.data.as_ref()));
1626073 129
1626073 130
  for file in Asset::iter() {
1626073 131
      println!("{}", file.as_ref());
1626073 132
  }
5a7a548 133
}
26bf2b6 134
```
26bf2b6 135
ef6e84b 136
## Integrations
ef6e84b 137
ef6e84b 138
1. [Poem](https://github.com/poem-web/poem) for poem framework under feature flag "embed"
ef6e84b 139
2. [warp_embed](https://docs.rs/warp-embed/latest/warp_embed/) for warp framework
ef6e84b 140
26bf2b6 141
## Examples
01d1dda 142
b8c122c 143
```sh
b8c122c 144
cargo run --example basic #  dev mode where it reads from the fs
b8c122c 145
cargo run --example basic --release # release mode where it reads from binary
b8c122c 146
cargo run --example actix --features actix # https://github.com/actix/actix-web
b8c122c 147
cargo run --example rocket --features rocket # https://github.com/SergioBenitez/Rocket
b8c122c 148
cargo run --example warp --features warp-ex # https://github.com/seanmonstar/warp
b8c122c 149
cargo run --example axum --features axum-ex # https://github.com/tokio-rs/axum
b8c122c 150
cargo run --example poem --features poem-ex # https://github.com/poem-web/poem
b8c122c 151
cargo run --example salvo --features salvo-ex # https://github.com/salvo-rs/salvo
b8c122c 152
```
ff1bf2f 153
5a7a548 154
## Testing
01d1dda 155
91d5481 156
```sh
91d5481 157
cargo test --test lib
91d5481 158
cargo test --test lib --features "debug-embed"
91d5481 159
cargo test --test lib --features "compression" --release
91d5481 160
cargo test --test mime_guess --features "mime-guess"
91d5481 161
cargo test --test mime_guess --features "mime-guess" --release
91d5481 162
cargo test --test interpolated_path --features "interpolate-folder-path"
91d5481 163
cargo test --test interpolated_path --features "interpolate-folder-path" --release
91d5481 164
cargo test --test custom_crate_path
91d5481 165
cargo test --test custom_crate_path --release
91d5481 166
cargo build --example basic
91d5481 167
cargo build --example rocket --features rocket
91d5481 168
cargo build --example actix --features actix
91d5481 169
cargo build --example axum --features axum-ex
91d5481 170
cargo build --example warp --features warp-ex
91d5481 171
cargo test --test lib --release
91d5481 172
cargo build --example basic --release
91d5481 173
cargo build --example rocket --features rocket --release
91d5481 174
cargo build --example actix --features actix --release
91d5481 175
cargo build --example axum --features axum-ex --release
91d5481 176
cargo build --example warp --features warp-ex --release
91d5481 177
```