~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.
00066480
—
Peter John 3 years ago
Merge pull request #193 from jaztec/master
- impl/src/lib.rs +14 -4
- readme.md +1 -0
- tests/interpolated_path.rs +2 -2
- tests/lib.rs +3 -2
- tests/metadata.rs +11 -0
- utils/src/lib.rs +17 -5
impl/src/lib.rs
CHANGED
|
@@ -17,7 +17,12 @@ fn embedded(ident: &syn::Ident, folder_path: String, prefix: Option<&str>, inclu
|
|
|
17
17
|
|
|
18
18
|
let includes: Vec<&str> = includes.iter().map(AsRef::as_ref).collect();
|
|
19
19
|
let excludes: Vec<&str> = excludes.iter().map(AsRef::as_ref).collect();
|
|
20
|
+
for rust_embed_utils::FileEntry {
|
|
21
|
+
rel_path,
|
|
22
|
+
full_canonical_path,
|
|
23
|
+
is_dir: _,
|
|
20
|
-
|
|
24
|
+
} in rust_embed_utils::get_files(folder_path, &includes, &excludes)
|
|
25
|
+
{
|
|
21
26
|
match_values.push(embed_file(&rel_path, &full_canonical_path));
|
|
22
27
|
|
|
23
28
|
list_values.push(if let Some(prefix) = prefix {
|
|
@@ -170,20 +175,25 @@ fn generate_assets(ident: &syn::Ident, folder_path: String, prefix: Option<Strin
|
|
|
170
175
|
fn embed_file(rel_path: &str, full_canonical_path: &str) -> TokenStream2 {
|
|
171
176
|
let file = rust_embed_utils::read_file_from_fs(Path::new(full_canonical_path)).expect("File should be readable");
|
|
172
177
|
let hash = file.metadata.sha256_hash();
|
|
178
|
+
let is_dir = file.metadata.is_dir();
|
|
173
179
|
let last_modified = match file.metadata.last_modified() {
|
|
174
180
|
Some(last_modified) => quote! { Some(#last_modified) },
|
|
175
181
|
None => quote! { None },
|
|
176
182
|
};
|
|
177
183
|
|
|
178
|
-
let embedding_code = if cfg!(feature = "compression") {
|
|
184
|
+
let embedding_code = if cfg!(feature = "compression") && !is_dir {
|
|
179
185
|
quote! {
|
|
180
186
|
rust_embed::flate!(static FILE: [u8] from #full_canonical_path);
|
|
181
187
|
let bytes = &FILE[..];
|
|
182
188
|
}
|
|
183
|
-
} else {
|
|
189
|
+
} else if !is_dir {
|
|
184
190
|
quote! {
|
|
185
191
|
let bytes = &include_bytes!(#full_canonical_path)[..];
|
|
186
192
|
}
|
|
193
|
+
} else {
|
|
194
|
+
quote! {
|
|
195
|
+
let bytes = Vec::new();
|
|
196
|
+
}
|
|
187
197
|
};
|
|
188
198
|
|
|
189
199
|
quote! {
|
|
@@ -192,7 +202,7 @@ fn embed_file(rel_path: &str, full_canonical_path: &str) -> TokenStream2 {
|
|
|
192
202
|
|
|
193
203
|
Some(rust_embed::EmbeddedFile {
|
|
194
204
|
data: std::borrow::Cow::from(bytes),
|
|
195
|
-
metadata: rust_embed::Metadata::__rust_embed_new([#(#hash),*], #last_modified)
|
|
205
|
+
metadata: rust_embed::Metadata::__rust_embed_new([#(#hash),*], #last_modified, #is_dir),
|
|
196
206
|
})
|
|
197
207
|
}
|
|
198
208
|
}
|
readme.md
CHANGED
|
@@ -55,6 +55,7 @@ pub struct EmbeddedFile {
|
|
|
55
55
|
pub struct Metadata {
|
|
56
56
|
hash: [u8; 32],
|
|
57
57
|
last_modified: Option<u64>,
|
|
58
|
+
is_dir: bool,
|
|
58
59
|
}
|
|
59
60
|
```
|
|
60
61
|
|
tests/interpolated_path.rs
CHANGED
|
@@ -19,7 +19,7 @@ fn iter_works() {
|
|
|
19
19
|
assert!(Asset::get(file.as_ref()).is_some());
|
|
20
20
|
num_files += 1;
|
|
21
21
|
}
|
|
22
|
-
assert_eq!(num_files,
|
|
22
|
+
assert_eq!(num_files, 8);
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
#[test]
|
|
@@ -32,6 +32,6 @@ fn trait_works_generic_helper<E: rust_embed::RustEmbed>() {
|
|
|
32
32
|
assert!(E::get(file.as_ref()).is_some());
|
|
33
33
|
num_files += 1;
|
|
34
34
|
}
|
|
35
|
-
assert_eq!(num_files,
|
|
35
|
+
assert_eq!(num_files, 8);
|
|
36
36
|
assert!(E::get("gg.html").is_none(), "gg.html should not exist");
|
|
37
37
|
}
|
tests/lib.rs
CHANGED
|
@@ -10,6 +10,7 @@ fn get_works() {
|
|
|
10
10
|
assert!(Asset::get("index.html").is_some(), "index.html should exist");
|
|
11
11
|
assert!(Asset::get("gg.html").is_none(), "gg.html should not exist");
|
|
12
12
|
assert!(Asset::get("images/llama.png").is_some(), "llama.png should exist");
|
|
13
|
+
assert!(Asset::get("images").is_some(), "images should exist");
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
/// Using Windows-style path separators (`\`) is acceptable
|
|
@@ -28,7 +29,7 @@ fn iter_works() {
|
|
|
28
29
|
assert!(Asset::get(file.as_ref()).is_some());
|
|
29
30
|
num_files += 1;
|
|
30
31
|
}
|
|
31
|
-
assert_eq!(num_files,
|
|
32
|
+
assert_eq!(num_files, 8);
|
|
32
33
|
}
|
|
33
34
|
|
|
34
35
|
#[test]
|
|
@@ -41,6 +42,6 @@ fn trait_works_generic_helper<E: rust_embed::RustEmbed>() {
|
|
|
41
42
|
assert!(E::get(file.as_ref()).is_some());
|
|
42
43
|
num_files += 1;
|
|
43
44
|
}
|
|
44
|
-
assert_eq!(num_files,
|
|
45
|
+
assert_eq!(num_files, 8);
|
|
45
46
|
assert!(E::get("gg.html").is_none(), "gg.html should not exist");
|
|
46
47
|
}
|
tests/metadata.rs
CHANGED
|
@@ -25,3 +25,14 @@ fn last_modified_is_accurate() {
|
|
|
25
25
|
|
|
26
26
|
assert_eq!(index_file.metadata.last_modified(), Some(expected_datetime_utc));
|
|
27
27
|
}
|
|
28
|
+
|
|
29
|
+
#[test]
|
|
30
|
+
fn is_dir_is_accurate() {
|
|
31
|
+
let index_file: EmbeddedFile = Asset::get("index.html").expect("index.html exists");
|
|
32
|
+
let doc_file: EmbeddedFile = Asset::get("images/doc.txt").expect("doc.txt exists");
|
|
33
|
+
let images_folder: EmbeddedFile = Asset::get("images").expect("images exists");
|
|
34
|
+
|
|
35
|
+
assert_eq!(index_file.metadata.is_dir(), false);
|
|
36
|
+
assert_eq!(doc_file.metadata.is_dir(), false);
|
|
37
|
+
assert!(images_folder.metadata.is_dir());
|
|
38
|
+
}
|
utils/src/lib.rs
CHANGED
|
@@ -10,6 +10,7 @@ use std::{fs, io};
|
|
|
10
10
|
pub struct FileEntry {
|
|
11
11
|
pub rel_path: String,
|
|
12
12
|
pub full_canonical_path: String,
|
|
13
|
+
pub is_dir: bool,
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
#[cfg(not(feature = "include-exclude"))]
|
|
@@ -58,10 +59,10 @@ pub fn get_files<'patterns>(folder_path: String, includes: &'patterns [&str], ex
|
|
|
58
59
|
.sort_by_file_name()
|
|
59
60
|
.into_iter()
|
|
60
61
|
.filter_map(|e| e.ok())
|
|
61
|
-
.filter(|e| e.file_type().is_file())
|
|
62
62
|
.filter_map(move |e| {
|
|
63
63
|
let rel_path = path_to_str(e.path().strip_prefix(&folder_path).unwrap());
|
|
64
64
|
let full_canonical_path = path_to_str(std::fs::canonicalize(e.path()).expect("Could not get canonical path"));
|
|
65
|
+
let is_dir = e.file_type().is_dir();
|
|
65
66
|
|
|
66
67
|
let rel_path = if std::path::MAIN_SEPARATOR == '\\' {
|
|
67
68
|
rel_path.replace('\\', "/")
|
|
@@ -70,7 +71,11 @@ pub fn get_files<'patterns>(folder_path: String, includes: &'patterns [&str], ex
|
|
|
70
71
|
};
|
|
71
72
|
|
|
72
73
|
if is_path_included(&rel_path, includes, excludes) {
|
|
74
|
+
Some(FileEntry {
|
|
75
|
+
rel_path,
|
|
73
|
-
|
|
76
|
+
full_canonical_path,
|
|
77
|
+
is_dir,
|
|
78
|
+
})
|
|
74
79
|
} else {
|
|
75
80
|
None
|
|
76
81
|
}
|
|
@@ -87,12 +92,13 @@ pub struct EmbeddedFile {
|
|
|
87
92
|
pub struct Metadata {
|
|
88
93
|
hash: [u8; 32],
|
|
89
94
|
last_modified: Option<u64>,
|
|
95
|
+
is_dir: bool,
|
|
90
96
|
}
|
|
91
97
|
|
|
92
98
|
impl Metadata {
|
|
93
99
|
#[doc(hidden)]
|
|
94
|
-
pub fn __rust_embed_new(hash: [u8; 32], last_modified: Option<u64>) -> Self {
|
|
100
|
+
pub fn __rust_embed_new(hash: [u8; 32], last_modified: Option<u64>, is_dir: bool) -> Self {
|
|
95
|
-
Self { hash, last_modified }
|
|
101
|
+
Self { hash, last_modified, is_dir }
|
|
96
102
|
}
|
|
97
103
|
|
|
98
104
|
/// The SHA256 hash of the file
|
|
@@ -105,10 +111,15 @@ impl Metadata {
|
|
|
105
111
|
pub fn last_modified(&self) -> Option<u64> {
|
|
106
112
|
self.last_modified
|
|
107
113
|
}
|
|
114
|
+
|
|
115
|
+
/// Check if an entry is a directory
|
|
116
|
+
pub fn is_dir(&self) -> bool {
|
|
117
|
+
self.is_dir
|
|
118
|
+
}
|
|
108
119
|
}
|
|
109
120
|
|
|
110
121
|
pub fn read_file_from_fs(file_path: &Path) -> io::Result<EmbeddedFile> {
|
|
111
|
-
let data = fs::read(file_path)?;
|
|
122
|
+
let data = if !file_path.is_dir() { fs::read(file_path)? } else { Vec::new() };
|
|
112
123
|
let data = Cow::from(data);
|
|
113
124
|
|
|
114
125
|
let mut hasher = sha2::Sha256::new();
|
|
@@ -132,6 +143,7 @@ pub fn read_file_from_fs(file_path: &Path) -> io::Result<EmbeddedFile> {
|
|
|
132
143
|
metadata: Metadata {
|
|
133
144
|
hash,
|
|
134
145
|
last_modified: source_date_epoch.or(last_modified),
|
|
146
|
+
is_dir: fs::metadata(file_path)?.is_dir(),
|
|
135
147
|
},
|
|
136
148
|
})
|
|
137
149
|
}
|