Skip to main content

docsrs_cli/cache/
paths.rs

1//! Cache root resolution (CLI override / XDG).
2
3use std::path::PathBuf;
4
5/// Resolve cache root: explicit CLI override, then XDG cache home.
6///
7/// Precedence:
8/// 1. Explicit override (`--cache-dir` / caller)
9/// 2. `directories::ProjectDirs` cache dir
10///
11/// Product never reads path knobs from environment variables.
12pub fn resolve_cache_dir(override_dir: Option<PathBuf>) -> Option<PathBuf> {
13    resolve_cache_dir_with_source(override_dir).0
14}
15
16/// Resolve cache root and report which layer won.
17pub fn resolve_cache_dir_with_source(
18    override_dir: Option<PathBuf>,
19) -> (Option<PathBuf>, crate::config::PathSource) {
20    use crate::config::PathSource;
21    if let Some(p) = override_dir {
22        return (Some(p), PathSource::CliFlag);
23    }
24    if let Some(p) = directories::ProjectDirs::from("", "", crate::config::APP_NAME)
25        .map(|d| d.cache_dir().to_path_buf())
26    {
27        return (Some(p), PathSource::Xdg);
28    }
29    (None, PathSource::Unresolved)
30}