lean_ctx/core/property_graph/
meta.rs1use serde::{Deserialize, Serialize};
2use std::path::PathBuf;
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5#[serde(default)]
6pub struct PropertyGraphMetaV1 {
7 pub schema_version: u32,
8 pub engine_version: u32,
15 pub built_with: String,
18 pub project_root: String,
23 pub built_at: String,
25 pub git_head: Option<String>,
27 pub git_dirty: Option<bool>,
29 pub nodes: Option<usize>,
31 pub edges: Option<usize>,
33 pub files_indexed: Option<usize>,
35 pub build_time_ms: Option<u64>,
37}
38
39impl Default for PropertyGraphMetaV1 {
40 fn default() -> Self {
41 Self {
42 schema_version: 1,
43 engine_version: 0,
44 built_with: String::new(),
45 project_root: String::new(),
46 built_at: String::new(),
47 git_head: None,
48 git_dirty: None,
49 nodes: None,
50 edges: None,
51 files_indexed: None,
52 build_time_ms: None,
53 }
54 }
55}
56
57pub fn meta_path(project_root: &str) -> PathBuf {
58 super::graph_dir(project_root).join("graph.meta.json")
59}
60
61pub fn load_meta(project_root: &str) -> Option<PropertyGraphMetaV1> {
62 load_meta_file(&meta_path(project_root))
63}
64
65pub fn load_meta_from_dir(dir: &std::path::Path) -> Option<PropertyGraphMetaV1> {
68 load_meta_file(&dir.join("graph.meta.json"))
69}
70
71impl PropertyGraphMetaV1 {
72 pub fn built_at_local(&self) -> Option<String> {
75 let dt = lean_ctx_time::OffsetDateTime::parse_from_rfc3339(&self.built_at).ok()?;
76 Some(dt.to_local().format("%Y-%m-%d %H:%M:%S").to_string())
77 }
78}
79
80fn load_meta_file(path: &std::path::Path) -> Option<PropertyGraphMetaV1> {
81 let s = std::fs::read_to_string(path).ok()?;
82 let meta: PropertyGraphMetaV1 = serde_json::from_str(&s).ok()?;
83 if meta.schema_version != 1 || meta.built_at.trim().is_empty() {
84 return None;
85 }
86 Some(meta)
87}
88
89pub fn write_meta(project_root: &str, meta: &PropertyGraphMetaV1) -> Result<PathBuf, String> {
90 let path = meta_path(project_root);
91 if let Some(parent) = path.parent() {
92 std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
93 }
94 let json = serde_json::to_string_pretty(meta).map_err(|e| e.to_string())?;
95 crate::config_io::write_atomic(&path, &json)?;
96 Ok(path)
97}