use std::path::PathBuf;
pub struct CacheLayout {
root: PathBuf,
}
impl CacheLayout {
pub fn new() -> Self {
if let Ok(dir) = std::env::var("BV_CACHE_DIR") {
return Self::with_root(PathBuf::from(dir));
}
let root = xdg_cache_home().join("bv");
Self { root }
}
pub fn with_root(root: PathBuf) -> Self {
Self { root }
}
pub fn root(&self) -> &PathBuf {
&self.root
}
pub fn image_dir(&self, digest: &str) -> PathBuf {
self.root.join("images").join(digest)
}
pub fn manifest_path(&self, tool_id: &str, version: &str) -> PathBuf {
self.root
.join("tools")
.join(tool_id)
.join(version)
.join("manifest.toml")
}
pub fn index_dir(&self, index_name: &str) -> PathBuf {
self.root.join("index").join(index_name)
}
pub fn data_dir(&self, dataset_id: &str, version: &str) -> PathBuf {
self.root.join("data").join(dataset_id).join(version)
}
pub fn sif_dir(&self) -> PathBuf {
self.root.join("sif")
}
pub fn tmp_dir(&self) -> PathBuf {
self.root.join("tmp")
}
}
impl Default for CacheLayout {
fn default() -> Self {
Self::new()
}
}
fn xdg_cache_home() -> PathBuf {
if let Ok(dir) = std::env::var("XDG_CACHE_HOME") {
return PathBuf::from(dir);
}
let home = std::env::var_os("HOME")
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from("."));
home.join(".cache")
}