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


547bcc09 Mcat12

7 years ago
Compile on stable Rust versions
Files changed (6) hide show
  1. .travis.yml +1 -0
  2. examples/basic.rs +1 -2
  3. examples/rocket.rs +2 -2
  4. readme.md +2 -7
  5. src/lib.rs +5 -12
  6. tests/lib.rs +2 -5
.travis.yml CHANGED
@@ -8,6 +8,7 @@ cache:
8
8
  matrix:
9
9
  include:
10
10
  - rust: nightly
11
+ - rust: 1.26
11
12
 
12
13
  script:
13
14
  - |
examples/basic.rs CHANGED
@@ -1,9 +1,8 @@
1
- #![feature(attr_literals)]
2
1
  #[macro_use]
3
2
  extern crate rust_embed;
4
3
 
5
4
  #[derive(RustEmbed)]
6
- #[folder("examples/public/")]
5
+ #[folder = "examples/public/"]
7
6
  struct Asset;
8
7
 
9
8
  fn main() {
examples/rocket.rs CHANGED
@@ -1,4 +1,4 @@
1
- #![feature(test, plugin, decl_macro, attr_literals)]
1
+ #![feature(test, plugin, decl_macro)]
2
2
  #![plugin(rocket_codegen)]
3
3
  extern crate rocket;
4
4
  extern crate rocket_contrib;
@@ -12,7 +12,7 @@ use rocket::response;
12
12
  use rocket::http::{ContentType, Status};
13
13
 
14
14
  #[derive(RustEmbed)]
15
- #[folder("examples/public/")]
15
+ #[folder = "examples/public/"]
16
16
  struct Asset;
17
17
 
18
18
  #[get("/")]
readme.md CHANGED
@@ -13,23 +13,18 @@ rust-embed="2.0.0"
13
13
  ## Documentation
14
14
  Declare a struct name it Asset or something and add an attribute `folder` to it which has the path to your static folder.
15
15
  ```rust
16
- #![feature(attr_literals)]
17
-
18
16
  #[derive(RustEmbed)]
19
- #[folder("examples/public/")]
17
+ #[folder = "examples/public/"]
20
18
  struct Asset;
21
19
  ```
22
20
 
23
21
  ## Usage
24
22
  ```rust
25
- #![feature(attr_literals)]
26
23
  #[macro_use]
27
24
  extern crate rust_embed;
28
- #[macro_use]
29
- extern crate log;
30
25
 
31
26
  #[derive(RustEmbed)]
32
- #[folder("examples/public/")]
27
+ #[folder = "examples/public/"]
33
28
  struct Asset;
34
29
 
35
30
  fn main() {
src/lib.rs CHANGED
@@ -78,7 +78,7 @@ fn generate_assets(ident: &syn::Ident, folder_path: String) -> quote::Tokens {
78
78
  }
79
79
 
80
80
  fn help() {
81
- panic!("#[derive(RustEmbed)] should contain one attribute like this #[folder(\"examples/public/\")]");
81
+ panic!("#[derive(RustEmbed)] should contain one attribute like this #[folder = \"examples/public/\"]");
82
82
  }
83
83
 
84
84
  fn impl_rust_embed(ast: &syn::DeriveInput) -> Tokens {
@@ -94,10 +94,10 @@ fn impl_rust_embed(ast: &syn::DeriveInput) -> Tokens {
94
94
  help();
95
95
  }
96
96
  let value = &ast.attrs[0].value;
97
- let items = match value {
97
+ let literal_value = match value {
98
- &MetaItem::List(ref attr_name, ref items) => {
98
+ &MetaItem::NameValue(ref attr_name, ref value) => {
99
99
  if attr_name == "folder" {
100
- items
100
+ value
101
101
  } else {
102
102
  panic!("#[derive(RustEmbed)] attribute name must be folder");
103
103
  }
@@ -106,14 +106,7 @@ fn impl_rust_embed(ast: &syn::DeriveInput) -> Tokens {
106
106
  panic!("#[derive(RustEmbed)] attribute name must be folder");
107
107
  }
108
108
  };
109
- let item = &items[0];
110
- let lit = match item {
111
- &NestedMetaItem::Literal(ref l) => l,
112
- _ => {
113
- panic!("Hello");
114
- }
115
- };
116
- let folder_path = match lit {
109
+ let folder_path = match literal_value {
117
110
  &Lit::Str(ref val, _) => val.clone(),
118
111
  _ => {
119
112
  panic!("#[derive(RustEmbed)] attribute value must be a string literal");
tests/lib.rs CHANGED
@@ -1,6 +1,3 @@
1
- #![feature(attr_literals)]
2
- #[macro_use]
3
- extern crate log;
4
1
  #[macro_use]
5
2
  extern crate rust_embed;
6
3
 
@@ -8,7 +5,7 @@ extern crate rust_embed;
8
5
  #[cfg(debug_assertions)]
9
6
  fn dev() {
10
7
  #[derive(RustEmbed)]
11
- #[folder("examples/public/")]
8
+ #[folder = "examples/public/"]
12
9
  struct Asset;
13
10
 
14
11
  match Asset::get("index.html") {
@@ -29,7 +26,7 @@ fn dev() {
29
26
  #[cfg(not(debug_assertions))]
30
27
  fn prod() {
31
28
  #[derive(RustEmbed)]
32
- #[folder("examples/public/")]
29
+ #[folder = "examples/public/"]
33
30
  struct Asset;
34
31
 
35
32
  match Asset::get("index.html") {