use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};
use serde::{Deserialize, Serialize};
use crate::error::KitResult;
pub const CACHE_VERSION: u32 = 1;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Index {
#[serde(rename = "cacheVersion", default = "default_cache_version")]
pub cache_version: u32,
#[serde(default)]
pub plugins: BTreeMap<String, IndexEntry>,
}
fn default_cache_version() -> u32 {
CACHE_VERSION
}
impl Default for Index {
fn default() -> Self {
Self {
cache_version: CACHE_VERSION,
plugins: BTreeMap::new(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexEntry {
pub version: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub abi: Option<u32>,
pub root: PathBuf,
#[serde(default)]
pub source: InstallSource,
#[serde(rename = "installedAt", default)]
pub installed_at: u64,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum InstallSource {
Prebuilt,
#[default]
BuildHost,
Local {
path: PathBuf,
},
}
pub fn now_unix() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0)
}
impl Index {
pub fn load(path: &Path) -> Self {
let Ok(text) = std::fs::read_to_string(path) else {
return Self::default();
};
match serde_json::from_str::<Self>(&text) {
Ok(mut idx) => {
idx.cache_version = CACHE_VERSION;
idx
}
Err(_) => Self::default(),
}
}
pub fn save(&self, path: &Path) -> KitResult<()> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
let text = serde_json::to_string_pretty(self).map_err(|e| {
crate::error::KitError::Registry(format!("failed to serialize index: {e}"))
})?;
let tmp = path.with_extension("json.tmp");
std::fs::write(&tmp, text)?;
std::fs::rename(&tmp, path)?;
Ok(())
}
pub fn insert(&mut self, crate_name: &str, entry: IndexEntry) {
self.plugins.insert(crate_name.to_string(), entry);
}
pub fn remove(&mut self, crate_name: &str) -> Option<IndexEntry> {
self.plugins.remove(crate_name)
}
pub fn get(&self, crate_name: &str) -> Option<&IndexEntry> {
self.plugins.get(crate_name)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn entry(version: &str) -> IndexEntry {
IndexEntry {
version: version.to_string(),
abi: Some(1),
root: PathBuf::from("some").join("plugin"),
source: InstallSource::BuildHost,
installed_at: 1_767_225_600,
}
}
#[test]
fn insert_get_remove_round_trip() {
let mut idx = Index::default();
assert!(idx.get("a").is_none());
idx.insert("a", entry("0.1.0"));
assert_eq!(idx.get("a").unwrap().version, "0.1.0");
let removed = idx.remove("a");
assert_eq!(removed.unwrap().version, "0.1.0");
assert!(idx.get("a").is_none());
assert!(idx.remove("a").is_none());
}
#[test]
fn save_then_load_round_trips() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("nested").join(".plugins.json");
let mut idx = Index::default();
idx.insert("myapp-plugin-foo", entry("0.2.0"));
idx.save(&path).expect("should be able to write to disk");
let back = Index::load(&path);
assert_eq!(back.cache_version, CACHE_VERSION);
let got = back.get("myapp-plugin-foo").unwrap();
assert_eq!(got.version, "0.2.0");
assert_eq!(got.abi, Some(1));
assert_eq!(got.source, InstallSource::BuildHost);
assert_eq!(got.installed_at, 1_767_225_600);
}
#[test]
fn a_missing_file_yields_an_empty_index() {
let dir = tempfile::tempdir().unwrap();
let idx = Index::load(&dir.path().join("nope.json"));
assert!(idx.plugins.is_empty());
assert_eq!(idx.cache_version, CACHE_VERSION);
}
#[test]
fn a_corrupt_file_yields_an_empty_index() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join(".plugins.json");
std::fs::write(&path, "{ this is not json").unwrap();
let idx = Index::load(&path);
assert!(idx.plugins.is_empty());
}
#[test]
fn missing_optional_fields_fall_back() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join(".plugins.json");
std::fs::write(&path, r#"{"plugins":{"a":{"version":"0.1.0","root":"r"}}}"#).unwrap();
let idx = Index::load(&path);
let got = idx.get("a").unwrap();
assert_eq!(got.source, InstallSource::BuildHost);
assert_eq!(got.abi, None);
assert_eq!(got.installed_at, 0);
}
#[test]
fn save_leaves_no_temp_file_behind() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join(".plugins.json");
let mut idx = Index::default();
idx.insert("a", entry("0.1.0"));
idx.save(&path).unwrap();
let leftovers: Vec<_> = std::fs::read_dir(dir.path())
.unwrap()
.filter_map(|e| e.ok())
.map(|e| e.file_name().to_string_lossy().into_owned())
.filter(|n| n.ends_with(".tmp"))
.collect();
assert!(
leftovers.is_empty(),
"left a temp file behind: {leftovers:?}"
);
}
#[test]
fn now_unix_is_after_the_epoch() {
assert!(now_unix() > 1_600_000_000);
}
}