1mod font;
28mod image;
29mod index;
30
31pub use font::{FontAsset, FontFormat, FontStyle, FontWeight};
32pub use image::{ImageAsset, ImageFormat, ImageVariant};
33pub use index::{
34 AssetAlias, AssetEntry, AssetIndex, EmbedAsset, EmbedIndex, FontIndex, ImageIndex,
35};
36
37use crate::{DocumentId, Result};
38
39pub trait Asset {
41 fn id(&self) -> &str;
43
44 fn path(&self) -> &str;
46
47 fn hash(&self) -> &DocumentId;
49
50 fn size(&self) -> u64;
52
53 fn mime_type(&self) -> &str;
55}
56
57pub fn verify_asset_hash(
63 path: &str,
64 data: &[u8],
65 expected: &DocumentId,
66 algorithm: crate::HashAlgorithm,
67) -> Result<()> {
68 let actual = crate::Hasher::hash(algorithm, data);
69 if actual.hex_digest() != expected.hex_digest() {
70 return Err(crate::Error::HashMismatch {
71 path: path.to_string(),
72 expected: expected.hex_digest(),
73 actual: actual.hex_digest(),
74 });
75 }
76 Ok(())
77}
78
79#[cfg(test)]
80mod tests {
81 use super::*;
82 use crate::HashAlgorithm;
83
84 #[test]
85 fn test_verify_asset_hash_valid() {
86 let data = b"test asset data";
87 let hash = crate::Hasher::hash(HashAlgorithm::Sha256, data);
88 assert!(verify_asset_hash("test.png", data, &hash, HashAlgorithm::Sha256).is_ok());
89 }
90
91 #[test]
92 fn test_verify_asset_hash_invalid() {
93 let data = b"test asset data";
94 let wrong_hash = crate::Hasher::hash(HashAlgorithm::Sha256, b"different data");
95 assert!(verify_asset_hash("test.png", data, &wrong_hash, HashAlgorithm::Sha256).is_err());
96 }
97}