use crate::common::{AppDataType, AppDirsError, AppInfo};
use crate::utils;
use std::fs;
use std::path::PathBuf;
#[cfg(any(target_os = "macos", target_os = "ios"))]
mod platform {
mod macos;
pub use self::macos::*;
}
#[cfg(all(
unix,
not(target_os = "macos"),
not(target_os = "ios"),
not(target_os = "android")
))]
mod platform {
mod unix;
pub use self::unix::*;
}
#[cfg(windows)]
mod platform {
mod windows;
pub use self::windows::*;
}
#[cfg(not(any(windows, unix, target_os = "macos", target_os = "ios")))]
mod platform {
mod unknown;
pub use self::unknown::*;
}
#[cfg(target_os = "android")]
mod platform {
mod android;
mod unix;
pub use self::android::*;
}
pub fn app_dir(t: AppDataType, app: &AppInfo, path: &str) -> Result<PathBuf, AppDirsError> {
let path = get_app_dir(t, app, path)?;
match fs::create_dir_all(&path) {
Ok(..) => Ok(path),
Err(e) => Err(e.into()),
}
}
pub fn get_app_dir(t: AppDataType, app: &AppInfo, path: &str) -> Result<PathBuf, AppDirsError> {
if app.author.is_empty() || app.name.is_empty() {
return Err(AppDirsError::InvalidAppInfo);
}
get_app_root(t, app).map(|mut root| {
for component in path.split('/').filter(|s| !s.is_empty()) {
root.push(utils::sanitized(component));
}
root
})
}
pub fn app_root(t: AppDataType, app: &AppInfo) -> Result<PathBuf, AppDirsError> {
let path = get_app_root(t, app)?;
match fs::create_dir_all(&path) {
Ok(..) => Ok(path),
Err(e) => Err(e.into()),
}
}
pub fn get_app_root(t: AppDataType, app: &AppInfo) -> Result<PathBuf, AppDirsError> {
if app.author.is_empty() || app.name.is_empty() {
return Err(AppDirsError::InvalidAppInfo);
}
get_data_root(t).map(|mut root| {
if platform::USE_AUTHOR {
root.push(utils::sanitized(app.author));
}
root.push(utils::sanitized(app.name));
root
})
}
pub fn data_root(t: AppDataType) -> Result<PathBuf, AppDirsError> {
let path = platform::get_app_dir(t)?;
match fs::create_dir_all(&path) {
Ok(..) => Ok(path),
Err(e) => Err(e.into()),
}
}
pub fn get_data_root(t: AppDataType) -> Result<PathBuf, AppDirsError> {
platform::get_app_dir(t)
}