~repos /rust-embed
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.
5a7a548d
—
pyros2097 7 years ago
get impl working
- readme.md +12 -1
- src/lib.rs +21 -18
readme.md
CHANGED
|
@@ -27,12 +27,23 @@ It exposes a function to serve all files stored in your assets folder which is u
|
|
|
27
27
|
system so that it doesn't take to much time to compile;]
|
|
28
28
|
|
|
29
29
|
```rust
|
|
30
|
-
asset(path: String) -> Option<
|
|
30
|
+
asset(path: String) -> Option<Vec<u8>>
|
|
31
|
+
|
|
32
|
+
let asset = embed!("examples/public".to_owned());
|
|
33
|
+
match asset("/index.html".to_owned()) {
|
|
34
|
+
None => assert!(false, "index.html should exist"),
|
|
35
|
+
_ => assert!(true),
|
|
36
|
+
}
|
|
31
37
|
```
|
|
32
38
|
|
|
33
39
|
## Examples
|
|
34
40
|
To run the examples,
|
|
35
41
|
`cargo run --example hyper`
|
|
36
42
|
|
|
43
|
+
## Testing
|
|
44
|
+
debug: `cargo test --lib -- --nocapture`
|
|
45
|
+
|
|
46
|
+
release: `cargo test --lib --release -- --nocapture`
|
|
47
|
+
|
|
37
48
|
Go Rusketeers!
|
|
38
49
|
The power is yours!
|
src/lib.rs
CHANGED
|
@@ -3,26 +3,28 @@ extern crate log;
|
|
|
3
3
|
extern crate walkdir;
|
|
4
4
|
|
|
5
5
|
#[cfg(debug_assertions)]
|
|
6
|
-
pub fn generate_assets(parent_path: String) -> Box<Fn(String) -> Option<
|
|
6
|
+
pub fn generate_assets(parent_path: String) -> Box<Fn(String) -> Option<Vec<u8>>> {
|
|
7
7
|
use std::fs::File;
|
|
8
8
|
use std::path::Path;
|
|
9
9
|
use std::io::Read;
|
|
10
10
|
info!("rust-embed: loading folder -> {}", parent_path);
|
|
11
11
|
Box::new(move |file_path| {
|
|
12
|
-
let
|
|
12
|
+
let name = &format!("{}{}", parent_path, file_path);
|
|
13
|
+
let path = &Path::new(name);
|
|
14
|
+
let key = String::from(path.to_str().expect("Path does not have a string representation"));
|
|
13
|
-
info!("rust-embed: asset from file -> {}",
|
|
15
|
+
info!("rust-embed: asset from file -> {}", key);
|
|
14
|
-
let mut file = match File::open(
|
|
16
|
+
let mut file = match File::open(path) {
|
|
15
17
|
Ok(mut file) => file,
|
|
16
18
|
Err(e) => {
|
|
17
|
-
|
|
19
|
+
error!("rust-embed: could not open file -> {} {}", key, e);
|
|
18
20
|
return None
|
|
19
21
|
}
|
|
20
22
|
};
|
|
21
|
-
let mut
|
|
23
|
+
let mut data: Vec<u8> = Vec::new();
|
|
22
|
-
match file.
|
|
24
|
+
match file.read_to_end(&mut data) {
|
|
23
|
-
Ok(_) => Some(
|
|
25
|
+
Ok(_) => Some(data),
|
|
24
26
|
Err(e) => {
|
|
25
|
-
|
|
27
|
+
error!("rust-embed: could not open file -> {} {}", key, e);
|
|
26
28
|
return None
|
|
27
29
|
}
|
|
28
30
|
}
|
|
@@ -30,30 +32,31 @@ pub fn generate_assets(parent_path: String) -> Box<Fn(String) -> Option<String>>
|
|
|
30
32
|
}
|
|
31
33
|
|
|
32
34
|
#[cfg(not(debug_assertions))]
|
|
33
|
-
pub fn generate_assets(parent_path: String) -> Box<Fn(String) -> Option<
|
|
35
|
+
pub fn generate_assets<'a>(parent_path: String) -> Box<Fn(String) -> Option<Vec<u8>>> {
|
|
34
36
|
use std::fs::File;
|
|
35
37
|
use std::io::Read;
|
|
36
38
|
use std::path::Path;
|
|
37
39
|
use walkdir::WalkDir;
|
|
38
40
|
use std::collections::HashMap;
|
|
39
41
|
|
|
40
|
-
|
|
42
|
+
info!("rust-embed: loading folder -> {}", parent_path);
|
|
41
43
|
let mut map = HashMap::new();
|
|
42
|
-
for entry in WalkDir::new(parent_path).into_iter().filter_map(|e| e.ok()).filter(|e| e.file_type().is_file()) {
|
|
44
|
+
for entry in WalkDir::new(parent_path.clone()).into_iter().filter_map(|e| e.ok()).filter(|e| e.file_type().is_file()) {
|
|
43
|
-
|
|
45
|
+
info!("rust-embed: asset from file -> {}", entry.path().display());
|
|
46
|
+
let base = &parent_path.clone();
|
|
44
|
-
let key = String::from(entry.path().to_str().expect("Path does not have a string representation"));
|
|
47
|
+
let key = String::from(entry.path().to_str().expect("Path does not have a string representation")).replace(base, "");
|
|
45
48
|
let mut file = File::open(&Path::new(&entry.path())).unwrap_or_else(|e| {
|
|
46
49
|
panic!("rust-embed: could not open file -> {} {}", key, e);
|
|
47
50
|
});
|
|
48
|
-
let mut
|
|
51
|
+
let mut data: Vec<u8> = Vec::new();
|
|
49
|
-
file.
|
|
52
|
+
file.read_to_end(&mut data).unwrap_or_else(|e| {
|
|
50
53
|
panic!("rust-embed: could not read file -> {} {}", key, e);
|
|
51
54
|
});
|
|
52
|
-
map.insert(key,
|
|
55
|
+
map.insert(key, data);
|
|
53
56
|
}
|
|
54
57
|
Box::new(move |file_path| {
|
|
55
58
|
match map.get(&file_path) {
|
|
56
|
-
Some(s) => Some(s.
|
|
59
|
+
Some(s) => Some(s.to_vec()),
|
|
57
60
|
None => None,
|
|
58
61
|
}
|
|
59
62
|
})
|