Skip to main content

caixa_resolver/
cache.rs

1//! Cache directory discovery — XDG-respecting.
2
3use std::path::{Path, PathBuf};
4
5/// A cache root — `~/.cache/caixa` (or `$XDG_CACHE_HOME/caixa`).
6#[derive(Debug, Clone)]
7pub struct CacheDir {
8    root: PathBuf,
9}
10
11impl CacheDir {
12    /// Discover the default cache directory and ensure it exists.
13    pub fn discover() -> std::io::Result<Self> {
14        let root = dirs::cache_dir()
15            .unwrap_or_else(|| {
16                PathBuf::from(std::env::var("HOME").unwrap_or_else(|_| ".".into())).join(".cache")
17            })
18            .join("caixa");
19        std::fs::create_dir_all(&root)?;
20        Ok(Self { root })
21    }
22
23    /// Use an explicit directory. Caller is responsible for its existence.
24    #[must_use]
25    pub fn at(path: impl Into<PathBuf>) -> Self {
26        Self { root: path.into() }
27    }
28
29    #[must_use]
30    pub fn root(&self) -> &Path {
31        &self.root
32    }
33
34    /// Per-source directory, keyed by a BLAKE3 hash of the canonical URL + ref.
35    #[must_use]
36    pub fn source_dir(&self, key: &str) -> PathBuf {
37        self.root.join("sources").join(key)
38    }
39}