thoughts_tool/utils/
paths.rs1use anyhow::Result;
2use dirs;
3use std::path::Path;
4use std::path::PathBuf;
5
6pub fn expand_path(path: &Path) -> Result<PathBuf> {
8 let path_str = path.to_string_lossy();
9
10 if let Some(stripped) = path_str.strip_prefix("~/") {
11 let home = dirs::home_dir()
12 .ok_or_else(|| anyhow::anyhow!("Could not determine home directory"))?;
13 Ok(home.join(stripped))
14 } else if path_str == "~" {
15 dirs::home_dir().ok_or_else(|| anyhow::anyhow!("Could not determine home directory"))
16 } else {
17 Ok(path.to_path_buf())
18 }
19}
20
21pub fn ensure_dir(path: &Path) -> Result<()> {
23 if !path.exists() {
24 std::fs::create_dir_all(path)?;
25 }
26 Ok(())
27}
28
29pub fn sanitize_dir_name(name: &str) -> String {
31 name.chars()
32 .map(|c| match c {
33 '/' | '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|' => '_',
34 _ => c,
35 })
36 .collect()
37}
38
39pub fn get_repo_config_path(repo_root: &Path) -> PathBuf {
43 repo_root.join(".thoughts").join("config.json")
44}
45
46#[cfg(target_os = "macos")]
48pub fn get_external_metadata_dir() -> Result<PathBuf> {
49 let home =
50 dirs::home_dir().ok_or_else(|| anyhow::anyhow!("Could not determine home directory"))?;
51 Ok(home.join(".thoughts").join("data").join("external"))
52}
53
54pub fn get_local_metadata_path(repo_root: &Path) -> PathBuf {
57 repo_root.join(".thoughts").join("data").join("local.json")
58}
59
60pub fn get_repo_rules_path(repo_root: &Path) -> PathBuf {
63 repo_root.join(".thoughts").join("rules.json")
64}
65
66fn xdg_config_home() -> Result<PathBuf> {
70 if let Some(dir) = std::env::var_os("XDG_CONFIG_HOME") {
71 return Ok(PathBuf::from(dir));
72 }
73 let home =
74 dirs::home_dir().ok_or_else(|| anyhow::anyhow!("Could not determine home directory"))?;
75 Ok(home.join(".config"))
76}
77
78pub fn get_repo_mapping_path() -> Result<PathBuf> {
82 Ok(xdg_config_home()?.join("agentic").join("repos.json"))
83}
84
85pub fn get_legacy_repo_mapping_path() -> Result<PathBuf> {
89 let home =
90 dirs::home_dir().ok_or_else(|| anyhow::anyhow!("Could not determine home directory"))?;
91 Ok(home.join(".thoughts").join("repos.json"))
92}
93
94pub fn get_personal_config_path() -> Result<PathBuf> {
96 let home =
97 dirs::home_dir().ok_or_else(|| anyhow::anyhow!("Could not determine home directory"))?;
98 Ok(home.join(".thoughts").join("config.json"))
99}
100
101#[cfg(test)]
102mod tests {
103 use super::*;
104 use serial_test::serial;
105
106 #[test]
107 #[serial]
108 fn test_expand_path() {
109 let home = dirs::home_dir().unwrap();
111 assert_eq!(expand_path(Path::new("~/test")).unwrap(), home.join("test"));
112 assert_eq!(expand_path(Path::new("~")).unwrap(), home);
113
114 assert_eq!(
116 expand_path(Path::new("/tmp/test")).unwrap(),
117 PathBuf::from("/tmp/test")
118 );
119
120 assert_eq!(
122 expand_path(Path::new("test")).unwrap(),
123 PathBuf::from("test")
124 );
125 }
126
127 #[test]
128 fn test_sanitize_dir_name() {
129 assert_eq!(sanitize_dir_name("normal-name_123"), "normal-name_123");
130 assert_eq!(
131 sanitize_dir_name("bad/name:with*chars?"),
132 "bad_name_with_chars_"
133 );
134 }
135}