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


f3cf632d Ryan Lopopolo

6 years ago
Interpolate environment variables in folder path
Files changed (2) hide show
  1. impl/Cargo.toml +5 -0
  2. impl/src/lib.rs +9 -7
impl/Cargo.toml CHANGED
@@ -18,5 +18,10 @@ syn = "0.11"
18
18
  quote = "0.3"
19
19
  walkdir = "2.2.7"
20
20
 
21
+ [dependencies.shellexpand]
22
+ version = "1.0"
23
+ optional = true
24
+
21
25
  [features]
22
26
  debug-embed = []
27
+ interpolate-folder-path = ["shellexpand"]
impl/src/lib.rs CHANGED
@@ -4,6 +4,8 @@ extern crate proc_macro;
4
4
  extern crate quote;
5
5
  extern crate syn;
6
6
 
7
+ #[cfg(feature = "interpolate-folder-path")]
8
+ extern crate shellexpand;
7
9
  extern crate walkdir;
8
10
 
9
11
  use proc_macro::TokenStream;
@@ -15,7 +17,7 @@ mod utils;
15
17
 
16
18
  #[cfg(all(debug_assertions, not(feature = "debug-embed")))]
17
19
  fn generate_assets(ident: &syn::Ident, folder_path: String) -> quote::Tokens {
18
- quote! {
20
+ quote! {
19
21
  impl #ident {
20
22
  pub fn get(file_path: &str) -> Option<std::borrow::Cow<'static, [u8]>> {
21
23
  use std::fs::File;
@@ -75,7 +77,7 @@ fn generate_assets(ident: &syn::Ident, folder_path: String) -> quote::Tokens {
75
77
 
76
78
  let array_len = list_values.len();
77
79
 
78
- quote! {
80
+ quote! {
79
81
  impl #ident {
80
82
  pub fn get(file_path: &str) -> Option<std::borrow::Cow<'static, [u8]>> {
81
83
  match file_path {
@@ -116,13 +118,10 @@ fn impl_rust_embed(ast: &syn::DeriveInput) -> Tokens {
116
118
  },
117
119
  };
118
120
 
119
- let attribute = ast.attrs
120
- .iter()
121
- .map(|attr| &attr.value)
122
- .find(|value| value.name() == "folder");
121
+ let attribute = ast.attrs.iter().map(|attr| &attr.value).find(|value| value.name() == "folder");
123
122
  let literal_value = match attribute {
124
123
  Some(&MetaItem::NameValue(_, ref literal)) => literal,
125
- _ => help()
124
+ _ => help(),
126
125
  };
127
126
  let folder_path = match literal_value {
128
127
  &Lit::Str(ref val, _) => val.clone(),
@@ -131,6 +130,9 @@ fn impl_rust_embed(ast: &syn::DeriveInput) -> Tokens {
131
130
  }
132
131
  };
133
132
 
133
+ #[cfg(feature = "interpolate-folder-path")]
134
+ let folder_path = shellexpand::full(&folder_path).unwrap().to_string();
135
+
134
136
  if !Path::new(&folder_path).exists() {
135
137
  panic!(
136
138
  "#[derive(RustEmbed)] folder '{}' does not exist. cwd: '{}'",