~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.
91899db7
—
Peter John 2 years ago
Merge pull request #225 from ngalaiko/master
- impl/src/lib.rs +5 -1
- readme.md +1 -0
- tests/metadata.rs +10 -0
- utils/src/lib.rs +20 -2
impl/src/lib.rs
CHANGED
|
@@ -209,6 +209,10 @@ fn embed_file(folder_path: Option<&str>, ident: &syn::Ident, rel_path: &str, ful
|
|
|
209
209
|
Some(last_modified) => quote! { Some(#last_modified) },
|
|
210
210
|
None => quote! { None },
|
|
211
211
|
};
|
|
212
|
+
let created = match file.metadata.created() {
|
|
213
|
+
Some(created) => quote! { Some(#created) },
|
|
214
|
+
None => quote! { None },
|
|
215
|
+
};
|
|
212
216
|
#[cfg(feature = "mime-guess")]
|
|
213
217
|
let mimetype_tokens = {
|
|
214
218
|
let mt = file.metadata.mimetype();
|
|
@@ -241,7 +245,7 @@ fn embed_file(folder_path: Option<&str>, ident: &syn::Ident, rel_path: &str, ful
|
|
|
241
245
|
|
|
242
246
|
rust_embed::EmbeddedFile {
|
|
243
247
|
data: std::borrow::Cow::Borrowed(&BYTES),
|
|
244
|
-
metadata: rust_embed::Metadata::__rust_embed_new([#(#hash),*], #last_modified #mimetype_tokens)
|
|
248
|
+
metadata: rust_embed::Metadata::__rust_embed_new([#(#hash),*], #last_modified, #created #mimetype_tokens)
|
|
245
249
|
}
|
|
246
250
|
}
|
|
247
251
|
})
|
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
|
+
created: Option<u64>,
|
|
58
59
|
}
|
|
59
60
|
```
|
|
60
61
|
|
tests/metadata.rs
CHANGED
|
@@ -25,3 +25,13 @@ 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 create_is_accurate() {
|
|
31
|
+
let index_file: EmbeddedFile = Asset::get("index.html").expect("index.html exists");
|
|
32
|
+
|
|
33
|
+
let metadata = fs::metadata(format!("{}/examples/public/index.html", env!("CARGO_MANIFEST_DIR"))).unwrap();
|
|
34
|
+
let expected_datetime_utc = metadata.created().unwrap().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
|
|
35
|
+
|
|
36
|
+
assert_eq!(index_file.metadata.created(), Some(expected_datetime_utc));
|
|
37
|
+
}
|
utils/src/lib.rs
CHANGED
|
@@ -89,16 +89,20 @@ pub struct EmbeddedFile {
|
|
|
89
89
|
pub struct Metadata {
|
|
90
90
|
hash: [u8; 32],
|
|
91
91
|
last_modified: Option<u64>,
|
|
92
|
+
created: Option<u64>,
|
|
92
93
|
#[cfg(feature = "mime-guess")]
|
|
93
94
|
mimetype: Cow<'static, str>,
|
|
94
95
|
}
|
|
95
96
|
|
|
96
97
|
impl Metadata {
|
|
97
98
|
#[doc(hidden)]
|
|
99
|
+
pub const fn __rust_embed_new(
|
|
98
|
-
|
|
100
|
+
hash: [u8; 32], last_modified: Option<u64>, created: Option<u64>, #[cfg(feature = "mime-guess")] mimetype: &'static str,
|
|
101
|
+
) -> Self {
|
|
99
102
|
Self {
|
|
100
103
|
hash,
|
|
101
104
|
last_modified,
|
|
105
|
+
created,
|
|
102
106
|
#[cfg(feature = "mime-guess")]
|
|
103
107
|
mimetype: Cow::Borrowed(mimetype),
|
|
104
108
|
}
|
|
@@ -115,6 +119,12 @@ impl Metadata {
|
|
|
115
119
|
self.last_modified
|
|
116
120
|
}
|
|
117
121
|
|
|
122
|
+
/// The created data in seconds since the UNIX epoch. If the underlying
|
|
123
|
+
/// platform/file-system does not support this, None is returned.
|
|
124
|
+
pub fn created(&self) -> Option<u64> {
|
|
125
|
+
self.created
|
|
126
|
+
}
|
|
127
|
+
|
|
118
128
|
/// The mime type of the file
|
|
119
129
|
#[cfg(feature = "mime-guess")]
|
|
120
130
|
pub fn mimetype(&self) -> &str {
|
|
@@ -135,12 +145,19 @@ pub fn read_file_from_fs(file_path: &Path) -> io::Result<EmbeddedFile> {
|
|
|
135
145
|
Err(_) => None,
|
|
136
146
|
};
|
|
137
147
|
|
|
148
|
+
let metadata = fs::metadata(file_path)?;
|
|
138
|
-
let last_modified =
|
|
149
|
+
let last_modified = metadata.modified().ok().map(|last_modified| {
|
|
139
150
|
last_modified
|
|
140
151
|
.duration_since(SystemTime::UNIX_EPOCH)
|
|
141
152
|
.expect("Time before the UNIX epoch is unsupported")
|
|
142
153
|
.as_secs()
|
|
143
154
|
});
|
|
155
|
+
let created = metadata.created().ok().map(|created| {
|
|
156
|
+
created
|
|
157
|
+
.duration_since(SystemTime::UNIX_EPOCH)
|
|
158
|
+
.expect("Time before the UNIX epoch is unsupported")
|
|
159
|
+
.as_secs()
|
|
160
|
+
});
|
|
144
161
|
|
|
145
162
|
#[cfg(feature = "mime-guess")]
|
|
146
163
|
let mimetype = mime_guess::from_path(file_path).first_or_octet_stream().to_string();
|
|
@@ -150,6 +167,7 @@ pub fn read_file_from_fs(file_path: &Path) -> io::Result<EmbeddedFile> {
|
|
|
150
167
|
metadata: Metadata {
|
|
151
168
|
hash,
|
|
152
169
|
last_modified: source_date_epoch.or(last_modified),
|
|
170
|
+
created: source_date_epoch.or(created),
|
|
153
171
|
#[cfg(feature = "mime-guess")]
|
|
154
172
|
mimetype: mimetype.into(),
|
|
155
173
|
},
|