Skip to main content

thoughts_tool/utils/
paths.rs

1use anyhow::Result;
2use dirs;
3use std::path::Path;
4use std::path::PathBuf;
5
6/// Expand tilde (~) in paths to home directory
7pub 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
21/// Ensure a directory exists, creating it if necessary
22pub fn ensure_dir(path: &Path) -> Result<()> {
23    if !path.exists() {
24        std::fs::create_dir_all(path)?;
25    }
26    Ok(())
27}
28
29/// Sanitize a directory name for use in filesystem
30pub fn sanitize_dir_name(name: &str) -> String {
31    name.chars()
32        .map(|c| match c {
33            '/' | '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|' => '_',
34            _ => c,
35        })
36        .collect()
37}
38
39// Add after line 50 (after sanitize_dir_name function)
40
41/// Get the repository configuration file path
42pub fn get_repo_config_path(repo_root: &Path) -> PathBuf {
43    repo_root.join(".thoughts").join("config.json")
44}
45
46/// Get external metadata directory for personal metadata about other repos
47#[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
54/// Get local metadata file path for a repository
55// TODO(2): Implement local metadata caching
56pub fn get_local_metadata_path(repo_root: &Path) -> PathBuf {
57    repo_root.join(".thoughts").join("data").join("local.json")
58}
59
60/// Get rules file path for a repository
61// TODO(2): Implement repository-specific rules system
62pub fn get_repo_rules_path(repo_root: &Path) -> PathBuf {
63    repo_root.join(".thoughts").join("rules.json")
64}
65
66/// Get the XDG config home directory.
67///
68/// Returns `$XDG_CONFIG_HOME` if set, otherwise `~/.config`.
69fn 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
78/// Get the repository mapping file path.
79///
80/// Returns the location at `~/.config/agentic/repos.json`.
81pub fn get_repo_mapping_path() -> Result<PathBuf> {
82    Ok(xdg_config_home()?.join("agentic").join("repos.json"))
83}
84
85/// Get the legacy repository mapping file path.
86///
87/// Returns the old location at `~/.thoughts/repos.json` for migration purposes.
88pub 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
94/// Get the personal config path (for deprecation warnings)
95pub 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        // Test tilde expansion
110        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        // Test absolute path
115        assert_eq!(
116            expand_path(Path::new("/tmp/test")).unwrap(),
117            PathBuf::from("/tmp/test")
118        );
119
120        // Test relative path
121        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}