dev_scripts/
cache.rs

1//! Directory handling.
2use std::path::{Path, PathBuf};
3
4use dirs::cache_dir;
5use log::debug;
6
7use crate::{
8    Error,
9    consts::{PROJECT_NAME, TESTING_DIR},
10};
11
12/// The directory in which all downloads and test artifacts reside.
13#[derive(Clone, Debug)]
14pub struct CacheDir(PathBuf);
15
16impl CacheDir {
17    /// Creates a new [`CacheDir`] from XDG default location.
18    ///
19    /// Defaults to `$XDG_CACHE_HOME/alpm/testing`.
20    /// If `$XDG_CACHE_HOME` is unset, falls back to `~/.cache/alpm/testing`.
21    ///
22    /// # Errors
23    ///
24    /// Returns an error if the cache directory for the current user cannot be retrieved.
25    pub fn from_xdg() -> Result<Self, Error> {
26        let path = cache_dir()
27            .ok_or(Error::CannotGetCacheDir)?
28            .join(PROJECT_NAME)
29            .join(TESTING_DIR);
30        debug!("Using path {path:?} as cache dir.");
31
32        Ok(Self(path))
33    }
34}
35
36impl From<PathBuf> for CacheDir {
37    fn from(value: PathBuf) -> Self {
38        debug!("Using path {value:?} as cache dir.");
39
40        Self(value)
41    }
42}
43
44impl AsRef<Path> for CacheDir {
45    fn as_ref(&self) -> &Path {
46        self.0.as_path()
47    }
48}