1use std::path::{Path, PathBuf};
4
5#[derive(Debug, Clone)]
7pub struct CacheDir {
8 root: PathBuf,
9}
10
11impl CacheDir {
12 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 #[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 #[must_use]
36 pub fn source_dir(&self, key: &str) -> PathBuf {
37 self.root.join("sources").join(key)
38 }
39}