auths_core/paths.rs
1//! Shared path resolution for the Auths home directory.
2//!
3//! All code that needs the `~/.auths` directory should call [`auths_home_with_config`]
4//! (or the legacy shim [`auths_home`]) instead of hardcoding
5//! `dirs::home_dir().join(".auths")`. This enables multi-agent simulation and CI
6//! overrides via the `AUTHS_HOME` env var.
7
8use std::path::PathBuf;
9
10use crate::config::EnvironmentConfig;
11
12/// Resolves the Auths home directory from an injected `EnvironmentConfig`.
13///
14/// Uses `config.auths_home` when set; otherwise falls back to `~/.auths`.
15/// Prefer this over `auths_home()` for new code — it keeps env-var reads at the
16/// process boundary.
17///
18/// Args:
19/// * `config` - The environment configuration to source the home path from.
20///
21/// Usage:
22/// ```ignore
23/// let env = auths_core::config::EnvironmentConfig::from_env();
24/// let dir = auths_core::paths::auths_home_with_config(&env)?;
25/// ```
26pub fn auths_home_with_config(config: &EnvironmentConfig) -> Result<PathBuf, AuthsHomeError> {
27 if let Some(ref home) = config.auths_home {
28 return Ok(home.clone());
29 }
30 let home = dirs::home_dir().ok_or(AuthsHomeError::NoHomeDir)?;
31 Ok(home.join(".auths"))
32}
33
34/// Resolves the Auths home directory.
35///
36/// Reads `AUTHS_HOME` from the environment (via `EnvironmentConfig::from_env()`),
37/// then falls back to `~/.auths`.
38///
39/// Prefer `auths_home_with_config` for new code.
40///
41/// Usage:
42/// ```ignore
43/// let dir = auths_core::paths::auths_home()?;
44/// let keys = dir.join("keys.enc");
45/// ```
46pub fn auths_home() -> Result<PathBuf, AuthsHomeError> {
47 auths_home_with_config(&EnvironmentConfig::from_env())
48}
49
50/// Error returned when the Auths home directory cannot be resolved.
51#[derive(Debug, thiserror::Error)]
52pub enum AuthsHomeError {
53 /// The user's home directory could not be determined.
54 #[error("Could not determine home directory")]
55 NoHomeDir,
56}
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61
62 #[test]
63 fn explicit_home_overrides_default() {
64 let env = EnvironmentConfig::builder()
65 .auths_home(std::path::PathBuf::from("/tmp/custom-auths"))
66 .build();
67 assert_eq!(
68 auths_home_with_config(&env).unwrap(),
69 std::path::PathBuf::from("/tmp/custom-auths")
70 );
71 }
72
73 #[test]
74 fn no_override_falls_back_to_default() {
75 let env = EnvironmentConfig::builder().build();
76 let path = auths_home_with_config(&env).unwrap();
77 assert!(path.ends_with(".auths"));
78 }
79}