Skip to main content

greentic_ext_runtime/
discovery.rs

1use std::path::{Path, PathBuf};
2
3#[derive(Debug, Clone)]
4pub struct DiscoveryPaths {
5    pub user: PathBuf,
6    pub project: Option<PathBuf>,
7}
8
9impl DiscoveryPaths {
10    #[must_use]
11    pub fn new(user: PathBuf) -> Self {
12        Self {
13            user,
14            project: None,
15        }
16    }
17
18    #[must_use]
19    pub fn with_project(mut self, project: PathBuf) -> Self {
20        self.project = Some(project);
21        self
22    }
23
24    #[must_use]
25    pub fn all(&self) -> Vec<&PathBuf> {
26        let mut v = vec![&self.user];
27        if let Some(p) = &self.project {
28            v.push(p);
29        }
30        v
31    }
32
33    /// Return the inferred greentic home directory — the parent of the
34    /// `user` extensions root. By convention `user` is the directory that
35    /// holds per-kind subdirs (e.g. `<home>/extensions/`), so its parent
36    /// is the home dir where `extensions-state.json` lives. Returns
37    /// `None` if `user` has no parent (e.g. `/`).
38    #[must_use]
39    pub fn home(&self) -> Option<&Path> {
40        self.user.parent()
41    }
42}
43
44/// Scan a single kind directory (e.g. `~/.greentic/extensions/design/`).
45/// Returns absolute paths to each extension subdirectory that contains a
46/// `describe.json`. Returns empty vec if the directory doesn't exist.
47pub fn scan_kind_dir(kind_dir: &Path) -> std::io::Result<Vec<PathBuf>> {
48    if !kind_dir.exists() {
49        return Ok(Vec::new());
50    }
51    let mut out = Vec::new();
52    for entry in std::fs::read_dir(kind_dir)? {
53        let entry = entry?;
54        if !entry.file_type()?.is_dir() {
55            continue;
56        }
57        if entry.path().join("describe.json").exists() {
58            out.push(entry.path());
59        }
60    }
61    out.sort();
62    Ok(out)
63}