#![allow(clippy::option_if_let_else)]
use std::path::PathBuf;
use crate::error::Error;
#[derive(Debug, Clone, Copy)]
pub enum Directories {
GlobalState,
RelativeCache,
GlobalCache,
}
impl Directories {
pub fn basedir(&self) -> Result<PathBuf, Error> {
match self {
Self::GlobalState => global_state_dir(),
Self::RelativeCache => Ok(Cache::Root.basedir()),
Self::GlobalCache => Ok(global_cache_dir()?.join(Cache::Root.basedir())),
}
}
}
const CACHE_BASE: &str = ".wick";
#[derive(Debug, Clone, Copy)]
pub enum Cache {
Root,
Assets,
}
impl Cache {
#[must_use]
pub fn basedir(&self) -> PathBuf {
match self {
Self::Root => PathBuf::from(CACHE_BASE),
Self::Assets => PathBuf::from("remote"),
}
}
}
fn global_state_dir() -> Result<PathBuf, Error> {
#[cfg(not(target_os = "windows"))]
return Ok(match xdg::BaseDirectories::with_prefix("wick") {
Ok(xdg) => xdg.get_state_home(),
Err(_) => std::env::current_dir().map_err(|_| Error::Pwd)?,
});
#[cfg(target_os = "windows")]
return Ok(match std::env::var("LOCALAPPDATA") {
Ok(var) => PathBuf::from(format!("{}/wick", var)),
Err(_) => std::env::current_dir().map_err(|_| Error::Pwd)?,
});
}
fn global_cache_dir() -> Result<PathBuf, Error> {
#[cfg(not(target_os = "windows"))]
return Ok(match xdg::BaseDirectories::with_prefix("wick") {
Ok(xdg) => xdg.get_cache_home(),
Err(_) => std::env::temp_dir(),
});
#[cfg(target_os = "windows")]
return Ok(std::env::temp_dir());
}