garmin_cli/config/
mod.rs

1mod credentials;
2
3pub use credentials::CredentialStore;
4
5use crate::error::{GarminError, Result};
6use std::path::PathBuf;
7
8/// Default configuration directory name
9const CONFIG_DIR_NAME: &str = "garmin";
10
11/// Get the configuration directory path
12/// Returns ~/.config/garmin on Unix, ~/Library/Application Support/garmin on macOS
13pub fn config_dir() -> Result<PathBuf> {
14    dirs::config_dir()
15        .map(|p| p.join(CONFIG_DIR_NAME))
16        .ok_or_else(|| GarminError::config("Could not determine config directory"))
17}
18
19/// Get the data directory path for storing tokens
20/// Returns ~/.local/share/garmin on Unix, ~/Library/Application Support/garmin on macOS
21pub fn data_dir() -> Result<PathBuf> {
22    dirs::data_dir()
23        .map(|p| p.join(CONFIG_DIR_NAME))
24        .ok_or_else(|| GarminError::config("Could not determine data directory"))
25}
26
27/// Ensure a directory exists, creating it if necessary
28pub fn ensure_dir(path: &PathBuf) -> Result<()> {
29    if !path.exists() {
30        std::fs::create_dir_all(path)?;
31    }
32    Ok(())
33}
34
35#[cfg(test)]
36mod tests {
37    use super::*;
38
39    #[test]
40    fn test_config_dir_exists() {
41        let dir = config_dir();
42        assert!(dir.is_ok());
43        let path = dir.unwrap();
44        assert!(path.ends_with("garmin"));
45    }
46
47    #[test]
48    fn test_data_dir_exists() {
49        let dir = data_dir();
50        assert!(dir.is_ok());
51        let path = dir.unwrap();
52        assert!(path.ends_with("garmin"));
53    }
54}