use crate::Result;
use crate::dir_context::path_consts::AIPACK_BASE;
use crate::support::files::home_dir;
use simple_fs::SPath;
use std::ops::Deref;
const BIN_DIR: &str = "bin";
#[derive(Debug, Clone)]
pub struct AipackBaseDir {
path: SPath,
}
impl AipackBaseDir {
pub fn new() -> Result<Self> {
Ok(Self {
path: aipack_base_dir()?,
})
}
pub fn path(&self) -> &SPath {
&self.path
}
}
impl AipackBaseDir {
pub fn bin_dir(&self) -> SPath {
self.path.join(BIN_DIR)
}
pub fn bin_tmp_dir(&self) -> SPath {
self.path.join(BIN_DIR).join("tmp")
}
}
impl AipackBaseDir {
pub fn exists(&self) -> bool {
self.path.exists()
}
pub fn join(&self, leaf_path: impl Into<SPath>) -> SPath {
self.path.join(leaf_path.into())
}
}
impl AsRef<SPath> for AipackBaseDir {
fn as_ref(&self) -> &SPath {
&self.path
}
}
impl Deref for AipackBaseDir {
type Target = SPath;
fn deref(&self) -> &Self::Target {
&self.path
}
}
#[cfg(test)]
impl AipackBaseDir {
pub fn new_for_test(path: impl Into<SPath>) -> Result<Self> {
let path = path.into();
Ok(Self { path })
}
}
fn aipack_base_dir() -> Result<SPath> {
let home_dir = home_dir();
if !home_dir.exists() {
Err(format!("Home dir '{home_dir}' does not exist"))?;
}
let base_dir = home_dir.join(AIPACK_BASE);
Ok(base_dir)
}