Skip to main content

strixonomy_core/
paths.rs

1//! Workspace configuration directory resolution (`.strixonomy` primary, `.ontocore` legacy).
2
3use std::path::{Path, PathBuf};
4
5/// Primary workspace config directory (v0.27+).
6pub const CONFIG_DIR: &str = ".strixonomy";
7
8/// Legacy OntoCore config directory (accepted through at least 1.0).
9pub const LEGACY_CONFIG_DIR: &str = ".ontocore";
10
11/// Relative cache directory under the config dir.
12pub const CACHE_SUBDIR: &str = "cache";
13
14/// Relative plugins directory under the config dir.
15pub const PLUGINS_SUBDIR: &str = "plugins";
16
17/// Diagnostics config filename under the config dir.
18pub const DIAGNOSTICS_FILE: &str = "diagnostics.toml";
19
20/// Default plugin export directory (relative to workspace).
21pub const PLUGIN_OUT_REL: &str = ".strixonomy/plugin-out";
22
23/// Legacy plugin export directory.
24pub const LEGACY_PLUGIN_OUT_REL: &str = ".ontocore/plugin-out";
25
26/// Disabled-plugins state file (relative to workspace).
27pub const PLUGIN_DISABLED_REL: &str = ".strixonomy/plugin-disabled.json";
28
29/// Legacy disabled-plugins state file.
30pub const LEGACY_PLUGIN_DISABLED_REL: &str = ".ontocore/plugin-disabled.json";
31
32/// Resolve `{workspace}/.strixonomy/{leaf}` preferring an existing primary path,
33/// else an existing legacy `.ontocore/{leaf}`, else the primary path for writes.
34pub fn resolve_config_path(workspace: &Path, leaf: &str) -> PathBuf {
35    let primary = workspace.join(CONFIG_DIR).join(leaf);
36    if primary.exists() {
37        return primary;
38    }
39    let legacy = workspace.join(LEGACY_CONFIG_DIR).join(leaf);
40    if legacy.exists() {
41        return legacy;
42    }
43    primary
44}
45
46/// Like [`resolve_config_path`] but for a relative path that already includes the
47/// config dir prefix (e.g. `.strixonomy/cache`).
48pub fn resolve_dotted_config_path(
49    workspace: &Path,
50    primary_rel: &str,
51    legacy_rel: &str,
52) -> PathBuf {
53    let primary = workspace.join(primary_rel);
54    if primary.exists() {
55        return primary;
56    }
57    let legacy = workspace.join(legacy_rel);
58    if legacy.exists() {
59        return legacy;
60    }
61    primary
62}
63
64/// Primary plugins directory path (may not exist yet).
65pub fn plugins_dir(workspace: &Path) -> PathBuf {
66    resolve_config_path(workspace, PLUGINS_SUBDIR)
67}
68
69/// All plugin directories to scan (primary first, then legacy if distinct and present).
70pub fn plugin_search_dirs(workspace: &Path) -> Vec<PathBuf> {
71    let primary = workspace.join(CONFIG_DIR).join(PLUGINS_SUBDIR);
72    let legacy = workspace.join(LEGACY_CONFIG_DIR).join(PLUGINS_SUBDIR);
73    let mut dirs = Vec::new();
74    if primary.is_dir() {
75        dirs.push(primary.clone());
76    }
77    if legacy.is_dir() && legacy != primary {
78        dirs.push(legacy);
79    }
80    if dirs.is_empty() {
81        dirs.push(primary);
82    }
83    dirs
84}
85
86/// Cache root under the workspace.
87pub fn cache_dir(workspace: &Path) -> PathBuf {
88    resolve_config_path(workspace, CACHE_SUBDIR)
89}
90
91/// Diagnostics.toml path.
92pub fn diagnostics_config_path(workspace: &Path) -> PathBuf {
93    resolve_config_path(workspace, DIAGNOSTICS_FILE)
94}
95
96#[cfg(test)]
97mod tests {
98    use super::*;
99    use std::fs;
100
101    #[test]
102    fn prefers_primary_when_present() {
103        let dir = tempfile::tempdir().unwrap();
104        fs::create_dir_all(dir.path().join(".strixonomy/cache")).unwrap();
105        fs::create_dir_all(dir.path().join(".ontocore/cache")).unwrap();
106        assert_eq!(cache_dir(dir.path()), dir.path().join(".strixonomy/cache"));
107    }
108
109    #[test]
110    fn falls_back_to_legacy() {
111        let dir = tempfile::tempdir().unwrap();
112        fs::create_dir_all(dir.path().join(".ontocore/plugins")).unwrap();
113        assert_eq!(plugins_dir(dir.path()), dir.path().join(".ontocore/plugins"));
114    }
115
116    #[test]
117    fn defaults_to_primary_when_missing() {
118        let dir = tempfile::tempdir().unwrap();
119        assert_eq!(
120            diagnostics_config_path(dir.path()),
121            dir.path().join(".strixonomy/diagnostics.toml")
122        );
123    }
124}