mod proj_dirs;
mod project_triplet;
pub mod dir_utils;
pub mod strategy;
pub use proj_dirs::{FullProjectDirs, MissingError, ProjectDirs};
pub struct Project {
_orig_qualifier: String,
_orig_organization: String,
_orig_application: String,
qualifier_value: String,
organization_name: String,
application_name: String,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "strum", derive(strum::Display, strum::EnumString))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "kebab-case"))]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub enum Directory {
Bin,
Cache,
Config,
Data,
Include,
Lib,
Log,
ProjectRoot,
Runtime,
State,
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct Scoped {
pub user: ProjectDirs,
pub system: ProjectDirs,
pub local: ProjectDirs,
}
impl Project {
pub fn new(qualifier: &str, organization: &str, application: &str) -> Self {
Self {
_orig_qualifier: qualifier.to_string(),
_orig_organization: organization.to_string(),
_orig_application: application.to_string(),
qualifier_value: project_triplet::qualifier_cleanup(qualifier),
organization_name: project_triplet::name_cleanup(organization),
application_name: project_triplet::name_cleanup(application),
}
}
pub fn application_name_unix(&self) -> String {
project_triplet::unix_name_cleanup(&self._orig_application)
}
pub fn application_name_windows(&self) -> String {
project_triplet::windows_name_cleanup(&self._orig_application)
}
pub fn application_name_macos(&self) -> String {
project_triplet::unix_name_cleanup(&self._orig_application)
}
pub fn organization_name_windows(&self) -> String {
project_triplet::windows_name_cleanup(&self._orig_organization)
}
pub fn organization_name(&self) -> &str {
&self.organization_name
}
pub fn application_name(&self) -> &str {
&self.application_name
}
pub fn qualifier(&self) -> &str {
&self.qualifier_value
}
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
fn unix_project_dirs(&self) -> Scoped {
use crate::strategy::fhs::Fhs;
use crate::strategy::unix::Unix;
use crate::strategy::xdg::{Xdg, XdgEnv};
Scoped {
user: self
.xdg_with_env(XdgEnv::new_system())
.map(|x| x.into())
.unwrap_or(ProjectDirs::empty()),
system: self.fhs().into(),
local: self
.unix_pwd()
.map(Into::into)
.unwrap_or(ProjectDirs::empty()),
}
}
#[cfg(target_os = "windows")]
fn windows_project_dirs(&self) -> Scoped {
use crate::strategy::unix::Unix;
use crate::strategy::windows::{Windows, WindowsEnv};
let windows_env = WindowsEnv::new_system();
Scoped {
user: self.windows_user_with_env(windows_env.clone()),
system: self.windows_system_with_env(windows_env),
local: self
.unix_pwd()
.map(Into::into)
.unwrap_or(ProjectDirs::empty()),
}
}
pub fn project_dirs(&self) -> Scoped {
#[cfg(all(target_family = "unix", not(target_os = "macos")))]
{
self.unix_project_dirs()
}
#[cfg(all(target_family = "unix", target_os = "macos"))]
{
todo!()
}
#[cfg(target_family = "windows")]
{
self.windows_project_dirs()
}
}
}