Skip to main content

lean_ctx/core/
artifacts.rs

1use std::path::{Path, PathBuf};
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct ArtifactRegistry {
7    pub artifacts: Vec<ArtifactSpec>,
8}
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
11pub struct ArtifactSpec {
12    pub name: String,
13    pub path: String,
14    pub description: String,
15    #[serde(default)]
16    pub tags: Vec<String>,
17}
18
19#[derive(Debug, Clone, Serialize, Deserialize)]
20pub struct ResolvedArtifact {
21    pub name: String,
22    pub path: String,
23    pub description: String,
24    #[serde(default)]
25    pub tags: Vec<String>,
26    pub exists: bool,
27    pub is_dir: bool,
28}
29
30#[derive(Debug, Default)]
31pub struct ResolvedArtifacts {
32    pub artifacts: Vec<ResolvedArtifact>,
33    pub warnings: Vec<String>,
34}
35
36pub fn load_resolved(project_root: &Path) -> ResolvedArtifacts {
37    let mut out = ResolvedArtifacts::default();
38    let root_canon = project_root
39        .canonicalize()
40        .unwrap_or_else(|_| project_root.to_path_buf());
41
42    let Some((registry_path, content)) = read_registry_file(project_root) else {
43        return out;
44    };
45
46    let parsed = parse_registry_json(&content).unwrap_or_else(|e| {
47        out.warnings.push(format!(
48            "artifact registry parse failed ({}): {e}",
49            registry_path.display()
50        ));
51        ArtifactRegistry { artifacts: vec![] }
52    });
53
54    let mut seen = std::collections::HashSet::<String>::new();
55    for spec in parsed.artifacts {
56        let name = spec.name.trim().to_string();
57        if name.is_empty() {
58            continue;
59        }
60        if !seen.insert(name.clone()) {
61            continue;
62        }
63
64        let raw = spec.path.trim();
65        if raw.is_empty() {
66            continue;
67        }
68        let rel = normalize_rel_path(raw);
69        let candidate = if Path::new(&rel).is_absolute() {
70            PathBuf::from(&rel)
71        } else {
72            project_root.join(&rel)
73        };
74
75        let abs = match crate::core::pathjail::jail_path(&candidate, project_root) {
76            Ok(p) => p,
77            Err(e) => {
78                out.warnings
79                    .push(format!("artifact path rejected ({name}): {rel} ({e})"));
80                continue;
81            }
82        };
83
84        let (exists, is_dir) = match abs.metadata() {
85            Ok(m) => (true, m.is_dir()),
86            Err(_) => (false, false),
87        };
88
89        let rel_out = abs
90            .strip_prefix(&root_canon)
91            .unwrap_or(&abs)
92            .to_string_lossy()
93            .to_string();
94
95        out.artifacts.push(ResolvedArtifact {
96            name,
97            path: rel_out,
98            description: spec.description.trim().to_string(),
99            tags: spec.tags,
100            exists,
101            is_dir,
102        });
103    }
104
105    out
106}
107
108fn read_registry_file(project_root: &Path) -> Option<(PathBuf, String)> {
109    let lean = project_root.join(".leanctxcontextartifacts.json");
110    if let Ok(s) = std::fs::read_to_string(&lean) {
111        return Some((lean, s));
112    }
113    let socrati = project_root.join(".socraticodecontextartifacts.json");
114    if let Ok(s) = std::fs::read_to_string(&socrati) {
115        return Some((socrati, s));
116    }
117    None
118}
119
120fn parse_registry_json(content: &str) -> Result<ArtifactRegistry, String> {
121    if let Ok(reg) = serde_json::from_str::<ArtifactRegistry>(content) {
122        return Ok(reg);
123    }
124    if let Ok(list) = serde_json::from_str::<Vec<ArtifactSpec>>(content) {
125        return Ok(ArtifactRegistry { artifacts: list });
126    }
127    Err("invalid JSON schema (expected { artifacts: [...] } or [...])".to_string())
128}
129
130fn normalize_rel_path(raw: &str) -> String {
131    let mut s = raw.trim().to_string();
132    while let Some(rest) = s.strip_prefix("./") {
133        s = rest.to_string();
134    }
135    s.trim_start_matches(['/', '\\']).to_string()
136}