~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 +6 -4
  2. src/utils.rs +8 -13
impl/src/lib.rs CHANGED
@@ -15,6 +15,9 @@ mod utils;
15
15
 
16
16
  #[cfg(all(debug_assertions, not(feature = "debug-embed")))]
17
17
  fn generate_assets(ident: &syn::Ident, folder_path: String) -> quote::Tokens {
18
+ use std::env::current_dir;
19
+ // resolve relative to current path
20
+ let folder_path = utils::path_to_str(current_dir().unwrap().join(folder_path));
18
21
  quote!{
19
22
  impl #ident {
20
23
  pub fn get(file_path: &str) -> Option<impl AsRef<[u8]>> {
@@ -22,10 +25,8 @@ fn generate_assets(ident: &syn::Ident, folder_path: String) -> quote::Tokens {
22
25
  use std::io::Read;
23
26
  use std::path::Path;
24
27
 
25
- let folder_path = #folder_path;
26
- let name = &format!("{}{}", folder_path, file_path);
28
+ let file_path = Path::new(#folder_path).join(file_path);
27
- let path = &Path::new(name);
28
- let mut file = match File::open(path) {
29
+ let mut file = match File::open(file_path) {
29
30
  Ok(mut file) => file,
30
31
  Err(_e) => {
31
32
  return None
@@ -41,6 +42,7 @@ fn generate_assets(ident: &syn::Ident, folder_path: String) -> quote::Tokens {
41
42
  }
42
43
 
43
44
  pub fn iter() -> impl Iterator<Item = impl AsRef<str>> {
45
+ use std::path::Path;
44
46
  use rust_embed::utils::get_files;
45
47
  get_files(String::from(#folder_path)).map(|e| e.rel_path)
46
48
  }
src/utils.rs CHANGED
@@ -1,3 +1,5 @@
1
+ use std;
2
+
1
3
  #[cfg_attr(all(debug_assertions, not(feature = "debug-embed")), allow(unused))]
2
4
  pub struct FileEntry {
3
5
  pub rel_path: String,
@@ -13,19 +15,8 @@ pub fn get_files(folder_path: String) -> impl Iterator<Item = FileEntry> {
13
15
  .filter_map(|e| e.ok())
14
16
  .filter(|e| e.file_type().is_file())
15
17
  .map(move |e| {
16
- let rel_path = e
17
- .path()
18
- .strip_prefix(&folder_path)
19
- .unwrap()
20
- .to_str()
21
- .expect("Path does not have a string representation")
18
+ let rel_path = path_to_str(e.path().strip_prefix(&folder_path).unwrap());
22
- .to_owned();
23
-
24
- let full_canonical_path = std::fs::canonicalize(e.path())
19
+ let full_canonical_path = path_to_str(std::fs::canonicalize(e.path()).expect("Could not get canonical path"));
25
- .expect("Could not get canonical path")
26
- .to_str()
27
- .expect("Path does not have a string representation")
28
- .to_owned();
29
20
 
30
21
  let rel_path = if std::path::MAIN_SEPARATOR == '\\' {
31
22
  rel_path.replace('\\', "/")
@@ -36,3 +27,7 @@ pub fn get_files(folder_path: String) -> impl Iterator<Item = FileEntry> {
36
27
  FileEntry { rel_path, full_canonical_path }
37
28
  })
38
29
  }
30
+
31
+ pub fn path_to_str<P: AsRef<std::path::Path>>(p: P) -> String {
32
+ p.as_ref().to_str().expect("Path does not have a string representation").to_owned()
33
+ }