embed-dir/README.md

42 lines
828 B
Markdown

Simple way to use [`include_bytes!`](https://doc.rust-lang.org/std/macro.include_bytes.html) for directories.
# Example
You can embed files two ways.
## Bytes mode
```rust
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
```rust
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}");
}
```