Skip to main content

work_graph/
walk.rs

1//! File list, crate manifests, and doc-page selection.
2
3use std::fs;
4use std::path::Path;
5
6use crate::git;
7use crate::model::{CrateNode, FileNode};
8use crate::IndexError;
9
10/// Skip cite-scrape on huge generated HTML (byte budget, fail closed for that page).
11pub const MAX_DOC_SCRAPE_BYTES: usize = 4 * 1024 * 1024;
12
13/// Lists project-files.
14pub fn list_project_files(root: &Path) -> Result<Vec<FileNode>, IndexError> {
15    let paths = git::ls_files(root)?;
16    Ok(paths
17        .into_iter()
18        .map(|path| FileNode {
19            lang: lang_for(&path),
20            path,
21        })
22        .collect())
23}
24
25/// Lists crates.
26pub fn list_crates(root: &Path, files: &[FileNode]) -> Result<Vec<CrateNode>, IndexError> {
27    let mut crates = Vec::new();
28    for file in files {
29        if !is_crate_manifest(&file.path) {
30            continue;
31        }
32        let abs = root.join(&file.path);
33        let Ok(src) = fs::read_to_string(&abs) else {
34            continue;
35        };
36        if let Some(name) = package_name(&src) {
37            crates.push(CrateNode {
38                name,
39                manifest: file.path.clone(),
40            });
41        }
42    }
43    crates.sort_by(|a, b| a.name.cmp(&b.name));
44    Ok(crates)
45}
46
47/// `true` when doc-page.
48pub fn is_doc_page(path: &str) -> bool {
49    let lower = path.to_ascii_lowercase();
50    if !(lower.ends_with(".md") || lower.ends_with(".html") || lower.ends_with(".htm")) {
51        return false;
52    }
53    lower.starts_with("docs/")
54        || lower.starts_with("skills/")
55        || lower.starts_with(".agents/")
56        || lower == "readme.md"
57        || lower.starts_with("crates/work-graph/")
58}
59
60/// `true` when crate-manifest.
61pub fn is_crate_manifest(path: &str) -> bool {
62    if !path.ends_with("Cargo.toml") {
63        return false;
64    }
65    // Workspace root + each crate / tool manifest. Skip vendor copies.
66    !path.starts_with("vendor/")
67}
68
69/// Language for a path.
70pub fn lang_for(path: &str) -> &'static str {
71    let lower = path.to_ascii_lowercase();
72    let ext = lower.rsplit('.').next().unwrap_or("");
73    match ext {
74        "rs" => "rust",
75        "toml" => "toml",
76        "md" => "markdown",
77        "html" | "htm" => "html",
78        "ts" | "tsx" => "typescript",
79        "js" | "mjs" => "javascript",
80        "vibe" => "vibe",
81        "n3" => "n3",
82        "ttl" => "turtle",
83        "json" => "json",
84        "wgsl" => "wgsl",
85        "css" => "css",
86        "py" => "python",
87        "sh" => "shell",
88        "ps1" => "powershell",
89        "yml" | "yaml" => "yaml",
90        _ => "other",
91    }
92}
93
94fn package_name(src: &str) -> Option<String> {
95    let pkg = src.find("[package]")?;
96    let rest = &src[pkg + "[package]".len()..];
97    let next_table = rest.find("\n[").unwrap_or(rest.len());
98    let section = &rest[..next_table];
99    for line in section.lines() {
100        let line = line.trim();
101        if let Some(val) = line.strip_prefix("name") {
102            let val = val.trim().trim_start_matches('=').trim();
103            if let Some(name) = unquote(val) {
104                return Some(name);
105            }
106        }
107    }
108    None
109}
110
111fn unquote(s: &str) -> Option<String> {
112    let s = s.trim();
113    if s.len() >= 2 && s.starts_with('"') && s.ends_with('"') {
114        return Some(s[1..s.len() - 1].to_string());
115    }
116    None
117}
118
119#[cfg(test)]
120mod tests {
121    use super::*;
122
123    #[test]
124    fn crate_manifests_skip_vendor() {
125        assert!(is_crate_manifest("crates/vibe/Cargo.toml"));
126        assert!(is_crate_manifest("Cargo.toml"));
127        assert!(!is_crate_manifest("vendor/imap-proto/Cargo.toml"));
128        assert!(!is_crate_manifest("crates/vibe/src/lib.rs"));
129    }
130
131    #[test]
132    fn doc_pages_are_docs_and_skills() {
133        assert!(is_doc_page("docs/vibe/SKILL.md"));
134        assert!(is_doc_page("docs/index.html"));
135        assert!(is_doc_page("README.md"));
136        assert!(!is_doc_page("crates/vibe/src/lib.rs"));
137        assert!(!is_doc_page("docs/data/schemaorg/foo.json"));
138    }
139
140    #[test]
141    fn package_name_reads_first_table() {
142        let src = "[workspace]\nmembers=[\"a\"]\n\n[package]\nname = \"work-graph\"\nversion = \"0.0.38\"\n";
143        assert_eq!(package_name(src).as_deref(), Some("work-graph"));
144    }
145}