Skip to main content

cookie_agent_protocol/
paths.rs

1//! Cross-platform per-user path resolution for Cookie Agent.
2//!
3//! This module lives in `cookie_agent_protocol` because models, config, engine,
4//! server, TUI, and tools already depend on that low-level crate. Placing it in
5//! `cookie_agent_config` would create a dependency cycle with models.
6
7use std::path::PathBuf;
8
9use thiserror::Error;
10
11/// Errors returned while resolving per-user paths.
12#[derive(Debug, Error)]
13pub enum PathError {
14    #[error("home directory is unavailable")]
15    HomeUnavailable,
16}
17
18/// Returns the current user's home directory.
19///
20/// # Errors
21///
22/// Returns [`PathError::HomeUnavailable`] when the standard library cannot
23/// determine a home directory for the current user.
24pub fn home_dir() -> Result<PathBuf, PathError> {
25    std::env::home_dir().ok_or(PathError::HomeUnavailable)
26}
27
28/// Returns the unified Cookie Agent per-user data root.
29///
30/// # Errors
31///
32/// Returns [`PathError::HomeUnavailable`] when the current user's home
33/// directory cannot be determined.
34pub fn user_data_root() -> Result<PathBuf, PathError> {
35    Ok(home_dir()?.join(".cookie-agent"))
36}
37
38#[cfg(test)]
39mod tests {
40    #[test]
41    fn user_data_root_is_below_home() {
42        let home = super::home_dir().expect("home directory");
43        assert_eq!(
44            super::user_data_root().expect("user data root"),
45            home.join(".cookie-agent")
46        );
47    }
48}