~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 (7) hide show
  1. .travis.yml +7 -7
  2. Cargo.toml +7 -3
  3. examples/basic.rs +1 -2
  4. examples/rocket.rs +2 -2
  5. readme.md +7 -7
  6. src/lib.rs +5 -12
  7. tests/lib.rs +2 -3
.travis.yml CHANGED
@@ -1,20 +1,20 @@
1
1
  language: rust
2
2
  sudo: false
3
3
  dist: trusty
4
+ rust:
5
+ - stable
6
+ - nightly
4
7
 
5
8
  cache:
6
9
  cargo: true
7
10
 
8
11
  matrix:
9
- include:
12
+ allow_failures:
10
- - rust: nightly
13
+ - rust: nightly
11
14
 
12
15
  script:
13
16
  - |
14
- cargo clean
15
- cargo test --tests --lib
17
+ cargo test --lib
16
- cargo test --tests --lib --release
18
+ cargo test --lib --release
17
- cargo clean
18
19
  cargo build --example basic
19
- cargo clean
20
20
  cargo build --release --example basic
Cargo.toml CHANGED
@@ -18,13 +18,17 @@ syn = "0.11"
18
18
  quote = "0.3"
19
19
  walkdir = "2.1.4"
20
20
 
21
+ rocket = { version = "0.3.6", optional = true }
22
+ rocket_codegen = { version = "0.3.6", optional = true }
23
+ rocket_contrib = { version = "0.3.6", optional = true }
24
+
21
25
  [dev-dependencies]
22
- rocket = "0.3.6"
23
- rocket_codegen = "0.3.6"
24
- rocket_contrib = "0.3.6"
25
26
  rouille = "2.1.0"
26
27
  fern = "0.5"
27
28
 
29
+ [features]
30
+ nightly = ["rocket", "rocket_codegen", "rocket_contrib"]
31
+
28
32
  [badges]
29
33
  appveyor = { repository = "pyros2097/rust-embed" }
30
34
  travis-ci = { repository = "pyros2097/rust-embed" }
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() {
@@ -46,6 +41,11 @@ To run the example in dev mode where it reads from the fs,
46
41
  To run the example in release mode where it reads from binary,
47
42
 
48
43
  `cargo run --release --example basic`
44
+
45
+ Note: To run the `rocket` example, add the `nightly` feature flag and run on a nightly build:
46
+
47
+ `cargo +nightly run --example rocket --features nightly`
48
+
49
49
  ## Testing
50
50
  debug: `cargo test --tests --lib`
51
51
 
src/lib.rs CHANGED
@@ -76,7 +76,7 @@ fn generate_assets(ident: &syn::Ident, folder_path: String) -> quote::Tokens {
76
76
  }
77
77
 
78
78
  fn help() {
79
- panic!("#[derive(RustEmbed)] should contain one attribute like this #[folder(\"examples/public/\")]");
79
+ panic!("#[derive(RustEmbed)] should contain one attribute like this #[folder = \"examples/public/\"]");
80
80
  }
81
81
 
82
82
  fn impl_rust_embed(ast: &syn::DeriveInput) -> Tokens {
@@ -92,10 +92,10 @@ fn impl_rust_embed(ast: &syn::DeriveInput) -> Tokens {
92
92
  help();
93
93
  }
94
94
  let value = &ast.attrs[0].value;
95
- let items = match value {
95
+ let literal_value = match value {
96
- &MetaItem::List(ref attr_name, ref items) => {
96
+ &MetaItem::NameValue(ref attr_name, ref value) => {
97
97
  if attr_name == "folder" {
98
- items
98
+ value
99
99
  } else {
100
100
  panic!("#[derive(RustEmbed)] attribute name must be folder");
101
101
  }
@@ -104,14 +104,7 @@ fn impl_rust_embed(ast: &syn::DeriveInput) -> Tokens {
104
104
  panic!("#[derive(RustEmbed)] attribute name must be folder");
105
105
  }
106
106
  };
107
- let item = &items[0];
108
- let lit = match item {
109
- &NestedMetaItem::Literal(ref l) => l,
110
- _ => {
111
- panic!("Hello");
112
- }
113
- };
114
- let folder_path = match lit {
107
+ let folder_path = match literal_value {
115
108
  &Lit::Str(ref val, _) => val.clone(),
116
109
  _ => {
117
110
  panic!("#[derive(RustEmbed)] attribute value must be a string literal");
tests/lib.rs CHANGED
@@ -1,4 +1,3 @@
1
- #![feature(attr_literals)]
2
1
  #[macro_use]
3
2
  extern crate rust_embed;
4
3
 
@@ -6,7 +5,7 @@ extern crate rust_embed;
6
5
  #[cfg(debug_assertions)]
7
6
  fn dev() {
8
7
  #[derive(RustEmbed)]
9
- #[folder("examples/public/")]
8
+ #[folder = "examples/public/"]
10
9
  struct Asset;
11
10
 
12
11
  match Asset::get("index.html") {
@@ -27,7 +26,7 @@ fn dev() {
27
26
  #[cfg(not(debug_assertions))]
28
27
  fn prod() {
29
28
  #[derive(RustEmbed)]
30
- #[folder("examples/public/")]
29
+ #[folder = "examples/public/"]
31
30
  struct Asset;
32
31
 
33
32
  match Asset::get("index.html") {