Skip to main content

hm_util/
dirs.rs

1//! Harmont-specific directory resolution.
2//!
3//! Every directory accessor in this module returns a Harmont-namespaced
4//! path. Raw platform primitives (`home_dir`, `config_dir`) live in
5//! `os::dirs` and are **not** re-exported — callers outside `hm-util`
6//! should never need them.
7
8#![allow(clippy::must_use_candidate)]
9
10use std::path::PathBuf;
11
12use crate::os::dirs as platform;
13
14/// `~/.harmont/` — CLI config home (config.toml, credentials.toml).
15pub fn harmont_config_dir() -> Option<PathBuf> {
16    platform::home_dir().map(|h| h.join(".harmont"))
17}
18
19/// `<config_dir>/harmont/` — XDG-aware data root (plugins, state).
20pub fn harmont_data_dir() -> Option<PathBuf> {
21    platform::config_dir().map(|c| c.join("harmont"))
22}
23
24/// `<config_dir>/harmont/plugins/` — user-global plugin directory.
25pub fn harmont_plugins_dir() -> Option<PathBuf> {
26    harmont_data_dir().map(|d| d.join("plugins"))
27}
28
29/// `<config_dir>/harmont/state/` — per-plugin persistent KV state.
30pub fn harmont_plugin_state_dir() -> Option<PathBuf> {
31    harmont_data_dir().map(|d| d.join("state"))
32}
33
34/// `~/.harmont/cache/` — local build cache root.
35pub fn harmont_cache_dir() -> Option<PathBuf> {
36    harmont_config_dir().map(|h| h.join("cache"))
37}
38
39/// `~/.harmont/cache/workspaces/` — COW workspace cache root.
40pub fn harmont_workspace_cache_dir() -> Option<PathBuf> {
41    harmont_cache_dir().map(|c| c.join("workspaces"))
42}
43
44#[cfg(test)]
45#[allow(clippy::unwrap_used)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn harmont_config_dir_under_home() {
51        let p = harmont_config_dir().unwrap();
52        assert!(p.ends_with(".harmont"));
53    }
54
55    #[test]
56    fn harmont_data_dir_under_config() {
57        let p = harmont_data_dir().unwrap();
58        assert!(p.ends_with("harmont"));
59    }
60
61    #[test]
62    fn harmont_plugins_dir_resolves() {
63        let p = harmont_plugins_dir().unwrap();
64        assert!(p.ends_with("harmont/plugins"));
65    }
66
67    #[test]
68    fn harmont_plugin_state_dir_resolves() {
69        let p = harmont_plugin_state_dir().unwrap();
70        assert!(p.ends_with("harmont/state"));
71    }
72
73    #[test]
74    fn harmont_cache_dir_resolves() {
75        let p = harmont_cache_dir().unwrap();
76        assert!(p.to_string_lossy().contains("cache"));
77    }
78
79    #[test]
80    fn harmont_workspace_cache_dir_resolves() {
81        let p = harmont_workspace_cache_dir().unwrap();
82        assert!(p.to_string_lossy().contains("workspaces"));
83    }
84}