use crate::cache::MetadataCache;
use crate::error::{KopiError, Result};
use crate::platform;
use std::fs;
use std::path::Path;
pub fn load_cache(path: &Path) -> Result<MetadataCache> {
let contents = fs::read_to_string(path)
.map_err(|e| KopiError::ConfigError(format!("Failed to read cache file: {e}")))?;
let cache: MetadataCache =
serde_json::from_str(&contents).map_err(|_e| KopiError::InvalidMetadata)?;
Ok(cache)
}
pub fn save_cache(cache: &MetadataCache, path: &Path) -> Result<()> {
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).map_err(|e| {
KopiError::ConfigError(format!("Failed to create cache directory: {e}"))
})?;
}
let json = serde_json::to_string_pretty(cache).map_err(|_e| KopiError::InvalidMetadata)?;
let temp_path = path.with_extension("tmp");
if temp_path.exists() {
fs::remove_file(&temp_path)
.map_err(|e| KopiError::ConfigError(format!("Failed to remove old temp file: {e}")))?;
}
fs::write(&temp_path, json)
.map_err(|e| KopiError::ConfigError(format!("Failed to write cache file: {e}")))?;
platform::file_ops::atomic_rename(&temp_path, path)
.map_err(|e| KopiError::ConfigError(format!("Failed to rename cache file: {e}")))?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::cache::DistributionCache;
use crate::models::distribution::Distribution as JdkDistribution;
use tempfile::TempDir;
#[test]
fn test_load_nonexistent_cache() {
let temp_dir = TempDir::new().unwrap();
let cache_path = temp_dir.path().join("cache.json");
assert!(load_cache(&cache_path).is_err());
}
#[test]
fn test_save_and_load_cache() {
let temp_dir = TempDir::new().unwrap();
let cache_path = temp_dir.path().join("cache.json");
let mut cache = MetadataCache::new();
let dist = DistributionCache {
distribution: JdkDistribution::Temurin,
display_name: "Eclipse Temurin".to_string(),
packages: Vec::new(),
};
cache.distributions.insert("temurin".to_string(), dist);
save_cache(&cache, &cache_path).unwrap();
let loaded_cache = load_cache(&cache_path).unwrap();
assert_eq!(loaded_cache.version, cache.version);
assert_eq!(loaded_cache.distributions.len(), 1);
assert!(loaded_cache.distributions.contains_key("temurin"));
}
}