1use std::path::{Path, PathBuf};
4
5pub const CONFIG_DIR: &str = ".strixonomy";
7
8pub const LEGACY_CONFIG_DIR: &str = ".ontocore";
10
11pub const CACHE_SUBDIR: &str = "cache";
13
14pub const PLUGINS_SUBDIR: &str = "plugins";
16
17pub const DIAGNOSTICS_FILE: &str = "diagnostics.toml";
19
20pub const PLUGIN_OUT_REL: &str = ".strixonomy/plugin-out";
22
23pub const LEGACY_PLUGIN_OUT_REL: &str = ".ontocore/plugin-out";
25
26pub const PLUGIN_DISABLED_REL: &str = ".strixonomy/plugin-disabled.json";
28
29pub const LEGACY_PLUGIN_DISABLED_REL: &str = ".ontocore/plugin-disabled.json";
31
32pub 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
46pub 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
64pub fn plugins_dir(workspace: &Path) -> PathBuf {
66 resolve_config_path(workspace, PLUGINS_SUBDIR)
67}
68
69pub 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
86pub fn cache_dir(workspace: &Path) -> PathBuf {
88 resolve_config_path(workspace, CACHE_SUBDIR)
89}
90
91pub 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}