use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct HashRegistry {
pub files: HashMap<String, String>,
}
pub fn xxh128_hex(data: &[u8]) -> String {
let hash = xxhash_rust::xxh3::xxh3_128(data);
format!("{hash:032x}")
}
pub fn read_hashes(cache_dir: &Path) -> HashRegistry {
let path = cache_dir.join("hashes.json");
std::fs::read_to_string(&path)
.ok()
.and_then(|data| serde_json::from_str(&data).ok())
.unwrap_or_default()
}
pub fn write_hashes(cache_dir: &Path, registry: &HashRegistry) -> std::io::Result<()> {
std::fs::create_dir_all(cache_dir)?;
let json = serde_json::to_string_pretty(registry).map_err(std::io::Error::other)?;
std::fs::write(cache_dir.join("hashes.json"), json)
}