use std::env;
use camino::{Utf8Path, Utf8PathBuf};
use crate::Directories;
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct LocalDirs {
cache_dir: Utf8PathBuf,
config_dir: Utf8PathBuf,
data_dir: Utf8PathBuf,
}
impl LocalDirs {
#[must_use]
pub fn new() -> Option<Self> {
let base = env::current_dir().ok()?;
let base = Utf8PathBuf::from_path_buf(base).ok()?.join(".local");
Some(Self::new_at(base))
}
pub fn new_at(base: impl AsRef<Utf8Path>) -> Self {
fn inner(base: &Utf8Path) -> LocalDirs {
LocalDirs {
cache_dir: base.join("cache"),
config_dir: base.join("config"),
data_dir: base.join("data"),
}
}
inner(base.as_ref())
}
}
impl Directories for LocalDirs {
fn cache_dir(&self) -> &Utf8Path {
&self.cache_dir
}
fn config_dir(&self) -> &Utf8Path {
&self.config_dir
}
fn data_dir(&self) -> &Utf8Path {
&self.data_dir
}
}