use bytes::Bytes;
use reinda_core::AssetDef;
#[cfg(not(feature = "hash"))]
pub(crate) fn hashed_path_of(_: &AssetDef, _: &Bytes) -> String {
unreachable!("`hash_path_of` called, but Cargo-feature 'hash' is disabled.");
}
#[cfg(feature = "hash")]
pub(crate) fn hashed_path_of(def: &AssetDef, content: &Bytes) -> String {
use std::path::Path;
use sha2::{Digest, Sha256};
const HASH_BYTES_IN_FILENAME: usize = 9;
let (first, second) = def.hash.expect("called `hashed_path_of`, but `def.hash` is None");
let mut out = String::new();
if let Some(parent) = Path::new(def.path).parent() {
if parent.iter().count() > 0 {
out.push_str(parent.to_str().unwrap());
out.push('/');
}
}
out.push_str(first);
let hash = Sha256::digest(&content);
base64::encode_config_buf(
&hash.as_slice()[..HASH_BYTES_IN_FILENAME],
base64::URL_SAFE_NO_PAD,
&mut out,
);
out.push_str(second);
out
}