use anyhow::{Context, Result};
use expand_tilde::expand_tilde;
use serde::{Deserialize, Serialize};
use std::collections::hash_map::DefaultHasher;
use std::fs;
use std::hash::{Hash, Hasher};
use std::path::{Path, PathBuf};
use super::DiscoveredComponent;
#[derive(Serialize, Deserialize)]
struct CacheEntry {
source: String,
version_hash: u64,
components: Vec<CachedComponent>,
}
#[derive(Serialize, Deserialize)]
struct CachedComponent {
tag_name: String,
html_content: String,
css_content: Option<String>,
}
pub struct DiscoveryCache {
cache_dir: PathBuf,
}
impl DiscoveryCache {
pub fn open() -> Result<Self> {
let home = expand_tilde(&PathBuf::from("~"))
.context("Could not determine home directory for component cache")?
.into_owned();
let cache_dir = home.join(".webui").join("cache").join("components");
fs::create_dir_all(&cache_dir).with_context(|| {
format!("Failed to create cache directory: {}", cache_dir.display())
})?;
Ok(Self { cache_dir })
}
fn cache_key(source: &str, pkg_json_path: &Path) -> String {
let mut hasher = DefaultHasher::new();
source.hash(&mut hasher);
pkg_json_path.hash(&mut hasher);
format!("{:016x}", hasher.finish())
}
fn version_hash(path: &Path) -> Result<u64> {
let content = fs::read(path)
.with_context(|| format!("Failed to read for hashing: {}", path.display()))?;
let mut hasher = DefaultHasher::new();
content.hash(&mut hasher);
Ok(hasher.finish())
}
pub fn get(
&self,
source: &str,
pkg_json_path: &Path,
) -> Result<Option<Vec<DiscoveredComponent>>> {
let key = Self::cache_key(source, pkg_json_path);
let cache_file = self.cache_dir.join(format!("{key}.json"));
if !cache_file.exists() {
return Ok(None);
}
let content = match fs::read_to_string(&cache_file) {
Ok(c) => c,
Err(_) => return Ok(None),
};
let entry: CacheEntry = match serde_json::from_str(&content) {
Ok(e) => e,
Err(_) => return Ok(None),
};
let current_hash = Self::version_hash(pkg_json_path)?;
if entry.version_hash != current_hash {
return Ok(None);
}
let components = entry
.components
.into_iter()
.map(|c| DiscoveredComponent {
tag_name: c.tag_name,
html_content: c.html_content,
css_content: c.css_content,
source: entry.source.clone(),
})
.collect();
Ok(Some(components))
}
pub fn put(
&self,
source: &str,
pkg_json_path: &Path,
components: &[DiscoveredComponent],
) -> Result<()> {
let key = Self::cache_key(source, pkg_json_path);
let cache_file = self.cache_dir.join(format!("{key}.json"));
let version_hash = Self::version_hash(pkg_json_path)?;
let entry = CacheEntry {
source: source.to_string(),
version_hash,
components: components
.iter()
.map(|c| CachedComponent {
tag_name: c.tag_name.clone(),
html_content: c.html_content.clone(),
css_content: c.css_content.clone(),
})
.collect(),
};
let json = serde_json::to_string(&entry).context("Failed to serialize cache entry")?;
let temp_file = self.cache_dir.join(format!("{key}.tmp"));
fs::write(&temp_file, &json)
.with_context(|| format!("Failed to write temp cache file: {}", temp_file.display()))?;
fs::rename(&temp_file, &cache_file)
.with_context(|| format!("Failed to finalize cache file: {}", cache_file.display()))?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cache_round_trip() {
let cache = DiscoveryCache::open().unwrap();
let tmp = tempfile::TempDir::new().unwrap();
let pkg_json = tmp.path().join("package.json");
fs::write(&pkg_json, r#"{"name":"test","version":"1.0.0"}"#).unwrap();
let components = vec![DiscoveredComponent {
tag_name: "test-comp".to_string(),
html_content: "<div>test</div>".to_string(),
css_content: Some(".test { color: red; }".to_string()),
source: "test-pkg".to_string(),
}];
cache.put("test-pkg", &pkg_json, &components).unwrap();
let cached = cache.get("test-pkg", &pkg_json).unwrap();
assert!(cached.is_some());
let cached = cached.unwrap();
assert_eq!(cached.len(), 1);
assert_eq!(cached[0].tag_name, "test-comp");
assert_eq!(cached[0].html_content, "<div>test</div>");
assert_eq!(
cached[0].css_content.as_deref(),
Some(".test { color: red; }")
);
}
#[test]
fn test_cache_invalidation_on_content_change() {
let cache = DiscoveryCache::open().unwrap();
let tmp = tempfile::TempDir::new().unwrap();
let pkg_json = tmp.path().join("package.json");
fs::write(&pkg_json, r#"{"name":"test","version":"1.0.0"}"#).unwrap();
let components = vec![DiscoveredComponent {
tag_name: "test-comp".to_string(),
html_content: "<div>v1</div>".to_string(),
css_content: None,
source: "test-pkg".to_string(),
}];
cache.put("test-pkg", &pkg_json, &components).unwrap();
fs::write(&pkg_json, r#"{"name":"test","version":"2.0.0"}"#).unwrap();
let cached = cache.get("test-pkg", &pkg_json).unwrap();
assert!(cached.is_none());
}
#[test]
fn test_cache_miss_for_unknown_source() {
let cache = DiscoveryCache::open().unwrap();
let tmp = tempfile::TempDir::new().unwrap();
let pkg_json = tmp.path().join("package.json");
fs::write(&pkg_json, r#"{"name":"unknown"}"#).unwrap();
let cached = cache.get("unknown-pkg", &pkg_json).unwrap();
assert!(cached.is_none());
}
#[test]
fn test_cache_handles_corrupt_file() {
let cache = DiscoveryCache::open().unwrap();
let tmp = tempfile::TempDir::new().unwrap();
let pkg_json = tmp.path().join("package.json");
fs::write(&pkg_json, r#"{"name":"test"}"#).unwrap();
let key = DiscoveryCache::cache_key("test-pkg", &pkg_json);
let cache_file = cache.cache_dir.join(format!("{key}.json"));
fs::write(&cache_file, "NOT VALID JSON!!!").unwrap();
let cached = cache.get("test-pkg", &pkg_json).unwrap();
assert!(cached.is_none());
}
}