mod font;
mod image;
mod index;
pub use font::{FontAsset, FontFormat, FontStyle, FontWeight};
pub use image::{ImageAsset, ImageFormat, ImageVariant};
pub use index::{
AssetAlias, AssetEntry, AssetIndex, EmbedAsset, EmbedIndex, FontIndex, ImageIndex,
};
use crate::{DocumentId, Result};
pub trait Asset {
fn id(&self) -> &str;
fn path(&self) -> &str;
fn hash(&self) -> &DocumentId;
fn size(&self) -> u64;
fn mime_type(&self) -> &str;
}
pub fn verify_asset_hash(
path: &str,
data: &[u8],
expected: &DocumentId,
algorithm: crate::HashAlgorithm,
) -> Result<()> {
let actual = crate::Hasher::hash(algorithm, data);
if actual.hex_digest() != expected.hex_digest() {
return Err(crate::Error::HashMismatch {
path: path.to_string(),
expected: expected.hex_digest(),
actual: actual.hex_digest(),
});
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::HashAlgorithm;
#[test]
fn test_verify_asset_hash_valid() {
let data = b"test asset data";
let hash = crate::Hasher::hash(HashAlgorithm::Sha256, data);
assert!(verify_asset_hash("test.png", data, &hash, HashAlgorithm::Sha256).is_ok());
}
#[test]
fn test_verify_asset_hash_invalid() {
let data = b"test asset data";
let wrong_hash = crate::Hasher::hash(HashAlgorithm::Sha256, b"different data");
assert!(verify_asset_hash("test.png", data, &wrong_hash, HashAlgorithm::Sha256).is_err());
}
}