~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 (1) hide show
  1. src/lib.rs +4 -17
src/lib.rs CHANGED
@@ -48,9 +48,6 @@ fn generate_assets(ident: &syn::Ident, folder_path: String) -> quote::Tokens {
48
48
 
49
49
  #[cfg(not(debug_assertions))]
50
50
  fn generate_assets(ident: &syn::Ident, folder_path: String) -> quote::Tokens {
51
- use std::fs::File;
52
- use std::io::Read;
53
- use std::path::Path;
54
51
  use walkdir::WalkDir;
55
52
  let mut values = Vec::<Tokens>::new();
56
53
  for entry in WalkDir::new(folder_path.clone())
@@ -60,21 +57,11 @@ fn generate_assets(ident: &syn::Ident, folder_path: String) -> quote::Tokens {
60
57
  {
61
58
  println!(" \x1b[92mCompiling\x1b[0m {}", entry.path().display());
62
59
  let base = &folder_path.clone();
63
- let key = String::from(
64
- entry
65
- .path()
66
- .to_str()
67
- .expect("Path does not have a string representation"),
60
+ let key = String::from(entry.path().to_str().expect("Path does not have a string representation")).replace(base, "");
68
- ).replace(base, "");
69
- let mut file = File::open(&Path::new(&entry.path())).unwrap_or_else(|e| {
61
+ let canonical_path = std::fs::canonicalize(entry.path()).expect("Could not get canonical path");
70
- panic!("could not open file -> {} {}", key, e);
71
- });
72
- let mut data: Vec<u8> = Vec::new();
62
+ let canonical_path_str = canonical_path.to_str();
73
- file.read_to_end(&mut data).unwrap_or_else(|e| {
74
- panic!("could not read file -> {} {}", key, e);
75
- });
76
63
  let value = quote!{
77
- #key => Some(vec!#data),
64
+ #key => Some(include_bytes!(#canonical_path_str).to_vec()),
78
65
  };
79
66
  values.push(value);
80
67
  }