use std::path::PathBuf;
use crate::Result;
use crate::engine::fs::Fs;
use crate::fs_path::DirName;
pub struct Store {
root: PathBuf,
}
impl Store {
pub fn new(root: PathBuf) -> Self {
Store { root }
}
pub fn root(&self) -> &std::path::Path {
&self.root
}
pub fn store_dir(&self) -> PathBuf {
self.root.join(".wanted")
}
pub fn installed_dir(&self) -> PathBuf {
self.store_dir().join("installed")
}
pub fn receipt_path(&self, name: &DirName) -> PathBuf {
self.installed_dir().join(name).join("receipt.toml")
}
pub fn list_installed(&self, fs: &dyn Fs) -> Result<Vec<String>> {
let installed = self.installed_dir();
if !fs.exists(&installed)? {
return Ok(Vec::new());
}
let entries = fs.read_dir(&installed)?;
let mut names: Vec<String> = entries
.into_iter()
.filter(|(_, is_dir)| *is_dir)
.map(|(name, _)| name)
.collect();
names.sort();
Ok(names)
}
}