use camino::{Utf8Path, Utf8PathBuf};
use crate::Directories;
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ServiceDirs {
cache_dir: Utf8PathBuf,
config_dir: Utf8PathBuf,
data_dir: Utf8PathBuf,
}
impl ServiceDirs {
#[allow(unused_variables)]
#[must_use]
pub fn new(organization: impl AsRef<str>, application: impl AsRef<str>) -> Self {
fn inner(organization: &str, application: &str) -> ServiceDirs {
#[cfg(unix)]
{
ServiceDirs {
cache_dir: Utf8PathBuf::from(format!("/var/cache/{application}")),
config_dir: Utf8PathBuf::from(format!("/etc/{application}")),
data_dir: Utf8PathBuf::from(format!("/var/lib/{application}")),
}
}
#[cfg(windows)]
{
let app_data = "C:\\Windows\\ServiceProfiles\\NetworkService\\AppData";
let project_dir = format!("{}/{}", organization, application);
ServiceDirs {
cache_dir: Utf8PathBuf::from(format!(
"{}\\Local\\{}\\cache",
app_data, project_dir
)),
config_dir: Utf8PathBuf::from(format!(
"{}\\Roaming\\{}\\config",
app_data, project_dir
)),
data_dir: Utf8PathBuf::from(format!(
"{}\\Roaming\\{}\\data",
app_data, project_dir
)),
}
}
#[cfg(not(any(unix, windows)))]
{
compile_error!("OS not supported")
}
}
inner(organization.as_ref(), application.as_ref())
}
}
impl Directories for ServiceDirs {
fn cache_dir(&self) -> &Utf8Path {
&self.cache_dir
}
fn config_dir(&self) -> &Utf8Path {
&self.config_dir
}
fn data_dir(&self) -> &Utf8Path {
&self.data_dir
}
}