use std::path::{Path, PathBuf};
use haz_domain::settings::cache::HashAlgo;
use haz_vfs::Filesystem;
use crate::layout;
#[derive(Debug, Clone)]
pub struct CacheReader<Fs: Filesystem> {
fs: Fs,
workspace_root: PathBuf,
root: PathBuf,
hash_algo: HashAlgo,
}
impl<Fs: Filesystem> CacheReader<Fs> {
pub fn new(fs: Fs, workspace_root: &Path, hash_algo: HashAlgo) -> Self {
Self {
fs,
workspace_root: workspace_root.to_path_buf(),
root: layout::cache_root(workspace_root),
hash_algo,
}
}
#[must_use]
pub fn workspace_root(&self) -> &Path {
&self.workspace_root
}
#[must_use]
pub fn cache_root(&self) -> &Path {
&self.root
}
#[must_use]
pub fn hash_algo(&self) -> HashAlgo {
self.hash_algo
}
#[must_use]
pub fn fs(&self) -> &Fs {
&self.fs
}
}
#[cfg(test)]
mod tests {
use std::path::Path;
use haz_domain::settings::cache::HashAlgo;
use haz_vfs::Filesystem;
use haz_vfs_testing::MemFilesystem;
use super::CacheReader;
#[test]
fn workspace_root_is_preserved() {
let fs = MemFilesystem::new();
let cache = CacheReader::new(fs, Path::new("/ws"), HashAlgo::Blake3);
assert_eq!(cache.workspace_root(), Path::new("/ws"));
}
#[test]
fn cache_010_cache_root_is_workspace_dot_haz_cache() {
let fs = MemFilesystem::new();
let cache = CacheReader::new(fs, Path::new("/ws"), HashAlgo::Blake3);
assert_eq!(cache.cache_root(), Path::new("/ws/.haz/cache"));
}
#[test]
fn cache_002_hash_algo_is_preserved() {
let fs = MemFilesystem::new();
let cache = CacheReader::new(fs, Path::new("/ws"), HashAlgo::Sha256);
assert_eq!(cache.hash_algo(), HashAlgo::Sha256);
}
#[test]
fn fs_accessor_returns_the_handle_passed_in() {
let mut fs = MemFilesystem::new();
fs.add_dir("/ws").unwrap();
let cache = CacheReader::new(fs, Path::new("/ws"), HashAlgo::Blake3);
cache.fs().metadata(Path::new("/ws")).unwrap();
}
}