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