Compare commits
12 Commits
41ec4d31a4
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
1ecada57cc
|
|||
|
dbaae2c9b5
|
|||
|
a6ba8928db
|
|||
|
743a6d300a
|
|||
|
659ab3f447
|
|||
|
685c0071f6
|
|||
|
44e2abfeaf
|
|||
|
6a64527022
|
|||
|
7b110a43b4
|
|||
|
1a81ce8376
|
|||
|
aac8787410
|
|||
|
d025c3c120
|
26
Cargo.lock
generated
26
Cargo.lock
generated
@@ -4,13 +4,31 @@ version = 4
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "dir-embed"
|
name = "dir-embed"
|
||||||
version = "0.1.1"
|
version = "0.3.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"mime",
|
||||||
|
"mime_guess",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "mime"
|
||||||
|
version = "0.3.17"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "mime_guess"
|
||||||
|
version = "2.0.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e"
|
||||||
|
dependencies = [
|
||||||
|
"mime",
|
||||||
|
"unicase",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.95"
|
version = "1.0.95"
|
||||||
@@ -40,6 +58,12 @@ dependencies = [
|
|||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "unicase"
|
||||||
|
version = "2.8.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "75b844d17643ee918803943289730bec8aac480150456169e647ed0b576ba539"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "unicode-ident"
|
name = "unicode-ident"
|
||||||
version = "1.0.18"
|
version = "1.0.18"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "dir-embed"
|
name = "dir-embed"
|
||||||
version = "0.1.1"
|
version = "0.3.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
description = "Like include_bytes! for directories"
|
description = "Like include_bytes! for directories"
|
||||||
@@ -11,7 +11,12 @@ readme = "README.md"
|
|||||||
[lib]
|
[lib]
|
||||||
proc-macro = true
|
proc-macro = true
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = []
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
mime = { version = "0.3.17" }
|
||||||
|
mime_guess = "2.0.5"
|
||||||
proc-macro2 = "1"
|
proc-macro2 = "1"
|
||||||
quote = "1"
|
quote = "1"
|
||||||
syn = { version = "2", features = ["full"] }
|
syn = { version = "2", features = ["full"] }
|
||||||
|
|||||||
48
README.md
48
README.md
@@ -1,11 +1,21 @@
|
|||||||
Simple way to use [`include_bytes!`](https://doc.rust-lang.org/std/macro.include_bytes.html) for directories.
|
Simple way to use [`include_bytes!`](https://doc.rust-lang.org/std/macro.include_bytes.html) 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
|
# Example
|
||||||
|
|
||||||
|
You can embed files three ways.
|
||||||
|
|
||||||
|
## Bytes mode
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
use dir_embed::Embed;
|
use dir_embed::Embed;
|
||||||
|
|
||||||
#[derive(Embed)]
|
#[derive(Embed)]
|
||||||
#[dir = "../web/static"] // Path is relativ to the current file
|
#[dir = "../web/static"] // Path is relativ to the current file
|
||||||
|
#[mode = "bytes"] // Is the default. Can be omitted.
|
||||||
pub struct Assets;
|
pub struct Assets;
|
||||||
|
|
||||||
fn main(){
|
fn main(){
|
||||||
@@ -17,3 +27,41 @@ fn main(){
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## 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}");
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Mime mode
|
||||||
|
Same as "Bytes mode" but also add the guessed mime type from [`mime_guess`](https://crates.io/crates/mime_guess).
|
||||||
|
Defaults to `application/octet-stream` for unknown types.
|
||||||
|
|
||||||
|
```rust
|
||||||
|
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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|||||||
131
src/lib.rs
131
src/lib.rs
@@ -1,3 +1,4 @@
|
|||||||
|
use mime::Mime;
|
||||||
use proc_macro::TokenStream;
|
use proc_macro::TokenStream;
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
use syn::spanned::Spanned;
|
use syn::spanned::Spanned;
|
||||||
@@ -6,35 +7,46 @@ use syn::{Attribute, DeriveInput, Expr, Ident, Lit, Meta, parse_macro_input};
|
|||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
#[proc_macro_derive(Embed, attributes(dir))]
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
enum EmbedMode {
|
||||||
|
Bytes,
|
||||||
|
Str,
|
||||||
|
BytesMime,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[proc_macro_derive(Embed, attributes(dir, mode))]
|
||||||
pub fn embed(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
pub fn embed(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||||
let input = parse_macro_input!(input as DeriveInput);
|
let input = parse_macro_input!(input as DeriveInput);
|
||||||
|
|
||||||
let struct_name = &input.ident;
|
let struct_name = &input.ident;
|
||||||
|
|
||||||
let attr = input
|
let dir_attr = input
|
||||||
.attrs
|
.attrs
|
||||||
.iter()
|
.iter()
|
||||||
.find(|e| e.path().is_ident("dir"))
|
.find(|e| e.path().is_ident("dir"))
|
||||||
.expect("No #[dir = \"...\"] attribute found");
|
.expect("No #[dir = \"...\"] attribute found");
|
||||||
|
|
||||||
let base_path = PathBuf::from(extract_dir_path(attr));
|
let mode_attr = input.attrs.iter().find(|e| e.path().is_ident("mode"));
|
||||||
|
|
||||||
|
let mode = mode_attr.map(extract_mode).unwrap_or(EmbedMode::Bytes);
|
||||||
|
|
||||||
|
let base_path = PathBuf::from(extract_dir_path(dir_attr));
|
||||||
let source_file = PathBuf::from(input.span().unwrap().file());
|
let source_file = PathBuf::from(input.span().unwrap().file());
|
||||||
|
|
||||||
let source_dir = if let Some(parent) = source_file.parent() {
|
let source_dir = if let Some(parent) = source_file.parent() {
|
||||||
parent
|
parent
|
||||||
} else {
|
} else {
|
||||||
// HACK: when running in rust-analyzer i can't seem to get the parent dir.
|
// HACK: when running in rust-analyzer i can't seem to get the parent dir.
|
||||||
return TokenStream::from(generate_impl(struct_name, Vec::new(), Vec::new()));
|
return TokenStream::from(generate_byte_impl(struct_name, Vec::new()));
|
||||||
};
|
};
|
||||||
|
|
||||||
let absolue_path = source_dir.join(&base_path);
|
let absolue_path = source_dir.join(&base_path);
|
||||||
|
|
||||||
let mut match_arms = Vec::new();
|
let mut match_arms = Vec::new();
|
||||||
let mut entries = Vec::new();
|
|
||||||
|
|
||||||
for entry in collect_files(&absolue_path) {
|
for entry in collect_files(&absolue_path) {
|
||||||
let rel_path = entry
|
let rel_path = entry
|
||||||
|
.0
|
||||||
.strip_prefix(&absolue_path)
|
.strip_prefix(&absolue_path)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.to_str()
|
.to_str()
|
||||||
@@ -42,32 +54,45 @@ pub fn embed(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
|||||||
.replace("\\", "/");
|
.replace("\\", "/");
|
||||||
|
|
||||||
let include_path = base_path.join(&rel_path);
|
let include_path = base_path.join(&rel_path);
|
||||||
let include_string = include_path.to_str();
|
let include_string = include_path.to_str().unwrap();
|
||||||
|
|
||||||
match_arms.push(quote! {
|
let arm = match mode {
|
||||||
#rel_path => Some(include_bytes!(#include_string) as &'static [u8]),
|
EmbedMode::Bytes => generate_byte_arm(&rel_path, include_string),
|
||||||
});
|
EmbedMode::Str => generate_str_arm(&rel_path, include_string),
|
||||||
|
EmbedMode::BytesMime => generate_mime_arm(
|
||||||
|
&rel_path,
|
||||||
|
include_string,
|
||||||
|
entry.1.unwrap_or(mime::APPLICATION_OCTET_STREAM),
|
||||||
|
),
|
||||||
|
};
|
||||||
|
|
||||||
entries.push(quote! {
|
match_arms.push(arm);
|
||||||
(#rel_path, include_bytes!(#include_string) as &'static [u8])
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let expanded = generate_impl(struct_name, match_arms, entries);
|
#[allow(unused_mut)]
|
||||||
|
let mut expanded = match mode {
|
||||||
|
EmbedMode::Bytes => generate_byte_impl(struct_name, match_arms),
|
||||||
|
EmbedMode::Str => generate_str_impl(struct_name, match_arms),
|
||||||
|
EmbedMode::BytesMime => generate_mime_impl(struct_name, match_arms),
|
||||||
|
};
|
||||||
|
|
||||||
proc_macro::TokenStream::from(expanded)
|
proc_macro::TokenStream::from(expanded)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn collect_files(dir: &Path) -> Vec<PathBuf> {
|
fn collect_files(dir: &Path) -> Vec<(PathBuf, Option<Mime>)> {
|
||||||
let mut files = Vec::new();
|
let mut files = Vec::new();
|
||||||
|
|
||||||
for entry in fs::read_dir(dir).unwrap() {
|
for entry in fs::read_dir(dir).unwrap() {
|
||||||
let path = entry.unwrap().path();
|
let path = entry.unwrap().path();
|
||||||
|
|
||||||
if path.is_file() {
|
if path.is_file() {
|
||||||
files.push(path);
|
let mime_type = mime_guess::from_path(&path);
|
||||||
|
files.push((path, mime_type.first()));
|
||||||
} else if path.is_dir() {
|
} else if path.is_dir() {
|
||||||
files.extend(collect_files(&path));
|
files.extend(collect_files(&path));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
files
|
files
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,10 +113,37 @@ fn extract_dir_path(attr: &Attribute) -> String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_impl(
|
fn extract_mode(attr: &Attribute) -> EmbedMode {
|
||||||
|
let meta = match &attr.meta {
|
||||||
|
Meta::NameValue(meta) => meta,
|
||||||
|
_ => panic!("Expected #[mode = \"bytes\"|\"str\"|\"mime\"] as a name-value attribute."),
|
||||||
|
};
|
||||||
|
|
||||||
|
let expr_lit = match &meta.value {
|
||||||
|
Expr::Lit(expr_lit) => expr_lit,
|
||||||
|
_ => panic!("Expected #[mode = \"bytes\"|\"str\"|\"mime\"] with a string literal."),
|
||||||
|
};
|
||||||
|
|
||||||
|
match &expr_lit.lit {
|
||||||
|
Lit::Str(str) => match str.value().as_str() {
|
||||||
|
"bytes" => EmbedMode::Bytes,
|
||||||
|
"str" => EmbedMode::Str,
|
||||||
|
"mime" => EmbedMode::BytesMime,
|
||||||
|
other => panic!("Unknown mode: {other}. Use `bytes`,`str` or `mime`."),
|
||||||
|
},
|
||||||
|
_ => panic!("Expected #[mode = \"bytes\"|\"str\"|\"mime\"] to be a string."),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_byte_arm(rel: &str, include: &str) -> proc_macro2::TokenStream {
|
||||||
|
quote! {
|
||||||
|
#rel => Some(include_bytes!(#include)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_byte_impl(
|
||||||
struct_name: &Ident,
|
struct_name: &Ident,
|
||||||
match_arms: Vec<proc_macro2::TokenStream>,
|
match_arms: Vec<proc_macro2::TokenStream>,
|
||||||
entries: Vec<proc_macro2::TokenStream>,
|
|
||||||
) -> proc_macro2::TokenStream {
|
) -> proc_macro2::TokenStream {
|
||||||
quote! {
|
quote! {
|
||||||
impl #struct_name {
|
impl #struct_name {
|
||||||
@@ -101,9 +153,50 @@ fn generate_impl(
|
|||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn iter() -> impl Iterator<Item = (&'static str, &'static [u8])> {
|
fn generate_str_arm(rel: &str, include: &str) -> proc_macro2::TokenStream {
|
||||||
[#(#entries),*].into_iter()
|
quote! {
|
||||||
|
#rel => Some(include_str!(#include)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_str_impl(
|
||||||
|
struct_name: &Ident,
|
||||||
|
match_arms: Vec<proc_macro2::TokenStream>,
|
||||||
|
) -> proc_macro2::TokenStream {
|
||||||
|
quote! {
|
||||||
|
impl #struct_name {
|
||||||
|
pub fn get(name: &str) -> Option<&'static str> {
|
||||||
|
match name {
|
||||||
|
#(#match_arms)*
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_mime_arm(rel: &str, include: &str, mime_type: Mime) -> proc_macro2::TokenStream {
|
||||||
|
let mime_str = mime_type.essence_str();
|
||||||
|
quote! {
|
||||||
|
#rel => Some((include_bytes!(#include),#mime_str)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_mime_impl(
|
||||||
|
struct_name: &Ident,
|
||||||
|
match_arms: Vec<proc_macro2::TokenStream>,
|
||||||
|
) -> proc_macro2::TokenStream {
|
||||||
|
quote! {
|
||||||
|
impl #struct_name {
|
||||||
|
pub fn get(name: &str) -> Option<(&'static [u8],&'static str)> {
|
||||||
|
match name {
|
||||||
|
#(#match_arms)*
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
1
testdata/bytes/bin
vendored
Normal file
1
testdata/bytes/bin
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
゙ュセ<EFBFBD>
|
||||||
12
testdata/bytes/index.html
vendored
Normal file
12
testdata/bytes/index.html
vendored
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Page Title</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<h1>This is a Heading</h1>
|
||||||
|
<p>This is a paragraph.</p>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
1
testdata/str/file1.txt
vendored
Normal file
1
testdata/str/file1.txt
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
file1
|
||||||
1
testdata/str/sub/file2.txt
vendored
Normal file
1
testdata/str/sub/file2.txt
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
file2
|
||||||
@@ -1,44 +1,49 @@
|
|||||||
use dir_embed::Embed;
|
use dir_embed::Embed;
|
||||||
|
|
||||||
#[derive(Embed)]
|
#[derive(Embed)]
|
||||||
#[dir = "./../testdata/"]
|
#[dir = "./../testdata/bytes/"]
|
||||||
|
#[mode = "bytes"]
|
||||||
pub struct Assets;
|
pub struct Assets;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn get() {
|
fn byte_get() {
|
||||||
assert!(Assets::get("file1.txt").is_some());
|
assert!(Assets::get("file1.txt").is_some());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn get_missing() {
|
fn byte_get_missing() {
|
||||||
assert!(Assets::get("missing.txt").is_none());
|
assert!(Assets::get("missing.txt").is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn read_content() {
|
fn byte_read_content() {
|
||||||
let content_should = "file1".as_bytes();
|
let content_should = "file1".as_bytes();
|
||||||
let content_is = Assets::get("file1.txt").unwrap();
|
let content_is = Assets::get("file1.txt").unwrap();
|
||||||
assert_eq!(*content_is, *content_should);
|
assert_eq!(*content_is, *content_should);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_string() {
|
fn byte_parse_string() {
|
||||||
let file: &[u8] = Assets::get("file1.txt").expect("Can't find file");
|
let file: &[u8] = Assets::get("file1.txt").expect("Can't find file");
|
||||||
|
|
||||||
let string = str::from_utf8(file).expect("Failed to parse file");
|
let string = str::from_utf8(file).expect("Failed to parse file");
|
||||||
|
|
||||||
println!("{string}");
|
assert_eq!(string, "file1");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn sub_directories_get() {
|
fn byte_sub_directories_get() {
|
||||||
assert!(Assets::get("sub/file2.txt").is_some());
|
assert!(Assets::get("sub/file2.txt").is_some());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn sub_directories_content() {
|
fn byte_sub_directories_content() {
|
||||||
let content_should = "file2".as_bytes();
|
let content_should = "file2".as_bytes();
|
||||||
let content_is = Assets::get("sub/file2.txt").unwrap();
|
let content_is = Assets::get("sub/file2.txt").unwrap();
|
||||||
assert_eq!(*content_is, *content_should);
|
assert_eq!(*content_is, *content_should);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn byte_read_bin() {
|
||||||
|
let file = Assets::get("bin").unwrap();
|
||||||
|
assert_eq!(file, [0xDE, 0xAD, 0xBE, 0xEF]);
|
||||||
|
}
|
||||||
61
tests/mime.rs
Normal file
61
tests/mime.rs
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
use dir_embed::Embed;
|
||||||
|
|
||||||
|
#[derive(Embed)]
|
||||||
|
#[dir = "./../testdata/bytes/"]
|
||||||
|
#[mode = "mime"]
|
||||||
|
pub struct Assets;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn mime_get() {
|
||||||
|
assert!(Assets::get("file1.txt").is_some());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn mime_get_missing() {
|
||||||
|
assert!(Assets::get("missing.txt").is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn mime_read_content() {
|
||||||
|
let content_should = "file1".as_bytes();
|
||||||
|
let content_is = Assets::get("file1.txt").unwrap();
|
||||||
|
assert_eq!(*content_is.0, *content_should);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn mime_parse_string() {
|
||||||
|
let file: &[u8] = Assets::get("file1.txt").expect("Can't find file").0;
|
||||||
|
let string = str::from_utf8(file).expect("Failed to parse file");
|
||||||
|
|
||||||
|
assert_eq!(string, "file1");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn mime_sub_directories_get() {
|
||||||
|
assert!(Assets::get("sub/file2.txt").is_some());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn mime_sub_directories_content() {
|
||||||
|
let content_should = "file2".as_bytes();
|
||||||
|
let content_is = Assets::get("sub/file2.txt").unwrap();
|
||||||
|
assert_eq!(*content_is.0, *content_should);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn mime_type_html() {
|
||||||
|
let file = Assets::get("index.html").unwrap();
|
||||||
|
assert_eq!(file.1, "text/html");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn mime_type_plain() {
|
||||||
|
let file = Assets::get("file1.txt").unwrap();
|
||||||
|
assert_eq!(file.1, "text/plain");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn mime_default_type() {
|
||||||
|
let file = Assets::get("bin").unwrap();
|
||||||
|
assert_eq!(file.1, "application/octet-stream");
|
||||||
|
}
|
||||||
35
tests/str.rs
Normal file
35
tests/str.rs
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
use dir_embed::Embed;
|
||||||
|
|
||||||
|
#[derive(Embed)]
|
||||||
|
#[dir = "./../testdata/str/"]
|
||||||
|
#[mode = "str"]
|
||||||
|
pub struct Assets;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn str_get() {
|
||||||
|
assert!(Assets::get("file1.txt").is_some());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn str_get_missing() {
|
||||||
|
assert!(Assets::get("missing.txt").is_none());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn str_read_content() {
|
||||||
|
let content_should = "file1";
|
||||||
|
let content_is = Assets::get("file1.txt").unwrap();
|
||||||
|
assert_eq!(content_is, content_should);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn str_sub_directories_get() {
|
||||||
|
assert!(Assets::get("sub/file2.txt").is_some());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn str_sub_directories_content() {
|
||||||
|
let content_should = "file2";
|
||||||
|
let content_is = Assets::get("sub/file2.txt").unwrap();
|
||||||
|
assert_eq!(content_is, content_should);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user