| 1 |
use crate::common::{AppDataType, AppDirsError, AppInfo}; |
| 2 |
use crate::utils; |
| 3 |
use std::fs; |
| 4 |
use std::path::PathBuf; |
| 5 |
|
| 6 |
#[cfg(any(target_os = "macos", target_os = "ios"))] |
| 7 |
mod platform { |
| 8 |
mod macos; |
| 9 |
pub use self::macos::*; |
| 10 |
} |
| 11 |
#[cfg(all(unix, not(target_os = "macos"), not(target_os = "ios"), not(target_os = "android")))] |
| 12 |
mod platform { |
| 13 |
mod unix; |
| 14 |
pub use self::unix::*; |
| 15 |
} |
| 16 |
#[cfg(windows)] |
| 17 |
mod platform { |
| 18 |
mod windows; |
| 19 |
pub use self::windows::*; |
| 20 |
} |
| 21 |
#[cfg(not(any(windows, unix, target_os = "macos", target_os = "ios")))] |
| 22 |
mod platform { |
| 23 |
mod unknown; |
| 24 |
pub use self::unknown::*; |
| 25 |
} |
| 26 |
#[cfg(target_os = "android")] |
| 27 |
mod platform { |
| 28 |
mod android; |
| 29 |
pub use self::android::*; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
pub fn app_dir(t: AppDataType, app: &AppInfo, path: &str) -> Result<PathBuf, AppDirsError> { |
| 42 |
let path = get_app_dir(t, app, path)?; |
| 43 |
match fs::create_dir_all(&path) { |
| 44 |
Ok(..) => Ok(path), |
| 45 |
Err(e) => Err(e.into()), |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
pub fn get_app_dir(t: AppDataType, app: &AppInfo, path: &str) -> Result<PathBuf, AppDirsError> { |
| 59 |
if app.author.is_empty() || app.name.is_empty() { |
| 60 |
return Err(AppDirsError::InvalidAppInfo); |
| 61 |
} |
| 62 |
get_app_root(t, app).map(|mut root| { |
| 63 |
for component in path.split('/').filter(|s| !s.is_empty()) { |
| 64 |
root.push(utils::sanitized(component)); |
| 65 |
} |
| 66 |
root |
| 67 |
}) |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
pub fn app_root(t: AppDataType, app: &AppInfo) -> Result<PathBuf, AppDirsError> { |
| 77 |
let path = get_app_root(t, app)?; |
| 78 |
match fs::create_dir_all(&path) { |
| 79 |
Ok(..) => Ok(path), |
| 80 |
Err(e) => Err(e.into()), |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
pub fn get_app_root(t: AppDataType, app: &AppInfo) -> Result<PathBuf, AppDirsError> { |
| 91 |
if app.author.is_empty() || app.name.is_empty() { |
| 92 |
return Err(AppDirsError::InvalidAppInfo); |
| 93 |
} |
| 94 |
get_data_root(t).map(|mut root| { |
| 95 |
if platform::USE_AUTHOR { |
| 96 |
root.push(utils::sanitized(app.author)); |
| 97 |
} |
| 98 |
root.push(utils::sanitized(app.name)); |
| 99 |
root |
| 100 |
}) |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
pub fn data_root(t: AppDataType) -> Result<PathBuf, AppDirsError> { |
| 110 |
let path = platform::get_app_dir(t)?; |
| 111 |
match fs::create_dir_all(&path) { |
| 112 |
Ok(..) => Ok(path), |
| 113 |
Err(e) => Err(e.into()), |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
pub fn get_data_root(t: AppDataType) -> Result<PathBuf, AppDirsError> { |
| 124 |
platform::get_app_dir(t) |
| 125 |
} |