//! Internal module for config-related path handling.

use super::errors;

use std::path::PathBuf;

pub fn resolve_config_dir(config_path: Option<PathBuf>) -> Result<PathBuf, errors::ConfigError> {
    stack_profile::ProfileStore::resolve(config_path)
        .map(|s| s.dir().to_path_buf())
        .map_err(|_| {
            errors::ConfigError::HomeDirError(
                "Unable to resolve home directory for default CS config path".to_string(),
            )
        })
}

#[cfg(test)]
mod tests {
    #[cfg(not(target_os = "windows"))]
    use super::*;

    #[cfg(not(target_os = "windows"))]
    use std::path::Path;

    #[cfg(not(target_os = "windows"))]
    #[test]
    fn test_default_dir_is_home_slash_dot_cipherstash_on_non_windows() {
        let default_config_path = resolve_config_dir(None).unwrap();
        let expected_config_path = Path::new(std::env!("HOME")).join(".cipherstash");
        assert_eq!(default_config_path, expected_config_path)
    }
}
