~repos /rust-embed

#rust#proc-macro#http

git clone https://pyrossh.dev/repos/rust-embed.git
Discussions: https://groups.google.com/g/rust-embed-devs

rust macro which loads files into the rust binary at compile time during release and loads the file from the fs during dev.


Files changed (5) hide show
  1. Cargo.toml +1 -0
  2. impl/Cargo.toml +1 -0
  3. impl/src/lib.rs +12 -7
  4. readme.md +3 -0
  5. tests/metadata.rs +19 -0
Cargo.toml CHANGED
@@ -92,6 +92,7 @@ warp-ex = ["warp", "tokio", "mime_guess"]
92
92
  axum-ex = ["axum", "tokio", "mime_guess"]
93
93
  poem-ex = ["poem", "tokio", "mime_guess", "hex"]
94
94
  salvo-ex = ["salvo", "tokio", "mime_guess", "hex"]
95
+ deterministic-timestamps = ["rust-embed-impl/deterministic-timestamps"]
95
96
 
96
97
 
97
98
  [badges]
impl/Cargo.toml CHANGED
@@ -32,3 +32,4 @@ interpolate-folder-path = ["shellexpand"]
32
32
  compression = []
33
33
  mime-guess = ["rust-embed-utils/mime-guess"]
34
34
  include-exclude = ["rust-embed-utils/include-exclude"]
35
+ deterministic-timestamps = []
impl/src/lib.rs CHANGED
@@ -241,13 +241,18 @@ fn embed_file(
241
241
  ) -> syn::Result<TokenStream2> {
242
242
  let file = rust_embed_utils::read_file_from_fs(Path::new(full_canonical_path)).expect("File should be readable");
243
243
  let hash = file.metadata.sha256_hash();
244
+ let (last_modified, created) = if cfg!(feature = "deterministic-timestamps") {
245
+ (quote! { ::std::option::Option::Some(0u64) }, quote! { ::std::option::Option::Some(0u64) })
246
+ } else {
244
- let last_modified = match file.metadata.last_modified() {
247
+ let last_modified = match file.metadata.last_modified() {
245
- Some(last_modified) => quote! { ::std::option::Option::Some(#last_modified) },
248
+ Some(last_modified) => quote! { ::std::option::Option::Some(#last_modified) },
246
- None => quote! { ::std::option::Option::None },
249
+ None => quote! { ::std::option::Option::None },
247
- };
250
+ };
248
- let created = match file.metadata.created() {
251
+ let created = match file.metadata.created() {
249
- Some(created) => quote! { ::std::option::Option::Some(#created) },
252
+ Some(created) => quote! { ::std::option::Option::Some(#created) },
250
- None => quote! { ::std::option::Option::None },
253
+ None => quote! { ::std::option::Option::None },
254
+ };
255
+ (last_modified, created)
251
256
  };
252
257
  #[cfg(feature = "mime-guess")]
253
258
  let mimetype_tokens = {
readme.md CHANGED
@@ -137,6 +137,9 @@ use rust_embed::Embed;
137
137
  struct Asset;
138
138
  ```
139
139
 
140
+ ### `deterministic-timestamps`
141
+ Overwrite embedded files' timestamps with `0` to preserve deterministic builds with `debug-embed` or release mode
142
+
140
143
  ## Usage
141
144
 
142
145
  ```rust
tests/metadata.rs CHANGED
@@ -17,6 +17,7 @@ fn hash_is_accurate() {
17
17
  }
18
18
 
19
19
  #[test]
20
+ #[cfg(not(feature = "deterministic-timestamps"))]
20
21
  fn last_modified_is_accurate() {
21
22
  let index_file: EmbeddedFile = Asset::get("index.html").expect("index.html exists");
22
23
 
@@ -27,6 +28,7 @@ fn last_modified_is_accurate() {
27
28
  }
28
29
 
29
30
  #[test]
31
+ #[cfg(not(feature = "deterministic-timestamps"))]
30
32
  fn create_is_accurate() {
31
33
  let index_file: EmbeddedFile = Asset::get("index.html").expect("index.html exists");
32
34
 
@@ -35,3 +37,20 @@ fn create_is_accurate() {
35
37
 
36
38
  assert_eq!(index_file.metadata.created(), Some(expected_datetime_utc));
37
39
  }
40
+
41
+ #[test]
42
+ #[cfg(feature = "deterministic-timestamps")]
43
+ fn deterministic_timestamps_are_zero() {
44
+ let index_file: EmbeddedFile = Asset::get("index.html").expect("index.html exists");
45
+
46
+ assert_eq!(
47
+ index_file.metadata.last_modified(),
48
+ Some(0),
49
+ "last_modified should be 0 with deterministic-timestamps"
50
+ );
51
+ assert_eq!(index_file.metadata.created(), Some(0), "created should be 0 with deterministic-timestamps");
52
+
53
+ let metadata = fs::metadata(format!("{}/examples/public/index.html", env!("CARGO_MANIFEST_DIR"))).unwrap();
54
+ let fs_modified = metadata.modified().unwrap().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
55
+ assert_ne!(fs_modified, 0, "Filesystem modified time should not be 0");
56
+ }