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


1164bd35 pyros2097

7 years ago
improve  embed and rocket example
Files changed (4) hide show
  1. Cargo.toml +1 -1
  2. examples/rocket.rs +8 -6
  3. readme.md +5 -4
  4. src/lib.rs +12 -10
Cargo.toml CHANGED
@@ -20,4 +20,4 @@ walkdir = "2.1.4"
20
20
  [dev-dependencies]
21
21
  rocket = "0.3.6"
22
22
  rocket_codegen = "0.3.6"
23
- rocket_contrib = "0.3.6"
23
+ rocket_contrib = "0.3.6"
examples/rocket.rs CHANGED
@@ -9,12 +9,12 @@ use std::ffi::OsStr;
9
9
  use std::io::Cursor;
10
10
  use rocket::response;
11
11
  use rocket::http::{ContentType, Status};
12
+ use rocket::State;
12
13
  use rust_embed::*;
13
14
 
14
15
  #[get("/")]
15
- fn index<'r>() -> response::Result<'r> {
16
+ fn index<'r>(asset: State<Asset>) -> response::Result<'r> {
16
- let asset = embed!("examples/public".to_owned());
17
- asset("/index.html".to_owned()).map_or_else(
17
+ asset("index.html".to_owned()).map_or_else(
18
18
  || Err(Status::NotFound),
19
19
  |d| {
20
20
  response::Response::build()
@@ -26,9 +26,8 @@ fn index<'r>() -> response::Result<'r> {
26
26
  }
27
27
 
28
28
  #[get("/dist/<file..>")]
29
- fn dist<'r>(file: PathBuf) -> response::Result<'r> {
29
+ fn dist<'r>(asset: State<Asset>, file: PathBuf) -> response::Result<'r> {
30
30
  let filename = file.display().to_string();
31
- let asset = embed!("examples/public/".to_owned());
32
31
  let ext = file.as_path().extension().and_then(OsStr::to_str).expect("Could not get file extension");
33
32
  let content_type = ContentType::from_extension(ext).expect("Could not get file content type");
34
33
  asset(filename.clone()).map_or_else(
@@ -43,5 +42,8 @@ fn dist<'r>(file: PathBuf) -> response::Result<'r> {
43
42
  }
44
43
 
45
44
  fn main() {
45
+ let asset = embed!("examples/public/".to_owned());
46
+ rocket::ignite()
47
+ .manage(asset)
46
- rocket::ignite().mount("/", routes![index, dist]).launch();
48
+ .mount("/", routes![index, dist]).launch();
47
49
  }
readme.md CHANGED
@@ -43,13 +43,14 @@ fn main() {
43
43
  ```
44
44
 
45
45
  ## Examples
46
- To run the examples,
46
+ To run the example in dev mode where it reads from the fs,
47
47
  `cargo run --example rocket`
48
-
48
+ To run the example in release mode where it read from binary,
49
+ `cargo run --release --example rocket`
49
50
  ## Testing
50
- debug: `cargo test --lib
51
+ debug: `cargo test --lib`
51
52
 
52
- release: `cargo test --lib --release
53
+ release: `cargo test --lib --release`
53
54
 
54
55
  Go Rusketeers!
55
56
  The power is yours!
src/lib.rs CHANGED
@@ -2,8 +2,10 @@
2
2
  extern crate log;
3
3
  extern crate walkdir;
4
4
 
5
+ pub type Asset = Box<Fn(String) -> Option<Vec<u8>> + std::marker::Sync + std::marker::Send>;
6
+
5
7
  #[cfg(debug_assertions)]
6
- pub fn generate_assets(parent_path: String) -> Box<Fn(String) -> Option<Vec<u8>>> {
8
+ pub fn generate_assets(parent_path: String) -> Asset {
7
9
  use std::fs::File;
8
10
  use std::path::Path;
9
11
  use std::io::Read;
@@ -32,7 +34,7 @@ pub fn generate_assets(parent_path: String) -> Box<Fn(String) -> Option<Vec<u8>>
32
34
  }
33
35
 
34
36
  #[cfg(not(debug_assertions))]
35
- pub fn generate_assets<'a>(parent_path: String) -> Box<Fn(String) -> Option<Vec<u8>>> {
37
+ pub fn generate_assets<'a>(parent_path: String) -> Asset {
36
38
  use std::fs::File;
37
39
  use std::io::Read;
38
40
  use std::path::Path;
@@ -72,16 +74,16 @@ mod tests {
72
74
  #[test]
73
75
  #[cfg(debug_assertions)]
74
76
  fn dev() {
75
- let asset = embed!("examples/public".to_owned());
77
+ let asset = embed!("examples/public/".to_owned());
76
- match asset("/index.html".to_owned()) {
78
+ match asset("index.html".to_owned()) {
77
79
  None => assert!(false, "index.html should exist"),
78
80
  _ => assert!(true),
79
81
  }
80
- match asset("/gg.html".to_owned()) {
82
+ match asset("gg.html".to_owned()) {
81
83
  Some(_) => assert!(false, "gg.html should not exist"),
82
84
  _ => assert!(true),
83
85
  }
84
- match asset("/images/llama.png".to_owned()) {
86
+ match asset("images/llama.png".to_owned()) {
85
87
  None => assert!(false, "llama.png should exist"),
86
88
  _ => assert!(true),
87
89
  }
@@ -90,16 +92,16 @@ mod tests {
90
92
  #[test]
91
93
  #[cfg(not(debug_assertions))]
92
94
  fn prod() {
93
- let asset = embed!("examples/public".to_owned());
95
+ let asset = embed!("examples/public/".to_owned());
94
- match asset("/index.html".to_owned()) {
96
+ match asset("index.html".to_owned()) {
95
97
  None => assert!(false, "index.html should exist"),
96
98
  _ => assert!(true),
97
99
  }
98
- match asset("/gg.html".to_owned()) {
100
+ match asset("gg.html".to_owned()) {
99
101
  Some(_) => assert!(false, "gg.html should not exist"),
100
102
  _ => assert!(true),
101
103
  }
102
- match asset("/images/llama.png".to_owned()) {
104
+ match asset("images/llama.png".to_owned()) {
103
105
  None => assert!(false, "llama.png should exist"),
104
106
  _ => assert!(true),
105
107
  }