~repos /rust-embed

#rust#proc-macro#http

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.


aa8483be mbme

4 years ago
implement support for multiple include/exclude glob attributes
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,13 @@ 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: Vec<String>, excludes: Vec<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
- for rust_embed_utils::FileEntry { rel_path, full_canonical_path } in rust_embed_utils::get_files(folder_path) {
18
+ for rust_embed_utils::FileEntry { rel_path, full_canonical_path } in rust_embed_utils::get_files(folder_path, includes, excludes) {
19
19
  match_values.push(embed_file(&rel_path, &full_canonical_path));
20
20
 
21
21
  list_values.push(if let Some(prefix) = prefix {
@@ -78,7 +78,7 @@ fn embedded(ident: &syn::Ident, folder_path: String, prefix: Option<&str>) -> To
78
78
  }
79
79
  }
80
80
 
81
- fn dynamic(ident: &syn::Ident, folder_path: String, prefix: Option<&str>) -> TokenStream2 {
81
+ fn dynamic(ident: &syn::Ident, folder_path: String, prefix: Option<&str>, includes: Vec<String>, excludes: Vec<String>) -> TokenStream2 {
82
82
  let (handle_prefix, map_iter) = if let Some(prefix) = prefix {
83
83
  (
84
84
  quote! { let file_path = file_path.strip_prefix(#prefix)?; },
@@ -88,6 +88,14 @@ fn dynamic(ident: &syn::Ident, folder_path: String, prefix: Option<&str>) -> Tok
88
88
  (TokenStream2::new(), quote! { std::borrow::Cow::from(e.rel_path) })
89
89
  };
90
90
 
91
+ let includes = quote! {
92
+ vec![#(String::from(#includes)),*]
93
+ };
94
+
95
+ let excludes = quote! {
96
+ vec![#(String::from(#excludes)),*]
97
+ };
98
+
91
99
  quote! {
92
100
  #[cfg(debug_assertions)]
93
101
  impl #ident {
@@ -96,13 +104,21 @@ fn dynamic(ident: &syn::Ident, folder_path: String, prefix: Option<&str>) -> Tok
96
104
  #handle_prefix
97
105
 
98
106
  let file_path = std::path::Path::new(#folder_path).join(file_path.replace("\\", "/"));
107
+ let rel_file_path = file_path.to_str()
108
+ .expect("Path does not have a string representation")
109
+ .strip_prefix(#folder_path).expect("Failed to turn path to relative path");
110
+
111
+ if rust_embed::utils::is_path_included(rel_file_path, &#includes, &#excludes) {
99
- rust_embed::utils::read_file_from_fs(&file_path).ok()
112
+ rust_embed::utils::read_file_from_fs(&file_path).ok()
113
+ } else {
114
+ None
115
+ }
100
116
  }
101
117
 
102
118
  /// Iterates over the file paths in the folder.
103
119
  pub fn iter() -> impl Iterator<Item = std::borrow::Cow<'static, str>> {
104
120
  use std::path::Path;
105
- rust_embed::utils::get_files(String::from(#folder_path))
121
+ rust_embed::utils::get_files(String::from(#folder_path), #includes, #excludes)
106
122
  .map(|e| #map_iter)
107
123
  }
108
124
  }
@@ -120,13 +136,13 @@ fn dynamic(ident: &syn::Ident, folder_path: String, prefix: Option<&str>) -> Tok
120
136
  }
121
137
  }
122
138
 
123
- fn generate_assets(ident: &syn::Ident, folder_path: String, prefix: Option<String>) -> TokenStream2 {
139
+ 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());
140
+ let embedded_impl = embedded(ident, folder_path.clone(), prefix.as_deref(), includes.clone(), excludes.clone());
125
141
  if cfg!(feature = "debug-embed") {
126
142
  return embedded_impl;
127
143
  }
128
144
 
129
- let dynamic_impl = dynamic(ident, folder_path, prefix.as_deref());
145
+ let dynamic_impl = dynamic(ident, folder_path, prefix.as_deref(), includes, excludes);
130
146
 
131
147
  quote! {
132
148
  #embedded_impl
@@ -165,17 +181,23 @@ fn embed_file(rel_path: &str, full_canonical_path: &str) -> TokenStream2 {
165
181
  }
166
182
  }
167
183
 
168
- /// Find a `name = "value"` attribute from the derive input
184
+ /// Find all pairs of the `name = "value"` attribute from the derive input
169
- fn find_attribute_value(ast: &syn::DeriveInput, attr_name: &str) -> Option<String> {
185
+ fn find_attribute_values(ast: &syn::DeriveInput, attr_name: &str) -> Vec<String> {
170
186
  ast
171
187
  .attrs
172
188
  .iter()
173
- .find(|value| value.path.is_ident(attr_name))
189
+ .filter(|value| value.path.is_ident(attr_name))
174
- .and_then(|attr| attr.parse_meta().ok())
190
+ .filter_map(|attr| attr.parse_meta().ok())
175
- .and_then(|meta| match meta {
191
+ .filter_map(|meta| match meta {
176
192
  Meta::NameValue(MetaNameValue { lit: Lit::Str(val), .. }) => Some(val.value()),
177
193
  _ => None,
178
194
  })
195
+ .collect()
196
+ }
197
+
198
+ /// Find a `name = "value"` attribute from the derive input
199
+ fn find_attribute_value(ast: &syn::DeriveInput, attr_name: &str) -> Option<String> {
200
+ find_attribute_values(ast, attr_name).into_iter().next()
179
201
  }
180
202
 
181
203
  fn impl_rust_embed(ast: &syn::DeriveInput) -> TokenStream2 {
@@ -189,6 +211,8 @@ fn impl_rust_embed(ast: &syn::DeriveInput) -> TokenStream2 {
189
211
 
190
212
  let folder_path = find_attribute_value(ast, "folder").expect("#[derive(RustEmbed)] should contain one attribute like this #[folder = \"examples/public/\"]");
191
213
  let prefix = find_attribute_value(ast, "prefix");
214
+ let includes = find_attribute_values(ast, "include");
215
+ let excludes = find_attribute_values(ast, "exclude");
192
216
 
193
217
  #[cfg(feature = "interpolate-folder-path")]
194
218
  let folder_path = shellexpand::full(&folder_path).unwrap().to_string();
@@ -221,10 +245,10 @@ fn impl_rust_embed(ast: &syn::DeriveInput) -> TokenStream2 {
221
245
  panic!("{}", message);
222
246
  };
223
247
 
224
- generate_assets(&ast.ident, folder_path, prefix)
248
+ generate_assets(&ast.ident, folder_path, prefix, includes, excludes)
225
249
  }
226
250
 
227
- #[proc_macro_derive(RustEmbed, attributes(folder, prefix))]
251
+ #[proc_macro_derive(RustEmbed, attributes(folder, prefix, include, exclude))]
228
252
  pub fn derive_input_object(input: TokenStream) -> TokenStream {
229
253
  let ast: DeriveInput = syn::parse(input).unwrap();
230
254
  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 = 1527818165;
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: &Vec<String>, _excludes: &Vec<String>) -> bool {
17
+ return true;
18
+ }
19
+
20
+ #[cfg(feature = "include-exclude")]
21
+ pub fn is_path_included(rel_path: &str, includes: &Vec<String>, excludes: &Vec<String>) -> bool {
22
+ use glob::Pattern;
23
+
24
+ // ignore path matched by exclusion pattern
25
+ for exclude in excludes {
26
+ let pattern = Pattern::new(exclude).expect(&format!("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).expect(&format!("invalid include pattern '{}'", include));
41
+
42
+ if pattern.matches(rel_path) {
43
+ return true;
44
+ }
45
+ }
46
+
47
+ return 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(folder_path: String, includes: Vec<String>, excludes: Vec<String>) -> impl Iterator<Item = FileEntry> {
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
- .map(move |e| {
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
- FileEntry { rel_path, full_canonical_path }
68
+ Some(FileEntry { rel_path, full_canonical_path })
69
+ } else {
70
+ None
71
+ }
33
72
  })
34
73
  }
35
74