Skip to main content

jar_genesis/
cache.rs

1use std::path::Path;
2
3use crate::git;
4
5/// Check that cache length matches the number of Genesis-Index trailers in git history.
6/// Returns Ok(()) if they match.
7pub fn check_staleness(cache: &[serde_json::Value], spec_dir: &Path) -> Result<(), String> {
8    let genesis_commit = git::read_genesis_commit_hash(spec_dir)
9        .map_err(|e| format!("failed to read genesis commit: {e}"))?;
10
11    if genesis_commit == "0000000000000000000000000000000000000000" {
12        return Ok(()); // Genesis not launched
13    }
14
15    let history_count = git::count_genesis_trailers(&genesis_commit)
16        .map_err(|e| format!("failed to count trailers: {e}"))?;
17
18    if cache.len() != history_count {
19        return Err(format!(
20            "{} entries cached, {} in git history",
21            cache.len(),
22            history_count
23        ));
24    }
25
26    Ok(())
27}
28
29/// CLI entry point: check cache from a file path.
30pub fn check(cache_file: &str) -> Result<(), Box<dyn std::error::Error>> {
31    let content = std::fs::read_to_string(cache_file)?;
32    let cache: Vec<serde_json::Value> = serde_json::from_str(&content)?;
33
34    let repo_root = git::repo_root()?;
35    let spec_dir = Path::new(&repo_root).join("spec");
36
37    match check_staleness(&cache, &spec_dir) {
38        Ok(()) => Ok(()),
39        Err(msg) => {
40            eprintln!("ERROR: genesis cache stale — {msg}");
41            std::process::exit(1);
42        }
43    }
44}