|
| 1 | +//! Hierarchical path management for the application |
| 2 | +
|
| 3 | +use std::path::PathBuf; |
| 4 | + |
| 5 | +use crate::os::Os; |
| 6 | + |
| 7 | +#[derive(Debug, thiserror::Error)] |
| 8 | +pub enum DirectoryError { |
| 9 | + #[error("home directory not found")] |
| 10 | + NoHomeDirectory, |
| 11 | + #[error("IO Error: {0}")] |
| 12 | + Io(#[from] std::io::Error), |
| 13 | +} |
| 14 | + |
| 15 | +pub mod workspace { |
| 16 | + //! Project-level paths (relative to current working directory) |
| 17 | + pub const MCP_CONFIG: &str = ".amazonq/mcp.json"; |
| 18 | + pub const RULES_PATTERN: &str = ".amazonq/rules/**/*.md"; |
| 19 | +} |
| 20 | + |
| 21 | +pub mod global { |
| 22 | + //! User-level paths (relative to home directory) |
| 23 | + pub const MCP_CONFIG: &str = ".aws/amazonq/mcp.json"; |
| 24 | + pub const GLOBAL_CONTEXT: &str = ".aws/amazonq/global_context.json"; |
| 25 | + pub const PROFILES_DIR: &str = ".aws/amazonq/profiles"; |
| 26 | +} |
| 27 | + |
| 28 | +pub mod application { |
| 29 | + //! Application data paths (system-specific) |
| 30 | + #[cfg(unix)] |
| 31 | + pub const DATA_DIR_NAME: &str = "amazon-q"; |
| 32 | + #[cfg(windows)] |
| 33 | + pub const DATA_DIR_NAME: &str = "AmazonQ"; |
| 34 | + pub const SETTINGS_FILE: &str = "settings.json"; |
| 35 | + pub const DATABASE_FILE: &str = "data.sqlite3"; |
| 36 | +} |
| 37 | + |
| 38 | +type Result<T, E = DirectoryError> = std::result::Result<T, E>; |
| 39 | + |
| 40 | +/// The directory of the users home |
| 41 | +/// - Linux: /home/Alice |
| 42 | +/// - MacOS: /Users/Alice |
| 43 | +/// - Windows: C:\Users\Alice |
| 44 | +pub fn home_dir(#[cfg_attr(windows, allow(unused_variables))] os: &Os) -> Result<PathBuf> { |
| 45 | + #[cfg(unix)] |
| 46 | + match cfg!(test) { |
| 47 | + true => os |
| 48 | + .env |
| 49 | + .get("HOME") |
| 50 | + .map_err(|_err| DirectoryError::NoHomeDirectory) |
| 51 | + .and_then(|h| { |
| 52 | + if h.is_empty() { |
| 53 | + Err(DirectoryError::NoHomeDirectory) |
| 54 | + } else { |
| 55 | + Ok(h) |
| 56 | + } |
| 57 | + }) |
| 58 | + .map(PathBuf::from) |
| 59 | + .map(|p| os.fs.chroot_path(p)), |
| 60 | + false => dirs::home_dir().ok_or(DirectoryError::NoHomeDirectory), |
| 61 | + } |
| 62 | + |
| 63 | + #[cfg(windows)] |
| 64 | + match cfg!(test) { |
| 65 | + true => os |
| 66 | + .env |
| 67 | + .get("USERPROFILE") |
| 68 | + .map_err(|_err| DirectoryError::NoHomeDirectory) |
| 69 | + .and_then(|h| { |
| 70 | + if h.is_empty() { |
| 71 | + Err(DirectoryError::NoHomeDirectory) |
| 72 | + } else { |
| 73 | + Ok(h) |
| 74 | + } |
| 75 | + }) |
| 76 | + .map(PathBuf::from) |
| 77 | + .map(|p| os.fs.chroot_path(p)), |
| 78 | + false => dirs::home_dir().ok_or(DirectoryError::NoHomeDirectory), |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/// The application data directory |
| 83 | +/// - Linux: `$XDG_DATA_HOME/{data_dir}` or `$HOME/.local/share/{data_dir}` |
| 84 | +/// - MacOS: `$HOME/Library/Application Support/{data_dir}` |
| 85 | +/// - Windows: `%LOCALAPPDATA%\{data_dir}` |
| 86 | +pub fn app_data_dir() -> Result<PathBuf> { |
| 87 | + Ok(dirs::data_local_dir() |
| 88 | + .ok_or(DirectoryError::NoHomeDirectory)? |
| 89 | + .join(application::DATA_DIR_NAME)) |
| 90 | +} |
| 91 | + |
| 92 | +/// Path resolver with hierarchy-aware methods |
| 93 | +pub struct PathResolver<'a> { |
| 94 | + os: &'a Os, |
| 95 | +} |
| 96 | + |
| 97 | +impl<'a> PathResolver<'a> { |
| 98 | + pub fn new(os: &'a Os) -> Self { |
| 99 | + Self { os } |
| 100 | + } |
| 101 | + |
| 102 | + /// Get workspace-scoped path resolver |
| 103 | + pub fn workspace(&self) -> WorkspacePaths<'_> { |
| 104 | + WorkspacePaths { os: self.os } |
| 105 | + } |
| 106 | + |
| 107 | + /// Get global-scoped path resolver |
| 108 | + pub fn global(&self) -> GlobalPaths<'_> { |
| 109 | + GlobalPaths { os: self.os } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +/// Workspace-scoped path methods |
| 114 | +pub struct WorkspacePaths<'a> { |
| 115 | + os: &'a Os, |
| 116 | +} |
| 117 | + |
| 118 | +impl<'a> WorkspacePaths<'a> { |
| 119 | + pub fn mcp_config(&self) -> Result<PathBuf> { |
| 120 | + Ok(self.os.env.current_dir()?.join(workspace::MCP_CONFIG)) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +/// Global-scoped path methods |
| 125 | +pub struct GlobalPaths<'a> { |
| 126 | + os: &'a Os, |
| 127 | +} |
| 128 | + |
| 129 | +impl<'a> GlobalPaths<'a> { |
| 130 | + pub fn mcp_config(&self) -> Result<PathBuf> { |
| 131 | + Ok(home_dir(self.os)?.join(global::MCP_CONFIG)) |
| 132 | + } |
| 133 | + |
| 134 | + pub fn global_context(&self) -> Result<PathBuf> { |
| 135 | + Ok(home_dir(self.os)?.join(global::GLOBAL_CONTEXT)) |
| 136 | + } |
| 137 | + |
| 138 | + pub fn profiles_dir(&self) -> Result<PathBuf> { |
| 139 | + Ok(home_dir(self.os)?.join(global::PROFILES_DIR)) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +/// Application path static methods |
| 144 | +pub struct ApplicationPaths; |
| 145 | + |
| 146 | +impl ApplicationPaths { |
| 147 | + /// Static method for settings path (to avoid circular dependency) |
| 148 | + pub fn settings_path_static() -> Result<PathBuf> { |
| 149 | + Ok(app_data_dir()?.join(application::SETTINGS_FILE)) |
| 150 | + } |
| 151 | + |
| 152 | + /// Static method for database path (to avoid circular dependency) |
| 153 | + pub fn database_path_static() -> Result<PathBuf> { |
| 154 | + Ok(app_data_dir()?.join(application::DATABASE_FILE)) |
| 155 | + } |
| 156 | +} |
0 commit comments