~repos /rust-embed

#rust#proc-macro#http

git clone https://pyrossh.dev/repos/rust-embed.git
Discussions: https://groups.google.com/g/rust-embed-devs

rust macro which loads files into the rust binary at compile time during release and loads the file from the fs during dev.


Files changed (2) hide show
  1. impl/src/lib.rs +20 -7
  2. utils/src/lib.rs +1 -1
impl/src/lib.rs CHANGED
@@ -56,6 +56,16 @@ fn embedded(
56
56
  (#path, #bytes),
57
57
  }
58
58
  });
59
+ let value_type = if cfg!(feature = "compression") {
60
+ quote! { fn() -> rust_embed::EmbeddedFile }
61
+ } else {
62
+ quote! { rust_embed::EmbeddedFile }
63
+ };
64
+ let get_value = if cfg!(feature = "compression") {
65
+ quote! {|idx| (ENTRIES[idx].1)()}
66
+ } else {
67
+ quote! {|idx| ENTRIES[idx].1.clone()}
68
+ };
59
69
  quote! {
60
70
  #not_debug_attr
61
71
  impl #ident {
@@ -63,10 +73,10 @@ fn embedded(
63
73
  pub fn get(file_path: &str) -> Option<rust_embed::EmbeddedFile> {
64
74
  #handle_prefix
65
75
  let key = file_path.replace("\\", "/");
66
- const ENTRIES: &'static [(&'static str, rust_embed::EmbeddedFile)] = &[
76
+ const ENTRIES: &'static [(&'static str, #value_type)] = &[
67
77
  #(#match_values)*];
68
78
  let position = ENTRIES.binary_search_by_key(&key.as_str(), |entry| entry.0);
69
- position.ok().map(|index| ENTRIES[index].1.clone())
79
+ position.ok().map(#get_value)
70
80
 
71
81
  }
72
82
 
@@ -209,21 +219,24 @@ fn embed_file(folder_path: Option<&str>, rel_path: &str, full_canonical_path: &s
209
219
  let full_relative_path = PathBuf::from_iter([folder_path.expect("folder_path must be provided under `compression` feature"), rel_path]);
210
220
  let full_relative_path = full_relative_path.to_string_lossy();
211
221
  quote! {
212
- rust_embed::flate!(static FILE: [u8] from #full_relative_path);
222
+ rust_embed::flate!(static BYTES: [u8] from #full_relative_path);
213
- const BYTES: &'static [u8] = FILE;
214
223
  }
215
224
  } else {
216
225
  quote! {
217
226
  const BYTES: &'static [u8] = include_bytes!(#full_canonical_path);
218
227
  }
219
228
  };
220
-
229
+ let closure_args = if cfg!(feature = "compression") {
230
+ quote! { || }
231
+ } else {
232
+ quote! {}
233
+ };
221
234
  quote! {
222
- {
235
+ #closure_args {
223
236
  #embedding_code
224
237
 
225
238
  rust_embed::EmbeddedFile {
226
- data: std::borrow::Cow::Borrowed(BYTES),
239
+ data: std::borrow::Cow::Borrowed(&BYTES),
227
240
  metadata: rust_embed::Metadata::__rust_embed_new([#(#hash),*], #last_modified #mimetype_tokens)
228
241
  }
229
242
  }
utils/src/lib.rs CHANGED
@@ -100,7 +100,7 @@ impl Metadata {
100
100
  hash,
101
101
  last_modified,
102
102
  #[cfg(feature = "mime-guess")]
103
- mimetype: mimetype.into(),
103
+ mimetype: Cow::Borrowed(mimetype),
104
104
  }
105
105
  }
106
106