~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.


287a73fe Day Fisher

1 year ago
Add metadata_only to dynamic mode; add test
Files changed (2) hide show
  1. impl/src/lib.rs +7 -3
  2. tests/metadata_only.rs +12 -0
impl/src/lib.rs CHANGED
@@ -107,7 +107,7 @@ fn embedded(
107
107
  })
108
108
  }
109
109
 
110
- fn dynamic(ident: &syn::Ident, folder_path: String, prefix: Option<&str>, includes: &[String], excludes: &[String]) -> TokenStream2 {
110
+ fn dynamic(ident: &syn::Ident, folder_path: String, prefix: Option<&str>, includes: &[String], excludes: &[String], metadata_only: bool) -> TokenStream2 {
111
111
  let (handle_prefix, map_iter) = if let ::std::option::Option::Some(prefix) = prefix {
112
112
  (
113
113
  quote! { let file_path = file_path.strip_prefix(#prefix)?; },
@@ -125,6 +125,10 @@ fn dynamic(ident: &syn::Ident, folder_path: String, prefix: Option<&str>, includ
125
125
  const EXCLUDES: &[&str] = &[#(#excludes),*];
126
126
  };
127
127
 
128
+ let strip_contents = metadata_only.then_some(quote! {
129
+ .map(|mut file| { file.data = ::std::default::Default::default(); file })
130
+ });
131
+
128
132
  let canonical_folder_path = Path::new(&folder_path).canonicalize().expect("folder path must resolve to an absolute path");
129
133
  let canonical_folder_path = canonical_folder_path.to_str().expect("absolute folder path must be valid unicode");
130
134
 
@@ -158,7 +162,7 @@ fn dynamic(ident: &syn::Ident, folder_path: String, prefix: Option<&str>, includ
158
162
  }
159
163
 
160
164
  if rust_embed::utils::is_path_included(&rel_file_path, INCLUDES, EXCLUDES) {
161
- rust_embed::utils::read_file_from_fs(&canonical_file_path).ok()
165
+ rust_embed::utils::read_file_from_fs(&canonical_file_path).ok() #strip_contents
162
166
  } else {
163
167
  ::std::option::Option::None
164
168
  }
@@ -206,7 +210,7 @@ fn generate_assets(
206
210
  return embedded_impl;
207
211
  }
208
212
  let embedded_impl = embedded_impl?;
209
- let dynamic_impl = dynamic(ident, absolute_folder_path, prefix.as_deref(), &includes, &excludes);
213
+ let dynamic_impl = dynamic(ident, absolute_folder_path, prefix.as_deref(), &includes, &excludes, metadata_only);
210
214
 
211
215
  Ok(quote! {
212
216
  #embedded_impl
tests/metadata_only.rs ADDED
@@ -0,0 +1,12 @@
1
+ use rust_embed::{EmbeddedFile, RustEmbed};
2
+
3
+ #[derive(RustEmbed)]
4
+ #[folder = "examples/public/"]
5
+ #[metadata_only = true]
6
+ struct Asset;
7
+
8
+ #[test]
9
+ fn file_is_empty() {
10
+ let index_file: EmbeddedFile = Asset::get("index.html").expect("index.html exists");
11
+ assert_eq!(index_file.data.len(), 0);
12
+ }