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


88b94eab Jasper van Herpt

3 years ago
Get directory entries in release builds as well
impl/src/lib.rs CHANGED
@@ -17,7 +17,12 @@ fn embedded(ident: &syn::Ident, folder_path: String, prefix: Option<&str>, inclu
17
17
 
18
18
  let includes: Vec<&str> = includes.iter().map(AsRef::as_ref).collect();
19
19
  let excludes: Vec<&str> = excludes.iter().map(AsRef::as_ref).collect();
20
+ for rust_embed_utils::FileEntry {
21
+ rel_path,
22
+ full_canonical_path,
23
+ is_dir: _,
20
- for rust_embed_utils::FileEntry { rel_path, full_canonical_path } in rust_embed_utils::get_files(folder_path, &includes, &excludes) {
24
+ } in rust_embed_utils::get_files(folder_path, &includes, &excludes)
25
+ {
21
26
  match_values.push(embed_file(&rel_path, &full_canonical_path));
22
27
 
23
28
  list_values.push(if let Some(prefix) = prefix {
@@ -176,15 +181,19 @@ fn embed_file(rel_path: &str, full_canonical_path: &str) -> TokenStream2 {
176
181
  None => quote! { None },
177
182
  };
178
183
 
179
- let embedding_code = if cfg!(feature = "compression") {
184
+ let embedding_code = if cfg!(feature = "compression") && !is_dir {
180
185
  quote! {
181
186
  rust_embed::flate!(static FILE: [u8] from #full_canonical_path);
182
187
  let bytes = &FILE[..];
183
188
  }
184
- } else {
189
+ } else if !is_dir {
185
190
  quote! {
186
191
  let bytes = &include_bytes!(#full_canonical_path)[..];
187
192
  }
193
+ } else {
194
+ quote! {
195
+ let bytes = Vec::new();
196
+ }
188
197
  };
189
198
 
190
199
  quote! {
tests/interpolated_path.rs CHANGED
@@ -19,7 +19,7 @@ fn iter_works() {
19
19
  assert!(Asset::get(file.as_ref()).is_some());
20
20
  num_files += 1;
21
21
  }
22
- assert_eq!(num_files, 6);
22
+ assert_eq!(num_files, 8);
23
23
  }
24
24
 
25
25
  #[test]
@@ -32,6 +32,6 @@ fn trait_works_generic_helper<E: rust_embed::RustEmbed>() {
32
32
  assert!(E::get(file.as_ref()).is_some());
33
33
  num_files += 1;
34
34
  }
35
- assert_eq!(num_files, 6);
35
+ assert_eq!(num_files, 8);
36
36
  assert!(E::get("gg.html").is_none(), "gg.html should not exist");
37
37
  }
tests/lib.rs CHANGED
@@ -29,7 +29,7 @@ fn iter_works() {
29
29
  assert!(Asset::get(file.as_ref()).is_some());
30
30
  num_files += 1;
31
31
  }
32
- assert_eq!(num_files, 6);
32
+ assert_eq!(num_files, 8);
33
33
  }
34
34
 
35
35
  #[test]
@@ -42,6 +42,6 @@ fn trait_works_generic_helper<E: rust_embed::RustEmbed>() {
42
42
  assert!(E::get(file.as_ref()).is_some());
43
43
  num_files += 1;
44
44
  }
45
- assert_eq!(num_files, 6);
45
+ assert_eq!(num_files, 8);
46
46
  assert!(E::get("gg.html").is_none(), "gg.html should not exist");
47
47
  }
tests/metadata.rs CHANGED
@@ -30,7 +30,7 @@ fn last_modified_is_accurate() {
30
30
  fn is_dir_is_accurate() {
31
31
  let index_file: EmbeddedFile = Asset::get("index.html").expect("index.html exists");
32
32
  let doc_file: EmbeddedFile = Asset::get("images/doc.txt").expect("doc.txt exists");
33
- let images_folder: EmbeddedFile = Asset::get("images/").expect("images exists");
33
+ let images_folder: EmbeddedFile = Asset::get("images").expect("images exists");
34
34
 
35
35
  assert_eq!(index_file.metadata.is_dir(), false);
36
36
  assert_eq!(doc_file.metadata.is_dir(), false);
utils/src/lib.rs CHANGED
@@ -10,6 +10,7 @@ use std::{fs, io};
10
10
  pub struct FileEntry {
11
11
  pub rel_path: String,
12
12
  pub full_canonical_path: String,
13
+ pub is_dir: bool,
13
14
  }
14
15
 
15
16
  #[cfg(not(feature = "include-exclude"))]
@@ -58,10 +59,10 @@ pub fn get_files<'patterns>(folder_path: String, includes: &'patterns [&str], ex
58
59
  .sort_by_file_name()
59
60
  .into_iter()
60
61
  .filter_map(|e| e.ok())
61
- .filter(|e| e.file_type().is_file())
62
62
  .filter_map(move |e| {
63
63
  let rel_path = path_to_str(e.path().strip_prefix(&folder_path).unwrap());
64
64
  let full_canonical_path = path_to_str(std::fs::canonicalize(e.path()).expect("Could not get canonical path"));
65
+ let is_dir = e.file_type().is_dir();
65
66
 
66
67
  let rel_path = if std::path::MAIN_SEPARATOR == '\\' {
67
68
  rel_path.replace('\\', "/")
@@ -70,7 +71,11 @@ pub fn get_files<'patterns>(folder_path: String, includes: &'patterns [&str], ex
70
71
  };
71
72
 
72
73
  if is_path_included(&rel_path, includes, excludes) {
74
+ Some(FileEntry {
75
+ rel_path,
73
- Some(FileEntry { rel_path, full_canonical_path })
76
+ full_canonical_path,
77
+ is_dir,
78
+ })
74
79
  } else {
75
80
  None
76
81
  }