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


38da804c Mcat12

5 years ago
Add compression feature with include_flate
Files changed (4) hide show
  1. Cargo.toml +2 -0
  2. impl/Cargo.toml +1 -0
  3. impl/src/lib.rs +23 -6
  4. src/lib.rs +5 -0
Cargo.toml CHANGED
@@ -15,6 +15,7 @@ walkdir = "2.2.7"
15
15
  rust-embed-impl = { version = "5.2.0", path = "impl"}
16
16
  rust-embed-utils = { version = "5.0.0", path = "utils"}
17
17
 
18
+ include-flate = { version = "0.1", optional = true, features = ["stable"] }
18
19
  actix-web = { version = "1", optional = true }
19
20
  mime_guess = { version = "2", optional = true }
20
21
  warp = { version = "0.1", optional = true }
@@ -23,6 +24,7 @@ rocket = { version = "0.4.2", optional = true }
23
24
  [features]
24
25
  debug-embed = ["rust-embed-impl/debug-embed", "rust-embed-utils/debug-embed"]
25
26
  interpolate-folder-path = ["rust-embed-impl/interpolate-folder-path"]
27
+ compression = ["rust-embed-impl/compression", "include-flate"]
26
28
  nightly = ["rocket"]
27
29
  actix = ["actix-web", "mime_guess"]
28
30
  warp-ex = ["warp", "mime_guess"]
impl/Cargo.toml CHANGED
@@ -27,3 +27,4 @@ optional = true
27
27
  [features]
28
28
  debug-embed = []
29
29
  interpolate-folder-path = ["shellexpand"]
30
+ compression = []
impl/src/lib.rs CHANGED
@@ -54,6 +54,28 @@ fn generate_assets(ident: &syn::Ident, folder_path: String) -> TokenStream2 {
54
54
  }
55
55
  }
56
56
 
57
+ #[cfg(all(not(feature = "compression"), any(not(debug_assertions), feature = "debug-embed")))]
58
+ fn embed_file(rel_path: &str, full_canonical_path: &str) -> TokenStream2 {
59
+ quote! {
60
+ #rel_path => {
61
+ let bytes = &include_bytes!(#full_canonical_path)[..];
62
+ Some(std::borrow::Cow::from(bytes))
63
+ },
64
+ }
65
+ }
66
+
67
+ #[cfg(all(feature = "compression", any(not(debug_assertions), feature = "debug-embed")))]
68
+ fn embed_file(rel_path: &str, full_canonical_path: &str) -> TokenStream2 {
69
+ quote! {
70
+ #rel_path => {
71
+ rust_embed::flate!(static FILE: [u8] from #full_canonical_path);
72
+
73
+ let bytes = &FILE[..];
74
+ Some(std::borrow::Cow::from(bytes))
75
+ },
76
+ }
77
+ }
78
+
57
79
  #[cfg(any(not(debug_assertions), feature = "debug-embed"))]
58
80
  fn generate_assets(ident: &syn::Ident, folder_path: String) -> TokenStream2 {
59
81
  extern crate rust_embed_utils;
@@ -62,12 +84,7 @@ fn generate_assets(ident: &syn::Ident, folder_path: String) -> TokenStream2 {
62
84
  let mut list_values = Vec::<String>::new();
63
85
 
64
86
  for rust_embed_utils::FileEntry { rel_path, full_canonical_path } in rust_embed_utils::get_files(folder_path) {
65
- match_values.push(quote! {
66
- #rel_path => {
67
- let bytes = &include_bytes!(#full_canonical_path)[..];
87
+ match_values.push(embed_file(&rel_path, &full_canonical_path));
68
- Some(std::borrow::Cow::from(bytes))
69
- },
70
- });
71
88
  list_values.push(rel_path);
72
89
  }
73
90
 
src/lib.rs CHANGED
@@ -1,6 +1,11 @@
1
1
  #[cfg(all(debug_assertions, not(feature = "debug-embed")))]
2
2
  extern crate walkdir;
3
3
 
4
+ #[cfg(feature = "compression")]
5
+ extern crate include_flate;
6
+ #[cfg(feature = "compression")]
7
+ pub use include_flate::flate;
8
+
4
9
  #[allow(unused_imports)]
5
10
  #[macro_use]
6
11
  extern crate rust_embed_impl;