~repos /rust-embed
git clone https://pyrossh.dev/repos/rust-embed.git
rust macro which loads files into the rust binary at compile time during release and loads the file from the fs during dev.
b65b10d5
—
Mark Drobnak 4 years ago
Merge pull request #148 from mbme/master
- Cargo.toml +6 -0
- impl/Cargo.toml +1 -0
- impl/src/lib.rs +49 -18
- readme.md +16 -0
- tests/include_exclude.rs +54 -0
- tests/metadata.rs +4 -1
- utils/Cargo.toml +5 -0
- utils/src/lib.rs +42 -3
Cargo.toml
CHANGED
|
@@ -31,6 +31,11 @@ name = "interpolated_path"
|
|
|
31
31
|
path = "tests/interpolated_path.rs"
|
|
32
32
|
required-features = ["interpolate-folder-path"]
|
|
33
33
|
|
|
34
|
+
[[test]]
|
|
35
|
+
name = "include_exclude"
|
|
36
|
+
path = "tests/include_exclude.rs"
|
|
37
|
+
required-features = ["include-exclude"]
|
|
38
|
+
|
|
34
39
|
[dependencies]
|
|
35
40
|
walkdir = "2.3.1"
|
|
36
41
|
rust-embed-impl = { version = "6.0.1", path = "impl"}
|
|
@@ -50,6 +55,7 @@ sha2 = "0.9"
|
|
|
50
55
|
debug-embed = ["rust-embed-impl/debug-embed", "rust-embed-utils/debug-embed"]
|
|
51
56
|
interpolate-folder-path = ["rust-embed-impl/interpolate-folder-path"]
|
|
52
57
|
compression = ["rust-embed-impl/compression", "include-flate"]
|
|
58
|
+
include-exclude = ["rust-embed-impl/include-exclude"]
|
|
53
59
|
nightly = ["rocket"]
|
|
54
60
|
actix = ["actix-web", "mime_guess"]
|
|
55
61
|
warp-ex = ["warp", "tokio", "mime_guess"]
|
impl/Cargo.toml
CHANGED
|
@@ -30,3 +30,4 @@ optional = true
|
|
|
30
30
|
debug-embed = []
|
|
31
31
|
interpolate-folder-path = ["shellexpand"]
|
|
32
32
|
compression = []
|
|
33
|
+
include-exclude = ["rust-embed-utils/include-exclude"]
|
impl/src/lib.rs
CHANGED
|
@@ -9,13 +9,15 @@ use proc_macro2::TokenStream as TokenStream2;
|
|
|
9
9
|
use std::{env, path::Path};
|
|
10
10
|
use syn::{Data, DeriveInput, Fields, Lit, Meta, MetaNameValue};
|
|
11
11
|
|
|
12
|
-
fn embedded(ident: &syn::Ident, folder_path: String, prefix: Option<&str>) -> TokenStream2 {
|
|
12
|
+
fn embedded(ident: &syn::Ident, folder_path: String, prefix: Option<&str>, includes: &[String], excludes: &[String]) -> TokenStream2 {
|
|
13
13
|
extern crate rust_embed_utils;
|
|
14
14
|
|
|
15
15
|
let mut match_values = Vec::<TokenStream2>::new();
|
|
16
16
|
let mut list_values = Vec::<String>::new();
|
|
17
17
|
|
|
18
|
+
let includes: Vec<&str> = includes.iter().map(AsRef::as_ref).collect();
|
|
19
|
+
let excludes: Vec<&str> = excludes.iter().map(AsRef::as_ref).collect();
|
|
18
|
-
for rust_embed_utils::FileEntry { rel_path, full_canonical_path } in rust_embed_utils::get_files(folder_path) {
|
|
20
|
+
for rust_embed_utils::FileEntry { rel_path, full_canonical_path } in rust_embed_utils::get_files(folder_path, &includes, &excludes) {
|
|
19
21
|
match_values.push(embed_file(&rel_path, &full_canonical_path));
|
|
20
22
|
|
|
21
23
|
list_values.push(if let Some(prefix) = prefix {
|
|
@@ -78,7 +80,7 @@ fn embedded(ident: &syn::Ident, folder_path: String, prefix: Option<&str>) -> To
|
|
|
78
80
|
}
|
|
79
81
|
}
|
|
80
82
|
|
|
81
|
-
fn dynamic(ident: &syn::Ident, folder_path: String, prefix: Option<&str>) -> TokenStream2 {
|
|
83
|
+
fn dynamic(ident: &syn::Ident, folder_path: String, prefix: Option<&str>, includes: &[String], excludes: &[String]) -> TokenStream2 {
|
|
82
84
|
let (handle_prefix, map_iter) = if let Some(prefix) = prefix {
|
|
83
85
|
(
|
|
84
86
|
quote! { let file_path = file_path.strip_prefix(#prefix)?; },
|
|
@@ -88,6 +90,14 @@ fn dynamic(ident: &syn::Ident, folder_path: String, prefix: Option<&str>) -> Tok
|
|
|
88
90
|
(TokenStream2::new(), quote! { std::borrow::Cow::from(e.rel_path) })
|
|
89
91
|
};
|
|
90
92
|
|
|
93
|
+
let declare_includes = quote! {
|
|
94
|
+
const includes: &[&str] = &[#(#includes),*];
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
let declare_excludes = quote! {
|
|
98
|
+
const excludes: &[&str] = &[#(#excludes),*];
|
|
99
|
+
};
|
|
100
|
+
|
|
91
101
|
quote! {
|
|
92
102
|
#[cfg(debug_assertions)]
|
|
93
103
|
impl #ident {
|
|
@@ -95,14 +105,27 @@ fn dynamic(ident: &syn::Ident, folder_path: String, prefix: Option<&str>) -> Tok
|
|
|
95
105
|
pub fn get(file_path: &str) -> Option<rust_embed::EmbeddedFile> {
|
|
96
106
|
#handle_prefix
|
|
97
107
|
|
|
108
|
+
#declare_includes
|
|
109
|
+
#declare_excludes
|
|
110
|
+
|
|
98
|
-
let
|
|
111
|
+
let rel_file_path = file_path.replace("\\", "/");
|
|
112
|
+
let file_path = std::path::Path::new(#folder_path).join(&rel_file_path);
|
|
113
|
+
|
|
114
|
+
if rust_embed::utils::is_path_included(&rel_file_path, includes, excludes) {
|
|
99
|
-
|
|
115
|
+
rust_embed::utils::read_file_from_fs(&file_path).ok()
|
|
116
|
+
} else {
|
|
117
|
+
None
|
|
118
|
+
}
|
|
100
119
|
}
|
|
101
120
|
|
|
102
121
|
/// Iterates over the file paths in the folder.
|
|
103
122
|
pub fn iter() -> impl Iterator<Item = std::borrow::Cow<'static, str>> {
|
|
104
123
|
use std::path::Path;
|
|
124
|
+
|
|
125
|
+
#declare_includes
|
|
126
|
+
#declare_excludes
|
|
127
|
+
|
|
105
|
-
rust_embed::utils::get_files(String::from(#folder_path))
|
|
128
|
+
rust_embed::utils::get_files(String::from(#folder_path), includes, excludes)
|
|
106
129
|
.map(|e| #map_iter)
|
|
107
130
|
}
|
|
108
131
|
}
|
|
@@ -120,13 +143,13 @@ fn dynamic(ident: &syn::Ident, folder_path: String, prefix: Option<&str>) -> Tok
|
|
|
120
143
|
}
|
|
121
144
|
}
|
|
122
145
|
|
|
123
|
-
fn generate_assets(ident: &syn::Ident, folder_path: String, prefix: Option<String>) -> TokenStream2 {
|
|
146
|
+
fn generate_assets(ident: &syn::Ident, folder_path: String, prefix: Option<String>, includes: Vec<String>, excludes: Vec<String>) -> TokenStream2 {
|
|
124
|
-
let embedded_impl = embedded(ident, folder_path.clone(), prefix.as_deref());
|
|
147
|
+
let embedded_impl = embedded(ident, folder_path.clone(), prefix.as_deref(), &includes, &excludes);
|
|
125
148
|
if cfg!(feature = "debug-embed") {
|
|
126
149
|
return embedded_impl;
|
|
127
150
|
}
|
|
128
151
|
|
|
129
|
-
let dynamic_impl = dynamic(ident, folder_path, prefix.as_deref());
|
|
152
|
+
let dynamic_impl = dynamic(ident, folder_path, prefix.as_deref(), &includes, &excludes);
|
|
130
153
|
|
|
131
154
|
quote! {
|
|
132
155
|
#embedded_impl
|
|
@@ -165,17 +188,18 @@ fn embed_file(rel_path: &str, full_canonical_path: &str) -> TokenStream2 {
|
|
|
165
188
|
}
|
|
166
189
|
}
|
|
167
190
|
|
|
168
|
-
/// Find
|
|
191
|
+
/// Find all pairs of the `name = "value"` attribute from the derive input
|
|
169
|
-
fn
|
|
192
|
+
fn find_attribute_values(ast: &syn::DeriveInput, attr_name: &str) -> Vec<String> {
|
|
170
193
|
ast
|
|
171
194
|
.attrs
|
|
172
195
|
.iter()
|
|
173
|
-
.
|
|
196
|
+
.filter(|value| value.path.is_ident(attr_name))
|
|
174
|
-
.
|
|
197
|
+
.filter_map(|attr| attr.parse_meta().ok())
|
|
175
|
-
.
|
|
198
|
+
.filter_map(|meta| match meta {
|
|
176
199
|
Meta::NameValue(MetaNameValue { lit: Lit::Str(val), .. }) => Some(val.value()),
|
|
177
200
|
_ => None,
|
|
178
201
|
})
|
|
202
|
+
.collect()
|
|
179
203
|
}
|
|
180
204
|
|
|
181
205
|
fn impl_rust_embed(ast: &syn::DeriveInput) -> TokenStream2 {
|
|
@@ -187,8 +211,15 @@ fn impl_rust_embed(ast: &syn::DeriveInput) -> TokenStream2 {
|
|
|
187
211
|
_ => panic!("RustEmbed can only be derived for unit structs"),
|
|
188
212
|
};
|
|
189
213
|
|
|
214
|
+
let mut folder_paths = find_attribute_values(ast, "folder");
|
|
215
|
+
if folder_paths.len() != 1 {
|
|
190
|
-
|
|
216
|
+
panic!("#[derive(RustEmbed)] must contain one attribute like this #[folder = \"examples/public/\"]");
|
|
217
|
+
}
|
|
218
|
+
let folder_path = folder_paths.remove(0);
|
|
219
|
+
|
|
220
|
+
let prefix = find_attribute_values(ast, "prefix").into_iter().next();
|
|
191
|
-
let
|
|
221
|
+
let includes = find_attribute_values(ast, "include");
|
|
222
|
+
let excludes = find_attribute_values(ast, "exclude");
|
|
192
223
|
|
|
193
224
|
#[cfg(feature = "interpolate-folder-path")]
|
|
194
225
|
let folder_path = shellexpand::full(&folder_path).unwrap().to_string();
|
|
@@ -221,10 +252,10 @@ fn impl_rust_embed(ast: &syn::DeriveInput) -> TokenStream2 {
|
|
|
221
252
|
panic!("{}", message);
|
|
222
253
|
};
|
|
223
254
|
|
|
224
|
-
generate_assets(&ast.ident, folder_path, prefix)
|
|
255
|
+
generate_assets(&ast.ident, folder_path, prefix, includes, excludes)
|
|
225
256
|
}
|
|
226
257
|
|
|
227
|
-
#[proc_macro_derive(RustEmbed, attributes(folder, prefix))]
|
|
258
|
+
#[proc_macro_derive(RustEmbed, attributes(folder, prefix, include, exclude))]
|
|
228
259
|
pub fn derive_input_object(input: TokenStream) -> TokenStream {
|
|
229
260
|
let ast: DeriveInput = syn::parse(input).unwrap();
|
|
230
261
|
let gen = impl_rust_embed(&ast);
|
readme.md
CHANGED
|
@@ -102,6 +102,21 @@ This will pull the `foo` directory relative to your `Cargo.toml` file.
|
|
|
102
102
|
|
|
103
103
|
Compress each file when embedding into the binary. Compression is done via [`include-flate`].
|
|
104
104
|
|
|
105
|
+
### `include-exclude`
|
|
106
|
+
Filter files to be embedded with multiple `#[include = "*.txt"]` and `#[exclude = "*.jpg"]` attributes.
|
|
107
|
+
Matching is done on relative file paths, via [`glob`].
|
|
108
|
+
`exclude` attributes have higher priority than `include` attributes.
|
|
109
|
+
Example:
|
|
110
|
+
|
|
111
|
+
```rust
|
|
112
|
+
#[derive(RustEmbed)]
|
|
113
|
+
#[folder = "examples/public/"]
|
|
114
|
+
#[include = "*.html"]
|
|
115
|
+
#[include = "images/*"]
|
|
116
|
+
#[exclude = "*.txt"]
|
|
117
|
+
struct Asset;
|
|
118
|
+
```
|
|
119
|
+
|
|
105
120
|
## Usage
|
|
106
121
|
|
|
107
122
|
```rust
|
|
@@ -154,3 +169,4 @@ Go Rusketeers!
|
|
|
154
169
|
The power is yours!
|
|
155
170
|
|
|
156
171
|
[`include-flate`]: https://crates.io/crates/include-flate
|
|
172
|
+
[`glob`]: https://crates.io/crates/glob
|
tests/include_exclude.rs
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
use rust_embed::RustEmbed;
|
|
2
|
+
|
|
3
|
+
#[derive(RustEmbed)]
|
|
4
|
+
#[folder = "examples/public/"]
|
|
5
|
+
struct AllAssets;
|
|
6
|
+
|
|
7
|
+
#[test]
|
|
8
|
+
fn get_works() {
|
|
9
|
+
assert!(AllAssets::get("index.html").is_some(), "index.html should exist");
|
|
10
|
+
assert!(AllAssets::get("gg.html").is_none(), "gg.html should not exist");
|
|
11
|
+
assert!(AllAssets::get("images/llama.png").is_some(), "llama.png should exist");
|
|
12
|
+
assert_eq!(AllAssets::iter().count(), 6);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
#[derive(RustEmbed)]
|
|
16
|
+
#[folder = "examples/public/"]
|
|
17
|
+
#[include = "*.html"]
|
|
18
|
+
#[include = "images/*"]
|
|
19
|
+
struct IncludeSomeAssets;
|
|
20
|
+
|
|
21
|
+
#[test]
|
|
22
|
+
fn including_some_assets_works() {
|
|
23
|
+
assert!(IncludeSomeAssets::get("index.html").is_some(), "index.html should exist");
|
|
24
|
+
assert!(IncludeSomeAssets::get("main.js").is_none(), "main.js should not exist");
|
|
25
|
+
assert!(IncludeSomeAssets::get("images/llama.png").is_some(), "llama.png should exist");
|
|
26
|
+
assert_eq!(IncludeSomeAssets::iter().count(), 4);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
#[derive(RustEmbed)]
|
|
30
|
+
#[folder = "examples/public/"]
|
|
31
|
+
#[exclude = "*.html"]
|
|
32
|
+
#[exclude = "images/*"]
|
|
33
|
+
struct ExcludeSomeAssets;
|
|
34
|
+
|
|
35
|
+
#[test]
|
|
36
|
+
fn excluding_some_assets_works() {
|
|
37
|
+
assert!(ExcludeSomeAssets::get("index.html").is_none(), "index.html should not exist");
|
|
38
|
+
assert!(ExcludeSomeAssets::get("main.js").is_some(), "main.js should exist");
|
|
39
|
+
assert!(ExcludeSomeAssets::get("images/llama.png").is_none(), "llama.png should not exist");
|
|
40
|
+
assert_eq!(ExcludeSomeAssets::iter().count(), 2);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
#[derive(RustEmbed)]
|
|
44
|
+
#[folder = "examples/public/"]
|
|
45
|
+
#[include = "images/*"]
|
|
46
|
+
#[exclude = "*.txt"]
|
|
47
|
+
struct ExcludePriorityAssets;
|
|
48
|
+
|
|
49
|
+
#[test]
|
|
50
|
+
fn exclude_has_higher_priority() {
|
|
51
|
+
assert!(ExcludePriorityAssets::get("images/doc.txt").is_none(), "doc.txt should not exist");
|
|
52
|
+
assert!(ExcludePriorityAssets::get("images/llama.png").is_some(), "llama.png should exist");
|
|
53
|
+
assert_eq!(ExcludePriorityAssets::iter().count(), 2);
|
|
54
|
+
}
|
tests/metadata.rs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
use rust_embed::{EmbeddedFile, RustEmbed};
|
|
2
2
|
use sha2::Digest;
|
|
3
|
+
use std::{fs, time::SystemTime};
|
|
3
4
|
|
|
4
5
|
#[derive(RustEmbed)]
|
|
5
6
|
#[folder = "examples/public/"]
|
|
@@ -18,7 +19,9 @@ fn hash_is_accurate() {
|
|
|
18
19
|
#[test]
|
|
19
20
|
fn last_modified_is_accurate() {
|
|
20
21
|
let index_file: EmbeddedFile = Asset::get("index.html").expect("index.html exists");
|
|
22
|
+
|
|
23
|
+
let metadata = fs::metadata(format!("{}/examples/public/index.html", env!("CARGO_MANIFEST_DIR"))).unwrap();
|
|
21
|
-
let expected_datetime_utc =
|
|
24
|
+
let expected_datetime_utc = metadata.modified().unwrap().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
|
|
22
25
|
|
|
23
26
|
assert_eq!(index_file.metadata.last_modified(), Some(expected_datetime_utc));
|
|
24
27
|
}
|
utils/Cargo.toml
CHANGED
|
@@ -15,5 +15,10 @@ edition = "2018"
|
|
|
15
15
|
walkdir = "2.3.1"
|
|
16
16
|
sha2 = "0.9"
|
|
17
17
|
|
|
18
|
+
[dependencies.glob]
|
|
19
|
+
version = "0.3.0"
|
|
20
|
+
optional = true
|
|
21
|
+
|
|
18
22
|
[features]
|
|
19
23
|
debug-embed = []
|
|
24
|
+
include-exclude = ["glob"]
|
utils/src/lib.rs
CHANGED
|
@@ -12,14 +12,49 @@ pub struct FileEntry {
|
|
|
12
12
|
pub full_canonical_path: String,
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
#[cfg(not(feature = "include-exclude"))]
|
|
16
|
+
pub fn is_path_included(_path: &str, _includes: &[&str], _excludes: &[&str]) -> bool {
|
|
17
|
+
true
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
#[cfg(feature = "include-exclude")]
|
|
21
|
+
pub fn is_path_included(rel_path: &str, includes: &[&str], excludes: &[&str]) -> bool {
|
|
22
|
+
use glob::Pattern;
|
|
23
|
+
|
|
24
|
+
// ignore path matched by exclusion pattern
|
|
25
|
+
for exclude in excludes {
|
|
26
|
+
let pattern = Pattern::new(exclude).unwrap_or_else(|_| panic!("invalid exclude pattern '{}'", exclude));
|
|
27
|
+
|
|
28
|
+
if pattern.matches(rel_path) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// accept path if no includes provided
|
|
34
|
+
if includes.is_empty() {
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// accept path if matched by inclusion pattern
|
|
39
|
+
for include in includes {
|
|
40
|
+
let pattern = Pattern::new(include).unwrap_or_else(|_| panic!("invalid include pattern '{}'", include));
|
|
41
|
+
|
|
42
|
+
if pattern.matches(rel_path) {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
false
|
|
48
|
+
}
|
|
49
|
+
|
|
15
50
|
#[cfg_attr(all(debug_assertions, not(feature = "debug-embed")), allow(unused))]
|
|
16
|
-
pub fn get_files(folder_path: String) -> impl Iterator<Item = FileEntry> {
|
|
51
|
+
pub fn get_files<'patterns>(folder_path: String, includes: &'patterns [&str], excludes: &'patterns [&str]) -> impl Iterator<Item = FileEntry> + 'patterns {
|
|
17
52
|
walkdir::WalkDir::new(&folder_path)
|
|
18
53
|
.follow_links(true)
|
|
19
54
|
.into_iter()
|
|
20
55
|
.filter_map(|e| e.ok())
|
|
21
56
|
.filter(|e| e.file_type().is_file())
|
|
22
|
-
.
|
|
57
|
+
.filter_map(move |e| {
|
|
23
58
|
let rel_path = path_to_str(e.path().strip_prefix(&folder_path).unwrap());
|
|
24
59
|
let full_canonical_path = path_to_str(std::fs::canonicalize(e.path()).expect("Could not get canonical path"));
|
|
25
60
|
|
|
@@ -29,7 +64,11 @@ pub fn get_files(folder_path: String) -> impl Iterator<Item = FileEntry> {
|
|
|
29
64
|
rel_path
|
|
30
65
|
};
|
|
31
66
|
|
|
67
|
+
if is_path_included(&rel_path, includes, excludes) {
|
|
32
|
-
|
|
68
|
+
Some(FileEntry { rel_path, full_canonical_path })
|
|
69
|
+
} else {
|
|
70
|
+
None
|
|
71
|
+
}
|
|
33
72
|
})
|
|
34
73
|
}
|
|
35
74
|
|