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


db3d1362 Peter

6 years ago
Merge pull request #67 from lopopolo/env-vars-in-folder-path
.travis.yml CHANGED
@@ -16,8 +16,10 @@ script:
16
16
  - cargo test --test lib
17
17
  - cargo test --test lib --features "debug-embed"
18
18
  - cargo test --test lib --release
19
+ - cargo test --test interpolated_path --features "interpolate-folder-path"
20
+ - cargo test --test interpolated_path --features "interpolate-folder-path" --release
19
21
  - cargo build --example basic
20
- - cargo build --example basic --release
22
+ - cargo build --example basic --release
21
23
  - cargo build --example actix --features actix
22
24
  - cargo build --example actix --features actix --release
23
25
  - cargo build --example warp --features warp-ex
Cargo.toml CHANGED
@@ -21,6 +21,7 @@ rocket = { version = "0.4.1", optional = true }
21
21
 
22
22
  [features]
23
23
  debug-embed = ["rust-embed-impl/debug-embed"]
24
+ interpolate-folder-path = ["rust-embed-impl/interpolate-folder-path"]
24
25
  nightly = ["rocket"]
25
26
  actix = ["actix-web", "mime_guess"]
26
27
  warp-ex = ["warp", "mime_guess"]
appveyor.yml CHANGED
@@ -133,6 +133,8 @@ test_script:
133
133
  - cargo test --test lib
134
134
  - cargo test --test lib --features "debug-embed"
135
135
  - cargo test --test lib --release
136
+ - cargo test --test interpolated_path --features "interpolate-folder-path"
137
+ - cargo test --test interpolated_path --features "interpolate-folder-path" --release
136
138
  - cargo build --example basic
137
139
  - cargo build --example basic --release
138
140
  - cargo build --example actix --features actix
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: '{}'",
src/lib.rs CHANGED
@@ -22,6 +22,8 @@ pub mod utils;
22
22
  /// #[derive(RustEmbed)]
23
23
  /// #[folder = "examples/public/"]
24
24
  /// struct Asset;
25
+ ///
26
+ /// fn main() {}
25
27
  /// ```
26
28
 
27
29
  pub trait RustEmbed {
tests/interpolated_path.rs ADDED
@@ -0,0 +1,47 @@
1
+ #[macro_use]
2
+ extern crate rust_embed;
3
+
4
+ /// Test doc comment
5
+ #[derive(RustEmbed)]
6
+ #[folder = "$CARGO_MANIFEST_DIR/examples/public/"]
7
+ struct Asset;
8
+
9
+ #[test]
10
+ fn get_works() {
11
+ match Asset::get("index.html") {
12
+ None => assert!(false, "index.html should exist"),
13
+ _ => assert!(true),
14
+ }
15
+ match Asset::get("gg.html") {
16
+ Some(_) => assert!(false, "gg.html should not exist"),
17
+ _ => assert!(true),
18
+ }
19
+ match Asset::get("images/llama.png") {
20
+ None => assert!(false, "llama.png should exist"),
21
+ _ => assert!(true),
22
+ }
23
+ }
24
+
25
+ #[test]
26
+ fn iter_works() {
27
+ let mut num_files = 0;
28
+ for file in Asset::iter() {
29
+ assert!(Asset::get(file.as_ref()).is_some());
30
+ num_files += 1;
31
+ }
32
+ assert_eq!(num_files, 6);
33
+ }
34
+
35
+ #[test]
36
+ fn trait_works_generic() {
37
+ trait_works_generic_helper::<Asset>();
38
+ }
39
+ fn trait_works_generic_helper<E: rust_embed::RustEmbed>() {
40
+ let mut num_files = 0;
41
+ for file in E::iter() {
42
+ assert!(E::get(file.as_ref()).is_some());
43
+ num_files += 1;
44
+ }
45
+ assert_eq!(num_files, 6);
46
+ assert!(E::get("gg.html").is_none(), "gg.html should not exist");
47
+ }