use std::borrow::Cow;
use std::hash::{Hash, Hasher};
use seahash::SeaHasher;
use crate::cache_key::{CacheKey, CacheKeyHasher};
pub fn cache_digest<H: CacheKey>(hashable: &H) -> String {
fn cache_key_u64<H: CacheKey>(hashable: &H) -> u64 {
let mut hasher = CacheKeyHasher::new();
hashable.cache_key(&mut hasher);
hasher.finish()
}
to_hex(cache_key_u64(hashable))
}
pub fn hash_digest<H: Hash>(hashable: &H) -> String {
fn hash_u64<H: Hash>(hashable: &H) -> u64 {
let mut hasher = SeaHasher::new();
hashable.hash(&mut hasher);
hasher.finish()
}
to_hex(hash_u64(hashable))
}
fn to_hex(num: u64) -> String {
hex::encode(num.to_le_bytes())
}
pub fn cache_name(name: &str, max_len: Option<usize>) -> Option<Cow<'_, str>> {
let limit = max_len.unwrap_or(usize::MAX);
if name
.bytes()
.all(|char| matches!(char, b'0'..=b'9' | b'a'..=b'f'))
{
return if name.is_empty() {
None
} else {
Some(Cow::Borrowed(name.get(..limit).unwrap_or(name)))
};
}
let mut normalized = String::with_capacity(name.len().min(limit));
let mut dash = false;
for char in name.bytes().take(limit) {
match char {
b'0'..=b'9' | b'a'..=b'z' | b'A'..=b'Z' => {
dash = false;
normalized.push(char.to_ascii_lowercase() as char);
}
_ => {
if !dash {
normalized.push('-');
dash = true;
}
}
}
}
if normalized.ends_with('-') {
normalized.pop();
}
if normalized.is_empty() {
None
} else {
Some(Cow::Owned(normalized))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cache_name() {
assert_eq!(cache_name("foo", None), Some("foo".into()));
assert_eq!(cache_name("foo-bar", None), Some("foo-bar".into()));
assert_eq!(cache_name("foo_bar", None), Some("foo-bar".into()));
assert_eq!(cache_name("foo-bar_baz", None), Some("foo-bar-baz".into()));
assert_eq!(cache_name("foo-bar_baz_", None), Some("foo-bar-baz".into()));
assert_eq!(cache_name("foo-_bar_baz", None), Some("foo-bar-baz".into()));
assert_eq!(cache_name("_+-_", None), None);
}
#[test]
fn test_cache_name_max_len() {
let long = "a".repeat(300);
assert_eq!(
cache_name(&long, Some(100)).as_deref().map(str::len),
Some(100)
);
let long_hex = "abcdef".repeat(50);
assert_eq!(
cache_name(&long_hex, Some(100)).as_deref().map(str::len),
Some(100)
);
assert_eq!(cache_name("aaaa_bbbb", Some(5)), Some("aaaa".into()));
assert_eq!(cache_name(&long, None).as_deref().map(str::len), Some(300));
}
}