use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CheckRun {
pub kind: String,
pub command: String,
pub exit_code: i32,
}
#[derive(Deserialize)]
struct State {
#[serde(default)]
trees: BTreeMap<String, Tree>,
}
#[derive(Deserialize)]
struct Tree {
#[serde(default)]
runs: BTreeMap<String, Run>,
}
#[derive(Deserialize)]
struct Run {
command: String,
exit_code: i32,
}
pub fn id_from_env() -> Option<String> {
["JOUET_SESSION_ID", "CLAUDE_CODE_SESSION_ID"]
.iter()
.filter_map(|name| std::env::var(name).ok())
.find(|v| !v.trim().is_empty())
}
fn path(dir: &Path, id: &str) -> Option<PathBuf> {
let safe: String = id
.chars()
.filter(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_'))
.take(128)
.collect();
(!safe.is_empty()).then(|| dir.join(format!("{safe}.json")))
}
pub fn last_checks(dir: &Path, id: &str, worktree: &Path) -> Option<Vec<CheckRun>> {
let bytes = std::fs::read(path(dir, id)?).ok()?;
let mut state: State = serde_json::from_slice(&bytes).ok()?;
let root = worktree
.canonicalize()
.unwrap_or_else(|_| worktree.to_path_buf());
let tree = state.trees.remove(root.to_string_lossy().as_ref())?;
let runs: Vec<CheckRun> = tree
.runs
.into_iter()
.map(|(kind, run)| CheckRun {
kind,
command: run.command,
exit_code: run.exit_code,
})
.collect();
(!runs.is_empty()).then_some(runs)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reads_what_the_hooks_record() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(
dir.path().join("abc-123.json"),
r#"{"trees":{"/src/app":{"runs":{"test":{"command":"cargo test","exit_code":101},"lint":{"command":"mise run lint","exit_code":0}}},"/src/other":{"runs":{"build":{"command":"cargo build","exit_code":0}}}}}"#,
)
.unwrap();
let app = Path::new("/src/app");
let runs = last_checks(dir.path(), "abc-123", app).unwrap();
assert_eq!(
runs,
[
CheckRun {
kind: "lint".into(),
command: "mise run lint".into(),
exit_code: 0
},
CheckRun {
kind: "test".into(),
command: "cargo test".into(),
exit_code: 101
},
]
);
assert_eq!(
last_checks(dir.path(), "abc-123", Path::new("/src/elsewhere")),
None
);
assert_eq!(last_checks(dir.path(), "other", app), None);
assert_eq!(last_checks(dir.path(), "../../etc", app), None);
std::fs::write(dir.path().join("bad.json"), "not json").unwrap();
assert_eq!(last_checks(dir.path(), "bad", app), None);
std::fs::write(
dir.path().join("empty.json"),
r#"{"trees":{"/src/app":{"runs":{}}}}"#,
)
.unwrap();
assert_eq!(last_checks(dir.path(), "empty", app), None);
}
}