~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.
d291ad15
—
AzureMarker 4 years ago
Add tests and more documentation for file metadata
- tests/interpolated_path.rs +3 -9
- tests/lib.rs +3 -9
- tests/metadata.rs +24 -0
- utils/src/lib.rs +2 -0
tests/interpolated_path.rs
CHANGED
|
@@ -7,15 +7,9 @@ struct Asset;
|
|
|
7
7
|
|
|
8
8
|
#[test]
|
|
9
9
|
fn get_works() {
|
|
10
|
-
|
|
10
|
+
assert!(Asset::get("index.html").is_some(), "index.html should exist");
|
|
11
|
-
panic!("index.html should exist");
|
|
12
|
-
}
|
|
13
|
-
|
|
11
|
+
assert!(Asset::get("gg.html").is_none(), "gg.html should not exist");
|
|
14
|
-
panic!("gg.html should not exist");
|
|
15
|
-
}
|
|
16
|
-
|
|
12
|
+
assert!(Asset::get("images/llama.png").is_some(), "llama.png should exist");
|
|
17
|
-
panic!("llama.png should exist");
|
|
18
|
-
}
|
|
19
13
|
}
|
|
20
14
|
|
|
21
15
|
#[test]
|
tests/lib.rs
CHANGED
|
@@ -7,15 +7,9 @@ struct Asset;
|
|
|
7
7
|
|
|
8
8
|
#[test]
|
|
9
9
|
fn get_works() {
|
|
10
|
-
|
|
10
|
+
assert!(Asset::get("index.html").is_some(), "index.html should exist");
|
|
11
|
-
panic!("index.html should exist");
|
|
12
|
-
}
|
|
13
|
-
|
|
11
|
+
assert!(Asset::get("gg.html").is_none(), "gg.html should not exist");
|
|
14
|
-
panic!("gg.html should not exist");
|
|
15
|
-
}
|
|
16
|
-
|
|
12
|
+
assert!(Asset::get("images/llama.png").is_some(), "llama.png should exist");
|
|
17
|
-
panic!("llama.png should exist");
|
|
18
|
-
}
|
|
19
13
|
}
|
|
20
14
|
|
|
21
15
|
/// Using Windows-style path separators (`\`) is acceptable
|
tests/metadata.rs
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
use rust_embed::{EmbeddedFile, RustEmbed};
|
|
2
|
+
use sha2::Digest;
|
|
3
|
+
|
|
4
|
+
#[derive(RustEmbed)]
|
|
5
|
+
#[folder = "examples/public/"]
|
|
6
|
+
struct Asset;
|
|
7
|
+
|
|
8
|
+
#[test]
|
|
9
|
+
fn hash_is_accurate() {
|
|
10
|
+
let index_file: EmbeddedFile = Asset::get("index.html").expect("index.html exists");
|
|
11
|
+
let mut hasher = sha2::Sha256::new();
|
|
12
|
+
hasher.update(index_file.data);
|
|
13
|
+
let expected_hash: [u8; 32] = hasher.finalize().into();
|
|
14
|
+
|
|
15
|
+
assert_eq!(index_file.metadata.sha256_hash(), expected_hash);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
#[test]
|
|
19
|
+
fn last_modified_is_accurate() {
|
|
20
|
+
let index_file: EmbeddedFile = Asset::get("index.html").expect("index.html exists");
|
|
21
|
+
let expected_datetime_utc = 1527818165;
|
|
22
|
+
|
|
23
|
+
assert_eq!(index_file.metadata.last_modified(), Some(expected_datetime_utc));
|
|
24
|
+
}
|
utils/src/lib.rs
CHANGED
|
@@ -33,11 +33,13 @@ pub fn get_files(folder_path: String) -> impl Iterator<Item = FileEntry> {
|
|
|
33
33
|
})
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
/// A file embedded into the binary
|
|
36
37
|
pub struct EmbeddedFile {
|
|
37
38
|
pub data: Cow<'static, [u8]>,
|
|
38
39
|
pub metadata: Metadata,
|
|
39
40
|
}
|
|
40
41
|
|
|
42
|
+
/// Metadata about an embedded file
|
|
41
43
|
pub struct Metadata {
|
|
42
44
|
hash: [u8; 32],
|
|
43
45
|
last_modified: Option<u64>,
|