use std::path::PathBuf;
use std::sync::OnceLock;
use super::error::Result;
pub const IGNORE_FILES: &[&str] = &[".DS_Store", ".git", ".env"];
pub const DEFAULT_CACHE_DIR: &str = ".cache";
pub const DEFAULT_DATA_DIR: &str = ".data";
pub const UPLOAD_DIR: &str = "uploads";
pub const VERSION_DIR: &str = "__versions__";
static DIRS: OnceLock<Dirs> = OnceLock::new();
#[derive(Debug, Clone, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
pub struct Dirs {
pub cache: PathBuf,
pub data: PathBuf,
}
impl Dirs {
pub fn new(cache: PathBuf, data: PathBuf) -> Self { Self { cache, data } }
pub fn get() -> &'static Dirs { DIRS.get_or_init(Dirs::default) }
pub fn init(dirs: Dirs) -> std::result::Result<(), Dirs> { DIRS.set(dirs) }
pub fn ensure_exists(&self) -> Result<()> {
let create = |dir: &PathBuf, fail: bool| -> Result<()> {
match std::fs::create_dir_all(dir) {
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => (),
Err(e) if fail => Err(e)?,
_ => (),
}
Ok(())
};
create(&self.cache, false)?;
create(&self.data, true)?;
Ok(())
}
}
impl Default for Dirs {
fn default() -> Self {
Self { cache: PathBuf::from(DEFAULT_CACHE_DIR), data: PathBuf::from(DEFAULT_DATA_DIR) }
}
}